PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/system/roksprocket/roksprocket.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 582 lines | 361 code | 71 blank | 150 comment | 88 complexity | 35d7be6c02946cf910f125451dba0ffd MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. defined('_JEXEC') or die('Restricted access');
  9. jimport('joomla.plugin.plugin');
  10. /**
  11. */
  12. class plgSystemRokSprocket extends JPlugin
  13. {
  14. const NEEDED_ROKCOMMON_VERSION = '3.1.6';
  15. /**
  16. *
  17. */
  18. const MODULE_NAME = 'mod_roksprocket';
  19. /**
  20. *
  21. */
  22. const COMPONENT_NAME = 'com_roksprocket';
  23. /**
  24. * @return mixed
  25. */
  26. public function onAfterInitialise()
  27. {
  28. $app = JFactory::getApplication();
  29. $db = JFactory::getDBO();
  30. if (!defined('ROKCOMMON') || !defined('ROKCOMMON_PLUGIN_LOADED')) {
  31. $error_string = 'RokSprocket needs the RokCommon Library and Plug-in installed and enabled. The RokCommon System Plug-in needs to be before the RokSprocket System Plug-in in the Plug-in Manager';
  32. } else if (!preg_match('/project.version/', ROKCOMMON) && version_compare(preg_replace('/-SNAPSHOT/', '', ROKCOMMON), self::NEEDED_ROKCOMMON_VERSION, '<')) {
  33. $error_string = sprintf('RokSprocket needs at least RokCommon Version %s. You currently have RokCommon Version %s', self::NEEDED_ROKCOMMON_VERSION, ROKCOMMON);
  34. }
  35. // check for newer rokcommons version
  36. /** @var $extensiontable JTableExtension */
  37. $extensiontable = JTable::getInstance('Extension');
  38. $ext_id = $extensiontable->find(array(
  39. 'type' => 'component',
  40. 'element'=> 'com_roksprocket'
  41. ));
  42. if ($ext_id == null) {
  43. $error_string = 'The RokSprocket Module needs the RokSprocket Component installed.';
  44. } else {
  45. $extensiontable->load($ext_id);
  46. if (!(bool)$extensiontable->enabled) {
  47. $error_string = 'The RokSprocket Module needs the RokSprocket Component enabled.';
  48. }
  49. }
  50. if (!empty($error_string)) {
  51. if (JError::$legacy) {
  52. return JError::raiseWarning(500, $error_string);
  53. } else {
  54. throw new Exception($error_string);
  55. }
  56. }
  57. $mod_id = $extensiontable->find(array(
  58. 'type' => 'module',
  59. 'element'=> 'mod_roksprocket'
  60. ));
  61. if ($ext_id == null) {
  62. return;
  63. } else {
  64. $extensiontable->load($ext_id);
  65. if (!(bool)$extensiontable->enabled) {
  66. return;
  67. }
  68. }
  69. define('ROKSPROCKET', '1.8.9');
  70. define('ROKSPROCKET_VERSION', '1.8.9');
  71. $container = RokCommon_Service::getContainer();
  72. // load each providers container file
  73. $this->loadProviders($container);
  74. $this->loadLayouts($container);
  75. $this->loadAddons($container);
  76. if (!$app->isAdmin()) return;
  77. if (array_key_exists('option', $_REQUEST) && array_key_exists('task', $_REQUEST)) {
  78. $input = $app->input;
  79. $option = $input->getWord('option');
  80. $task = $input->getCmd('task');
  81. $eid = $input->getInt('eid');
  82. if ($option == 'com_modules' || $option == 'com_advancedmodules') {
  83. switch ($task) {
  84. case 'module.add':
  85. $extensionId = (int)$app->getUserState('com_modules.add.module.extension_id', null);
  86. if (null == $extensionId && $eid != null) {
  87. $extensionId = $eid;
  88. }
  89. $query = $db->getQuery(true);
  90. $query->select('element, client_id');
  91. $query->from('#__extensions');
  92. $query->where('extension_id = ' . $extensionId);
  93. $query->where('type = ' . $db->quote('module'));
  94. $query->where('element = ' . $db->quote(self::MODULE_NAME));
  95. $db->setQuery($query);
  96. $extension = $db->loadObject();
  97. if (!empty($extension)) {
  98. $input->set('option', self::COMPONENT_NAME);
  99. JFactory::getApplication()->input->set('option', self::COMPONENT_NAME, 'GET', true);
  100. if (version_compare(JVERSION, '3.0', '<')) {
  101. JRequest::setVar('option', self::COMPONENT_NAME, 'GET', true);
  102. }
  103. }
  104. break;
  105. case 'module.edit':
  106. $module_id = $input->get('id', null, 'int');
  107. if ($this->isRokSprocketModule($module_id)) {
  108. $input->set('option', self::COMPONENT_NAME);
  109. JFactory::getApplication()->input->set('option', self::COMPONENT_NAME, 'GET', true);
  110. if (version_compare(JVERSION, '3.0', '<')) {
  111. JRequest::setVar('option', self::COMPONENT_NAME, 'GET', true);
  112. } }
  113. break;
  114. case 'modules.duplicate':
  115. $input->set('option', self::COMPONENT_NAME);
  116. JFactory::getApplication()->input->set('option', self::COMPONENT_NAME, 'GET', true);
  117. $input->set('task','module.duplicate');
  118. JFactory::getApplication()->input->set('task', 'module.duplicate', 'POST', true);
  119. if (version_compare(JVERSION, '3.0', '<')) {
  120. JRequest::setVar('option', self::COMPONENT_NAME, 'GET', true);
  121. JRequest::setVar('task', 'module.duplicate', 'POST', true);
  122. }
  123. break;
  124. }
  125. $session = JFactory::getSession();
  126. $session->set('com_roksprocket.redirected.from', $option);
  127. }
  128. }
  129. }
  130. /**
  131. * @param RokCommon_Service_Container $container
  132. */
  133. protected function loadProviders(RokCommon_Service_Container &$container)
  134. {
  135. RokCommon_Composite::addPackagePath('roksprocket_providers', $container['roksprocket.providers.path']);
  136. $jversion = new JVersion();
  137. if (version_compare($jversion->getShortVersion(), '3.1.0_beta3', '>=')){
  138. RokCommon_Composite::addPackagePath('roksprocket_providers', $container['roksprocket.providers.platforms_path'].'/joomla31',11);
  139. }
  140. $context = RokCommon_Composite::get('roksprocket_providers');
  141. $priority_files = $context->getAllSubFiles($container['roksprocket.providers.file']);
  142. ksort($priority_files, true);
  143. foreach ($priority_files as $priority => $files) {
  144. foreach ($files as $file) {
  145. RokCommon_Service::addConfigFile($file);
  146. }
  147. }
  148. }
  149. /**
  150. * @param RokCommon_Service_Container $container
  151. */
  152. protected function loadLayouts(RokCommon_Service_Container &$container)
  153. {
  154. /** @var $platforminfo RokCommon_PlatformInfo */
  155. $platforminfo = $container->platforminfo;
  156. RokCommon_Composite::addPackagePath('roksprocket_layouts', JPATH_SITE . '/components/com_roksprocket/layouts', 10);
  157. RokCommon_Composite::addPackagePath('roksprocket_layouts', $container['roksprocket.template.override.path'] . '/layouts', 20);
  158. JForm::addFieldPath($container['template.path'] . '/admin/forms/fields');
  159. $context = RokCommon_Composite::get('roksprocket_layouts');
  160. $priority_files = $context->getAllSubFiles('meta.xml');
  161. ksort($priority_files, true);
  162. foreach ($priority_files as $priority => $files) {
  163. foreach ($files as $file) {
  164. RokCommon_Service::addConfigFile($file);
  165. }
  166. }
  167. }
  168. protected function loadAddons(RokCommon_Service_Container &$container)
  169. {
  170. foreach ($container['roksprocket.addons'] as $service) {
  171. $instance = $container->$service;
  172. }
  173. }
  174. /**
  175. * @param $id
  176. *
  177. * @return bool
  178. */
  179. protected function isRokSprocketModule($id)
  180. {
  181. /** @var $table JTableModule */
  182. $table = JTable::getInstance('Module', 'JTable', array());
  183. if (!$table->load($id) || $table->get('module') !== self::MODULE_NAME) {
  184. return false;
  185. }
  186. return true;
  187. }
  188. /**
  189. * @return mixed
  190. */
  191. public function onBeforeRender()
  192. {
  193. if (!defined('ROKSPROCKET')) return;
  194. $app = JFactory::getApplication();
  195. $document = JFactory::getDocument();
  196. $doctype = $document->getType();
  197. if (!$app->isAdmin()) return;
  198. $option = JFactory::getApplication()->input->getWord('option', '');
  199. $view = JFactory::getApplication()->input->getWord('view', '');
  200. if ($doctype == 'html' && ($option == 'com_modules' || $option == 'com_advancedmodules') && (($view == 'modules') || empty($view))) {
  201. $css = "
  202. .sprocket > a {vertical-align:middle;}
  203. .badge {margin:1px 5px;color:#fff;padding:2px 6px;font-family:Helvetica,Arial,sans-serif;border-radius:4px;display:inline-block;vertical-align:middle;background-color: #666;}
  204. .provider {display:inline-block;width:16px;height:16px;vertical-align:middle;}
  205. span.amm-no-modal {padding-left: 19px;}
  206. ";
  207. $document->addStyleDeclaration($css);
  208. }
  209. }
  210. /**
  211. * @return mixed
  212. */
  213. public function onAfterRender()
  214. {
  215. if (!defined('ROKSPROCKET')) return;
  216. $app = JFactory::getApplication();
  217. if (!$app->isAdmin()) return;
  218. $option = JFactory::getApplication()->input->getWord('option', '');
  219. $view = JFactory::getApplication()->input->getWord('view', '');
  220. if (($option == 'com_modules' || $option == 'com_advancedmodules') && (($view == 'modules') || empty($view))) {
  221. $document = JFactory::getDocument();
  222. $doctype = $document->getType();
  223. if ($doctype == 'html') {
  224. $db = JFactory::getDBO();
  225. $query = $db->getQuery(true);
  226. $query->select('id, title, params');
  227. $query->from('#__modules');
  228. $query->where('module = ' . $db->quote(self::MODULE_NAME));
  229. $db->setQuery($query);
  230. $data = $db->loadObjectList();
  231. if (sizeof($data) > 0) {
  232. $body = JResponse::getBody();
  233. $pq = phpQuery::newDocument($body);
  234. foreach ($data as $sprocket) {
  235. $params = json_decode($sprocket->params);
  236. $layout = (!empty($params) && $params->layout) ? $params->layout : '';
  237. $provider = (!empty($params) && $params->provider) ? $params->provider : '';
  238. $jversion = new JVersion();
  239. if (version_compare($jversion->getShortVersion(), '3.0.0', '>')) {
  240. pq('td > input[value=' . $sprocket->id . ']')->parent()->parent()->find('td > div > a[href*=module.edit]')->parent()->addClass("sprocket")->find("> a:not(.btn)")->after('<span class="badge">' . ucfirst($layout) . '</span><div style="background:url(' . JURI::base(true) . '/components/com_roksprocket/assets/providers/' . $provider . '.png)" class="provider"></div>');
  241. //pq('td.sprocket')->find('a[href*="option=com_advancedmodules"][class="modal"]')->remove();
  242. pq('div.sprocket')->find('span.sprocket')->empty()->addClass('amm-no-modal')->removeClass('hasTip');
  243. } elseif (version_compare($jversion->getShortVersion(), '2.5.5', '>')) {
  244. pq('td > input[value=' . $sprocket->id . ']')->parent()->parent()->find('a[href*=module.edit]')->parent()->addClass("sprocket")->find("> a:not(.jgrid)")->after('<span class="badge">' . ucfirst($layout) . '</span><div style="background:url(' . JURI::base(true) . '/components/com_roksprocket/assets/providers/' . $provider . '.png)" class="provider"></div>');
  245. //pq('td.sprocket')->find('a[href*="option=com_advancedmodules"][class="modal"]')->remove();
  246. pq('div.sprocket')->find('span.sprocket')->empty()->addClass('amm-no-modal')->removeClass('hasTip');
  247. } else {
  248. pq('td > input[value=' . $sprocket->id . ']')->parent()->parent()->find('a[href*=module.edit]')->parent()->addClass("sprocket")->find("a:last")->after('<span class="badge">' . ucfirst($layout) . '</span><div style="background:url(' . JURI::base(true) . '/components/com_roksprocket/assets/providers/' . $provider . '.png)" class="provider"></div>');
  249. //pq('td.sprocket')->find('a[href*="option=com_advancedmodules"][class="modal"]')->remove();
  250. pq('td.sprocket')->find('span.sprocket')->empty()->addClass('amm-no-modal')->removeClass('hasTip');
  251. }
  252. }
  253. $body = $pq->getDocument()->htmlOuter();
  254. JResponse::setBody($body);
  255. }
  256. }
  257. }
  258. }
  259. /**
  260. * @param $form
  261. * @param $data
  262. *
  263. * @return mixed
  264. */
  265. public function onContentPrepareForm($form, $data)
  266. {
  267. if (!defined('ROKSPROCKET')) return;
  268. $app = JFactory::getApplication();
  269. if (!$app->isAdmin()) return;
  270. if (defined('ROKCOMMON')) {
  271. $jinput = JFactory::getApplication()->input;
  272. $option = $jinput->getWord('option');
  273. $layout = $jinput->getWord('layout');
  274. if (in_array($option, array(
  275. 'com_roksprocket'
  276. )) && $layout == 'edit' && $this->getModuleType($data)
  277. ) {
  278. $this->processModuleConfig($form, $data);
  279. }
  280. }
  281. }
  282. /**
  283. * @param $form
  284. * @param $data
  285. *
  286. * @return mixed
  287. */
  288. protected function processModuleConfig($form, $data)
  289. {
  290. // check the module to see if it has a rokconfig.xml
  291. $module_type = $this->getModuleType($data);
  292. $container = RokCommon_Service::getContainer();
  293. $options = new RokCommon_Options();
  294. $section = new RokCommon_Options_Section('roksprocket_module', 'module_config.xml');
  295. $section->addPath(JPATH_SITE . '/components/com_roksprocket/');
  296. $section->addPath($container['roksprocket.template.override.path']);
  297. $options->addSection($section);
  298. // load up the Providers
  299. foreach ($container['roksprocket.providers.registered'] as $type => $providerinfo) {
  300. $provider_class = $container[sprintf('roksprocket.providers.registered.%s.class', $type)];
  301. $available = call_user_func(array($provider_class, 'isAvailable'));
  302. if ($available) {
  303. $section = new RokCommon_Options_Section('provider_' . $type, $providerinfo->optionfile);
  304. $section->addPath($providerinfo->path);
  305. $options->addSection($section);
  306. }
  307. }
  308. // load up the layouts
  309. foreach ($container['roksprocket.layouts'] as $type => $layoutinfo) {
  310. $section = new RokCommon_Options_Section('layout_' . $type, $layoutinfo->options->file);
  311. foreach ($layoutinfo->paths as $layoutpath) {
  312. $section->addPath($layoutpath);
  313. }
  314. $options->addSection($section);
  315. }
  316. $container = RokCommon_Service::getContainer();
  317. $rcform = new RokCommon_Config_Form($form);
  318. $xml = $options->getJoinedXml();
  319. $jxml = new JXMLElement($xml->asXML());
  320. $fieldsets = $jxml->xpath('/config/fields[@name = "params"]/fieldset');
  321. foreach ($fieldsets as $fieldset) {
  322. $overwrite = ((string)$fieldset['overwrite'] == 'true') ? true : false;
  323. $rcform->load($fieldset, $overwrite, '/config');
  324. }
  325. JForm::addFieldPath(JPATH_SITE . '/components/com_roksprocket/fields');
  326. }
  327. /**
  328. * @param $data
  329. *
  330. * @return string
  331. */
  332. protected function getModuleType(&$data)
  333. {
  334. if (is_array($data) && isset($data['module'])) {
  335. return $data['module'];
  336. } elseif (is_array($data) && empty($data)) {
  337. $form = JFactory::getApplication()->input->get('jform', null, 'ARRAY');
  338. if (is_array($form) && array_key_exists('module', $form)) {
  339. return $form['module'];
  340. }
  341. }
  342. if (is_object($data) && method_exists($data, 'get')) {
  343. return $data->get('module');
  344. }
  345. return '';
  346. }
  347. public function onContentAfterDelete($context, $data)
  348. {
  349. if (!defined('ROKSPROCKET')) return;
  350. switch ($context) {
  351. case 'com_content.article':
  352. $provider = 'joomla';
  353. $id = $data->id;
  354. break;
  355. case 'com_zoo.item':
  356. $provider = 'zoo';
  357. $id = $data->id;
  358. break;
  359. default:
  360. return true;
  361. }
  362. $db = JFactory::getDBO();
  363. // delete old per module settings
  364. $query = $db->getQuery(true);
  365. $query->delete();
  366. $query->from('#__roksprocket_items');
  367. $query->where('provider = ' . $db->quote($provider));
  368. $query->where('provider_id = ' . $db->quote($id));
  369. $db->setQuery((string)$query);
  370. if (!$db->query()) {
  371. $this->setError($db->getErrorMsg());
  372. return false;
  373. }
  374. return true;
  375. }
  376. }
  377. $app = JFactory::getApplication();
  378. $input = $app->input;
  379. $format = $input->getWord('format');
  380. $option = $input->getWord('option');
  381. if (!$app->isAdmin() && $format == 'raw' && $option == 'com_roksprocket') {
  382. jimport('joomla.application.module.helper');
  383. jimport('joomla.utilities.utility');
  384. if (!class_exists('JDocumentRaw')) {
  385. /**
  386. * DocumentRAW class, provides an easy interface to parse and display raw output
  387. *
  388. * @package Joomla.Platform
  389. * @subpackage Document
  390. * @since 11.1
  391. */
  392. class JDocumentRaw extends JDocument
  393. {
  394. /**
  395. * Array of Header <link> tags
  396. *
  397. * @var array
  398. * @since 11.1
  399. */
  400. public $_links = array();
  401. /**
  402. * Array of custom tags
  403. *
  404. * @var array
  405. * @since 11.1
  406. */
  407. public $_custom = array();
  408. /**
  409. * Name of the template
  410. *
  411. * @var string
  412. * @since 11.1
  413. */
  414. public $template = null;
  415. /**
  416. * Base url
  417. *
  418. * @var string
  419. * @since 11.1
  420. */
  421. public $baseurl = null;
  422. /**
  423. * Array of template parameters
  424. *
  425. * @var array
  426. * @since 11.1
  427. */
  428. public $params = null;
  429. /**
  430. * File name
  431. *
  432. * @var array
  433. * @since 11.1
  434. */
  435. public $_file = null;
  436. /**
  437. * String holding parsed template
  438. *
  439. * @var string
  440. * @since 11.1
  441. */
  442. protected $_template = '';
  443. /**
  444. * Array of parsed template JDoc tags
  445. *
  446. * @var array
  447. * @since 11.1
  448. */
  449. protected $_template_tags = array();
  450. /**
  451. * Integer with caching setting
  452. *
  453. * @var integer
  454. * @since 11.1
  455. */
  456. protected $_caching = null;
  457. /**
  458. * Class constructor
  459. *
  460. * @param array $options Associative array of options
  461. *
  462. * @since 11.1
  463. */
  464. public function __construct($options = array())
  465. {
  466. parent::__construct($options);
  467. //set mime type
  468. $this->_mime = 'text/html';
  469. //set document type
  470. $this->_type = 'raw';
  471. }
  472. /**
  473. * Get the HTML document head data
  474. *
  475. * @return array The document head data in array form
  476. *
  477. * @since 11.1
  478. */
  479. public function getHeadData()
  480. {
  481. $data = array();
  482. $data['title'] = $this->title;
  483. $data['description'] = $this->description;
  484. $data['link'] = $this->link;
  485. $data['metaTags'] = $this->_metaTags;
  486. $data['links'] = $this->_links;
  487. $data['styleSheets'] = $this->_styleSheets;
  488. $data['style'] = $this->_style;
  489. $data['scripts'] = $this->_scripts;
  490. $data['script'] = $this->_script;
  491. $data['custom'] = $this->_custom;
  492. return $data;
  493. }
  494. /**
  495. * Render the document.
  496. *
  497. * @param boolean $cache If true, cache the output
  498. * @param array $params Associative array of attributes
  499. *
  500. * @return The rendered data
  501. *
  502. * @since 11.1
  503. */
  504. public function render($cache = false, $params = array())
  505. {
  506. parent::render();
  507. return $this->getBuffer();
  508. }
  509. }
  510. }
  511. }