00001 <?php
00002
00015
00016
00017 define ('AD_ORIENTATION_LEFT', 1);
00018 define ('AD_ORIENTATION_RIGHT', 2);
00019 define ('AD_ORIENTATION_CENTRE', 3);
00020
00021 import('classes.plugins.GenericPlugin');
00022
00023 class OpenAdsPlugin extends GenericPlugin {
00025 var $templateName;
00026
00033 function register($category, $path) {
00034 if (parent::register($category, $path)) {
00035 $this->addLocaleData();
00036
00037 if ($this->getEnabled()) {
00038 HookRegistry::register('TemplateManager::display', array(&$this, 'mainCallback'));
00039 HookRegistry::register('PluginRegistry::loadCategory', array(&$this, 'callbackLoadCategory'));
00040 }
00041
00042 return true;
00043 }
00044 return false;
00045 }
00046
00054 function callbackLoadCategory($hookName, $args) {
00055 $category =& $args[0];
00056 $plugins =& $args[1];
00057 switch ($category) {
00058 case 'blocks':
00059 $this->import('OpenAdsBlockPlugin');
00060 $openAdsBlockPlugin =& new OpenAdsBlockPlugin();
00061 $plugins[$category][$openAdsBlockPlugin->getSeq()] =& $openAdsBlockPlugin;
00062 break;
00063 }
00064 return false;
00065 }
00066
00072 function getName() {
00073 return 'OpenAdsPlugin';
00074 }
00075
00076 function getDisplayName() {
00077 $this->addLocaleData();
00078 return Locale::translate('plugins.generic.openads');
00079 }
00080
00081 function getDescription() {
00082 $this->addLocaleData();
00083 return Locale::translate($this->isConfigured()?'plugins.generic.openads.description':'plugins.generic.openads.descriptionUnconfigured');
00084 }
00085
00086 function mainCallback($hookName, $args) {
00087 $smarty =& $args[0];
00088 $template =& $args[1];
00089 if ($template == 'rt/rt.tpl') {
00090 $smarty->register_outputfilter(array(&$this, 'rtOutputFilter'));
00091 } else {
00092 $smarty->register_outputfilter(array(&$this, 'mainOutputFilter'));
00093 }
00094 $this->templateName = $template;
00095 return false;
00096 }
00097
00098 function mainOutputFilter($output, &$smarty) {
00099 $journal =& Request::getJournal();
00100
00101
00102 $headerAdHtml = $contentAdHtml = '';
00103 if ($journal) {
00104 $journalId = $journal->getJournalId();
00105 $this->import('OpenAdsConnection');
00106 $openAdsConnection =& new OpenAdsConnection($this, $this->getInstallationPath());
00107 $headerAdHtml = $openAdsConnection->getAdHtml($this->getSetting($journalId, 'headerAdId'));
00108 $headerAdOrientation = $this->getSetting($journal->getJournalId(), 'headerAdOrientation');
00109 $contentAdHtml = $openAdsConnection->getAdHtml($this->getSetting($journalId, 'contentAdId'));
00110 }
00111
00112
00113 if (!empty($headerAdHtml)) {
00114 if (($index = strpos($output, '<h1>')) !== false) {
00115 $smarty->unregister_outputfilter('mainOutputFilter');
00116 $newOutput = substr($output, 0, $index);
00117 switch ($headerAdOrientation) {
00118 case AD_ORIENTATION_CENTRE:
00119 $newOutput .= '<center>';
00120 $newOutput .= $headerAdHtml;
00121 $newOutput .= '</center>';
00122 break;
00123 case AD_ORIENTATION_RIGHT:
00124 $newOutput .= '<span style="float: right">';
00125 $newOutput .= $headerAdHtml;
00126 $newOutput .= '</span>';
00127 break;
00128 case AD_ORIENTATION_LEFT:
00129 default:
00130 $newOutput .= $headerAdHtml;
00131 break;
00132 }
00133 $newOutput .= substr($output, $index);
00134 $output =& $newOutput;
00135 } else if (($index = strpos($output, '<h2>')) !== false) {
00136 $smarty->unregister_outputfilter('mainOutputFilter');
00137 $newOutput = substr($output, 0, $index);
00138 $newOutput .= $headerAdHtml;
00139 $newOutput .= substr($output, $index);
00140 $output =& $newOutput;
00141 }
00142 }
00143
00144 if (in_array($this->templateName, array('article/article.tpl'))) {
00145 $output = str_replace ('{$adContent}', $contentAdHtml, $output);
00146 } else {
00147 $output = str_replace ('{$adContent}', '', $output);
00148 }
00149
00150 return $output;
00151
00152 }
00153
00157 function rtOutputFilter($output, &$smarty) {
00158 $journal =& Request::getJournal();
00159 if (!$journal) return $output;
00160
00161
00162 $this->import('OpenAdsConnection');
00163 $openAdsConnection =& new OpenAdsConnection($this, $this->getInstallationPath());
00164 $sidebarAdHtml = $openAdsConnection->getAdHtml($this->getSetting($journal->getJournalId(), 'sidebarAdId'));
00165
00166 $index = strrpos($output, '<h5>' . Locale::translate('rt.readingTools') . '</h5>');
00167 if ($index !== false && !empty($sidebarAdHtml)) {
00168 $newOutput = substr($output,0,$index);
00169 $newOutput .= $sidebarAdHtml;
00170 $newOutput .= substr($output, $index);
00171 $output =& $newOutput;
00172 }
00173 $smarty->unregister_outputfilter('sidebarOutputFilter');
00174 return $output;
00175 }
00176
00180 function getManagementVerbs() {
00181 $verbs = array();
00182 if ($this->getEnabled()) {
00183 $verbs[] = array(
00184 'disable',
00185 Locale::translate('manager.plugins.disable')
00186 );
00187 if ($this->isConfigured()) $verbs[] = array(
00188 'settings',
00189 Locale::translate('plugins.generic.openads.manager.settings')
00190 );
00191 } else {
00192 if ($this->isConfigured()) $verbs[] = array(
00193 'enable',
00194 Locale::translate('manager.plugins.enable')
00195 );
00196 }
00197 return $verbs;
00198 }
00199
00203 function getEnabled() {
00204 $journal =& Request::getJournal();
00205 if (!$journal) return false;
00206 return $this->getSetting($journal->getJournalId(), 'enabled');
00207 }
00208
00212 function setEnabled($enabled) {
00213 $journal =& Request::getJournal();
00214 if ($journal) {
00215 $this->updateSetting($journal->getJournalId(), 'enabled', $enabled ? true : false);
00216 return true;
00217 }
00218 return false;
00219 }
00220
00224 function manage($verb, $args) {
00225 $templateMgr = &TemplateManager::getManager();
00226 $journal = &Request::getJournal();
00227 $returner = false;
00228
00229 switch ($verb) {
00230 case 'enable': $this->setEnabled(true); break;
00231 case 'disable': $this->setEnabled(false); break;
00232 case 'settings':
00233 $this->import('OpenAdsSettingsForm');
00234 $this->import('OpenAdsConnection');
00235 $openAdsConnection =& new OpenAdsConnection($this, $this->getInstallationPath());
00236 $openAdsConnection->loadConfig();
00237 $form =& new OpenAdsSettingsForm($this, $openAdsConnection, $journal->getJournalId());
00238 if (array_shift($args) == 'save') {
00239 $form->readInputData();
00240 $form->execute();
00241 Request::redirect(null, 'manager', 'plugins');
00242 } else {
00243 $form->initData();
00244 $form->display();
00245 $returner = true;
00246 }
00247 }
00248 return $returner;
00249 }
00250
00255 function isConfigured() {
00256 $this->import('OpenAdsConnection');
00257 $config =& new OpenAdsConnection($this, $this->getInstallationPath());
00258 return $config->isConfigured();
00259 }
00260
00265 function getInstallationPath() {
00266 return Config::getVar('open_ads', 'install_path');
00267 }
00268 }
00269
00270 ?>