RoundedCornersPlugin.inc.php 4.77 KB
Newer Older
jalperin's avatar
jalperin committed
1
2
3
<?php

/**
asmecher's avatar
asmecher committed
4
 * @file RoundedCornersPlugin.inc.php
jalperin's avatar
jalperin committed
5
 *
asmecher's avatar
asmecher committed
6
 * Copyright (c) 2007-2009 Juan Pablo Alperin, Gunther Eysenbach
jalperin's avatar
jalperin committed
7
8
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
asmecher's avatar
asmecher committed
9
 * @class RoundedCornersPlugin
asmecher's avatar
asmecher committed
10
 * @ingroup plugins_generic_roundedCorners
jalperin's avatar
jalperin committed
11
 *
asmecher's avatar
asmecher committed
12
 * @brief Rounded Corners plugin class
jalperin's avatar
jalperin committed
13
14
 *
 */
asmecher's avatar
asmecher committed
15

jalperin's avatar
jalperin committed
16
17
18
19
20
21
22
import('classes.plugins.GenericPlugin');

class RoundedCornersPlugin extends GenericPlugin {

	function getName() {
		return 'RoundedCornersPlugin';
	}
asmecher's avatar
asmecher committed
23

jalperin's avatar
jalperin committed
24
25
26
	function getDisplayName() {
		return Locale::translate('plugins.generic.roundedcorners.displayName');
	}
asmecher's avatar
asmecher committed
27

jalperin's avatar
jalperin committed
28
29
30
31
32
33
34
	function getDescription() {
		return Locale::translate('plugins.generic.roundedcorners.description');
	}   

	function register($category, $path) {
		if (!Config::getVar('general', 'installed')) return false;
		if (parent::register($category, $path)) {	
asmecher's avatar
asmecher committed
35
36
			if ( $this->getEnabled() ) {
				HookRegistry::register('TemplateManager::display', array(&$this, 'templateManagerCallback'));
jalperin's avatar
jalperin committed
37
38
39
40
41
42
			}
			$this->addLocaleData();
			return true;
		}
		return false;
	}
asmecher's avatar
asmecher committed
43

44
	function templateManagerCallback($hookName, $args) {
asmecher's avatar
asmecher committed
45
46
47
48
49
50
51
		$templateMgr =& $args[0]; //TemplateManager::getManager();
		$baseUrl = $templateMgr->get_template_vars('baseUrl');
		$roundedCornerCssUrl = $baseUrl . '/plugins/generic/roundedCorners/roundedcorners.css';
		$templateMgr->addStyleSheet($roundedCornerCssUrl);
		$templateMgr->register_outputfilter(array('RoundedCornersPlugin', 'roundOutputFilter'));
	}

jalperin's avatar
jalperin committed
52
53
54
55
	/**
	 * Determine whether or not this plugin is enabled.
	 */
	function getEnabled() {
asmecher's avatar
asmecher committed
56
		$journal =& Request::getJournal();
jalperin's avatar
jalperin committed
57
58
59
60
61
62
63
64
		if (!$journal) return false;
		return $this->getSetting($journal->getJournalId(), 'enabled');
	}

	/**
	 * Set the enabled/disabled state of this plugin
	 */
	function setEnabled($enabled) {
asmecher's avatar
asmecher committed
65
		$journal =& Request::getJournal();
jalperin's avatar
jalperin committed
66
67
68
69
70
71
		if ($journal) {
			$this->updateSetting($journal->getJournalId(), 'enabled', $enabled ? true : false);
			return true;
		}
		return false;
	}
asmecher's avatar
asmecher committed
72

jalperin's avatar
jalperin committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	/**
	 * Display verbs for the management interface.
	 */
	function getManagementVerbs() {
		$verbs = array();
		if ($this->getEnabled()) {
			$verbs[] = array(
				'disable',
				Locale::translate('manager.plugins.disable')
			);
		} else {
			$verbs[] = array(
				'enable',
				Locale::translate('manager.plugins.enable')
			);
		}
		return $verbs;
	}
asmecher's avatar
asmecher committed
91

jalperin's avatar
jalperin committed
92
93
	/**
	 * Perform management functions
94
95
96
 	 * @param $verb string
 	 * @param $args array
	 * @param $message string Location for the plugin to put a result msg
jalperin's avatar
jalperin committed
97
	 */
98
	function manage($verb, $args, &$message) {
jalperin's avatar
jalperin committed
99
		$returner = false;
asmecher's avatar
asmecher committed
100

101
102
103
104
105
106
107
108
109
110
		switch ($verb) {
			case 'enable':
				$this->setEnabled(true);
				$message = Locale::translate('plugins.generic.roundedcorners.enabled');
				break;
			case 'disable':
				$this->setEnabled(false);
				$message = Locale::translate('plugins.generic.roundedcorners.disabled');
				break;
		}
jalperin's avatar
jalperin committed
111
112
		return $returner;		
	}
asmecher's avatar
asmecher committed
113

jalperin's avatar
jalperin committed
114
115
116
	/** 
	 * Do the work of adding in the <span> blocks
	 */	
asmecher's avatar
asmecher committed
117
118
	function roundOutputFilter($output, &$smarty) {
		$top = '<span class="rtop"><span class="r1"></span><span class="r2"></span><span class="r3"></span><span class="r4"></span></span><div class="roundedCorner">';
jalperin's avatar
jalperin committed
119
120
		$bottom = '</div><span class="rbottom"><span class="r4"></span><span class="r3"></span><span class="r2"></span><span class="r1"></span></span>';
		$newOutput = $output;
asmecher's avatar
asmecher committed
121

asmecher's avatar
asmecher committed
122
		$matches = RoundedCornersPlugin::_getDivs($newOutput, 'block');
asmecher's avatar
asmecher committed
123
124
125
		if (count($matches) > 0) {
			foreach ($matches as $match) {	
				if (preg_match('/<div[^>]+class\=\"block\"[^>]*>(\s*)(<\/div>[^<]*)$/', $match) > 0 ) continue;
asmecher's avatar
asmecher committed
126

asmecher's avatar
asmecher committed
127
				$newBlock = preg_replace('/(<div[^>]+class\=\"block)(\"[^>]*>)/is', "\\1 alreadyRounded\\2$top", $match, PREG_OFFSET_CAPTURE);
asmecher's avatar
asmecher committed
128
				$newBlock = preg_replace('/([^>]*)(<\/div>[^<]*)$/', "\\1$bottom\\2", $newBlock);
asmecher's avatar
asmecher committed
129

asmecher's avatar
asmecher committed
130
				$newOutput = str_replace($match, $newBlock, $newOutput);
jalperin's avatar
jalperin committed
131
132
			}
		}
asmecher's avatar
asmecher committed
133

jalperin's avatar
jalperin committed
134
135
		return $newOutput;
	}
asmecher's avatar
asmecher committed
136

jalperin's avatar
jalperin committed
137
138
139
140
	/**
	 * look for the opening and closing divs with a particular $class in the $subject 
	 * Have to count opening and closing divs since regexes are not so good matching opening and closing tags
	 */
asmecher's avatar
asmecher committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
	function _getDivs($subject, $class) {
		preg_match_all("/<div[^>]+class\=\"$class\"[^>]*>/is", $subject, $matches, PREG_OFFSET_CAPTURE);

		$matches = $matches[0];
		for ($i=0; $i<count($matches); $i++) {
			$openDivs = 0;
			$closedDivs = 0;
			$divClosePosition = 0;
			$divPosition = array();
			preg_match_all("/<\/?div[^>]*>/is", $subject, $divPosition, PREG_OFFSET_CAPTURE, $matches[$i][1]);
			$divPosition = $divPosition[0];
			for ($i2=0; $i2<count($divPosition); $i2++) {
				if (eregi("\/", $divPosition[$i2][0])) {
					$closedDivs++;
				} else {
					$openDivs++;
				}
jalperin's avatar
jalperin committed
158

asmecher's avatar
asmecher committed
159
160
161
162
163
164
165
166
167
168
				if($closedDivs > $openDivs-1) {
					$divClosePosition = $divPosition[$i2][1];
					$divLength = $divClosePosition+6 - $matches[$i][1];
					$divs[$i] = substr($subject, $matches[$i][1], $divLength);
					break;
				}
			}
		}
		return $divs;
	}
jalperin's avatar
jalperin committed
169
}
asmecher's avatar
asmecher committed
170

jalperin's avatar
jalperin committed
171
?>