PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_modules/controller.php

https://gitlab.com/endomorphosis/greenrenaissancejoomla
PHP | 815 lines | 561 code | 134 blank | 120 comment | 87 complexity | 9e776b2bd7adbec65fa1498673491055 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: controller.php 10094 2008-03-02 04:35:10Z instance $
  4. * @package Joomla
  5. * @subpackage Modules
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // no direct access
  15. defined( '_JEXEC' ) or die( 'Restricted access' );
  16. jimport( 'joomla.application.component.controller' );
  17. $client = JRequest::getVar('client', 0, '', 'int');
  18. if ($client == 1) {
  19. JSubMenuHelper::addEntry(JText::_('Site'), 'index.php?option=com_modules&client_id=0');
  20. JSubMenuHelper::addEntry(JText::_('Administrator'), 'index.php?option=com_modules&client=1', true );
  21. } else {
  22. JSubMenuHelper::addEntry(JText::_('Site'), 'index.php?option=com_modules&client_id=0', true );
  23. JSubMenuHelper::addEntry(JText::_('Administrator'), 'index.php?option=com_modules&client=1');
  24. }
  25. class ModulesController extends JController
  26. {
  27. /**
  28. * Constructor
  29. */
  30. function __construct( $config = array() )
  31. {
  32. parent::__construct( $config );
  33. // Register Extra tasks
  34. $this->registerTask( 'apply', 'save' );
  35. $this->registerTask( 'unpublish', 'publish' );
  36. $this->registerTask( 'orderup', 'reorder' );
  37. $this->registerTask( 'orderdown', 'reorder' );
  38. $this->registerTask( 'accesspublic', 'access' );
  39. $this->registerTask( 'accessregistered','access' );
  40. $this->registerTask( 'accessspecial', 'access' );
  41. }
  42. /**
  43. * Compiles a list of installed or defined modules
  44. */
  45. function view()
  46. {
  47. global $mainframe;
  48. // Initialize some variables
  49. $db =& JFactory::getDBO();
  50. $client =& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
  51. $option = 'com_modules';
  52. $filter_order = $mainframe->getUserStateFromRequest( $option.'filter_order', 'filter_order', 'm.position', 'cmd' );
  53. $filter_order_Dir = $mainframe->getUserStateFromRequest( $option.'filter_order_Dir', 'filter_order_Dir', '', 'word' );
  54. $filter_state = $mainframe->getUserStateFromRequest( $option.'filter_state', 'filter_state', '', 'word' );
  55. $filter_position = $mainframe->getUserStateFromRequest( $option.'filter_position', 'filter_position', '', 'cmd' );
  56. $filter_type = $mainframe->getUserStateFromRequest( $option.'filter_type', 'filter_type', '', 'cmd' );
  57. $filter_assigned = $mainframe->getUserStateFromRequest( $option.'filter_assigned', 'filter_assigned', '', 'cmd' );
  58. $search = $mainframe->getUserStateFromRequest( $option.'search', 'search', '', 'string' );
  59. $search = JString::strtolower( $search );
  60. $limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
  61. $limitstart = $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );
  62. $where[] = 'm.client_id = '.(int) $client->id;
  63. $joins[] = 'LEFT JOIN #__users AS u ON u.id = m.checked_out';
  64. $joins[] = 'LEFT JOIN #__groups AS g ON g.id = m.access';
  65. $joins[] = 'LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id';
  66. // used by filter
  67. if ( $filter_assigned ) {
  68. $joins[] = 'LEFT JOIN #__templates_menu AS t ON t.menuid = mm.menuid';
  69. $where[] = 't.template = '.$db->Quote($filter_assigned);
  70. }
  71. if ( $filter_position ) {
  72. $where[] = 'm.position = '.$db->Quote($filter_position);
  73. }
  74. if ( $filter_type ) {
  75. $where[] = 'm.module = '.$db->Quote($filter_type);
  76. }
  77. if ( $search ) {
  78. $where[] = 'LOWER( m.title ) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
  79. }
  80. if ( $filter_state ) {
  81. if ( $filter_state == 'P' ) {
  82. $where[] = 'm.published = 1';
  83. } else if ($filter_state == 'U' ) {
  84. $where[] = 'm.published = 0';
  85. }
  86. }
  87. $where = ' WHERE ' . implode( ' AND ', $where );
  88. $join = ' ' . implode( ' ', $joins );
  89. $orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', m.ordering ASC';
  90. // get the total number of records
  91. $query = 'SELECT COUNT(DISTINCT m.id)'
  92. . ' FROM #__modules AS m'
  93. . $join
  94. . $where
  95. ;
  96. $db->setQuery( $query );
  97. $total = $db->loadResult();
  98. jimport('joomla.html.pagination');
  99. $pageNav = new JPagination( $total, $limitstart, $limit );
  100. $query = 'SELECT m.*, u.name AS editor, g.name AS groupname, MIN(mm.menuid) AS pages'
  101. . ' FROM #__modules AS m'
  102. . $join
  103. . $where
  104. . ' GROUP BY m.id'
  105. . $orderby
  106. ;
  107. $db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
  108. $rows = $db->loadObjectList();
  109. if ($db->getErrorNum()) {
  110. echo $db->stderr();
  111. return false;
  112. }
  113. // get list of Positions for dropdown filter
  114. $query = 'SELECT m.position AS value, m.position AS text'
  115. . ' FROM #__modules as m'
  116. . ' WHERE m.client_id = '.(int) $client->id
  117. . ' GROUP BY m.position'
  118. . ' ORDER BY m.position'
  119. ;
  120. $positions[] = JHTML::_('select.option', '0', '- '. JText::_( 'Select Position' ) .' -' );
  121. $db->setQuery( $query );
  122. $positions = array_merge( $positions, $db->loadObjectList() );
  123. $lists['position'] = JHTML::_('select.genericlist', $positions, 'filter_position', 'class="inputbox" size="1" onchange="this.form.submit()"', 'value', 'text', "$filter_position" );
  124. // get list of Positions for dropdown filter
  125. $query = 'SELECT module AS value, module AS text'
  126. . ' FROM #__modules'
  127. . ' WHERE client_id = '.(int) $client->id
  128. . ' GROUP BY module'
  129. . ' ORDER BY module'
  130. ;
  131. $db->setQuery( $query );
  132. $types[] = JHTML::_('select.option', '0', '- '. JText::_( 'Select Type' ) .' -' );
  133. $types = array_merge( $types, $db->loadObjectList() );
  134. $lists['type'] = JHTML::_('select.genericlist', $types, 'filter_type', 'class="inputbox" size="1" onchange="this.form.submit()"', 'value', 'text', "$filter_type" );
  135. // state filter
  136. $lists['state'] = JHTML::_('grid.state', $filter_state );
  137. // template assignment filter
  138. $query = 'SELECT DISTINCT(template) AS text, template AS value'.
  139. ' FROM #__templates_menu' .
  140. ' WHERE client_id = '.(int) $client->id;
  141. $db->setQuery( $query );
  142. $assigned[] = JHTML::_('select.option', '0', '- '. JText::_( 'Select Template' ) .' -' );
  143. $assigned = array_merge( $assigned, $db->loadObjectList() );
  144. $lists['assigned'] = JHTML::_('select.genericlist', $assigned, 'filter_assigned', 'class="inputbox" size="1" onchange="this.form.submit()"', 'value', 'text', "$filter_assigned" );
  145. // table ordering
  146. $lists['order_Dir'] = $filter_order_Dir;
  147. $lists['order'] = $filter_order;
  148. // search filter
  149. $lists['search']= $search;
  150. require_once( JApplicationHelper::getPath( 'admin_html' ) );
  151. HTML_modules::view( $rows, $client, $pageNav, $lists );
  152. }
  153. /**
  154. * Compiles information to add or edit a module
  155. * @param string The current GET/POST option
  156. * @param integer The unique id of the record to edit
  157. */
  158. function copy()
  159. {
  160. // Check for request forgeries
  161. JRequest::checkToken() or jexit( 'Invalid Token' );
  162. // Initialize some variables
  163. $db =& JFactory::getDBO();
  164. $client =& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
  165. $this->setRedirect( 'index.php?option=com_modules&client='.$client->id );
  166. $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
  167. $n = count( $cid );
  168. if ($n == 0) {
  169. return JError::raiseWarning( 500, JText::_( 'No items selected' ) );
  170. }
  171. $row =& JTable::getInstance('module');
  172. $tuples = array();
  173. foreach ($cid as $id)
  174. {
  175. // load the row from the db table
  176. $row->load( (int) $id );
  177. $row->title = JText::sprintf( 'Copy of', $row->title );
  178. $row->id = 0;
  179. $row->iscore = 0;
  180. $row->published = 0;
  181. if (!$row->check()) {
  182. return JError::raiseWarning( 500, $row->getError() );
  183. }
  184. if (!$row->store()) {
  185. return JError::raiseWarning( 500, $row->getError() );
  186. }
  187. $row->checkin();
  188. $row->reorder( 'position='.$db->Quote( $row->position ).' AND client_id='.(int) $client->id );
  189. $query = 'SELECT menuid'
  190. . ' FROM #__modules_menu'
  191. . ' WHERE moduleid = '.(int) $cid[0]
  192. ;
  193. $db->setQuery( $query );
  194. $rows = $db->loadResultArray();
  195. foreach ($rows as $menuid) {
  196. $tuples[] = '('.(int) $row->id.','.(int) $menuid.')';
  197. }
  198. }
  199. if (!empty( $tuples ))
  200. {
  201. // Module-Menu Mapping: Do it in one query
  202. $query = 'INSERT INTO #__modules_menu (moduleid,menuid) VALUES '.implode( ',', $tuples );
  203. $db->setQuery( $query );
  204. if (!$db->query()) {
  205. return JError::raiseWarning( 500, $row->getError() );
  206. }
  207. }
  208. $msg = JText::sprintf( 'Items Copied', $n );
  209. $this->setRedirect( 'index.php?option=com_modules&client='. $client->id, $msg );
  210. }
  211. /**
  212. * Saves the module after an edit form submit
  213. */
  214. function save()
  215. {
  216. // Check for request forgeries
  217. JRequest::checkToken() or jexit( 'Invalid Token' );
  218. global $mainframe;
  219. $cache = & JFactory::getCache();
  220. $cache->clean( 'com_content' );
  221. // Initialize some variables
  222. $db =& JFactory::getDBO();
  223. $client =& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
  224. $this->setRedirect( 'index.php?option=com_modules&client='.$client->id );
  225. $post = JRequest::get( 'post' );
  226. // fix up special html fields
  227. $post['content'] = JRequest::getVar( 'content', '', 'post', 'string', JREQUEST_ALLOWRAW );
  228. $post['client_id'] = $client->id;
  229. $row =& JTable::getInstance('module');
  230. if (!$row->bind( $post, 'selections' )) {
  231. return JError::raiseWarning( 500, $row->getError() );
  232. }
  233. if (!$row->check()) {
  234. return JError::raiseWarning( 500, $row->getError() );
  235. }
  236. // if new item, order last in appropriate group
  237. if (!$row->id) {
  238. $where = 'position='.$db->Quote( $row->position ).' AND client_id='.(int) $client->id ;
  239. $row->ordering = $row->getNextOrder( $where );
  240. }
  241. if (!$row->store()) {
  242. return JError::raiseWarning( 500, $row->getError() );
  243. }
  244. $row->checkin();
  245. $menus = JRequest::getVar( 'menus', '', 'post', 'word' );
  246. $selections = JRequest::getVar( 'selections', array(), 'post', 'array' );
  247. JArrayHelper::toInteger($selections);
  248. // delete old module to menu item associations
  249. $query = 'DELETE FROM #__modules_menu'
  250. . ' WHERE moduleid = '.(int) $row->id
  251. ;
  252. $db->setQuery( $query );
  253. if (!$db->query()) {
  254. return JError::raiseWarning( 500, $row->getError() );
  255. }
  256. // check needed to stop a module being assigned to `All`
  257. // and other menu items resulting in a module being displayed twice
  258. if ( $menus == 'all' ) {
  259. // assign new module to `all` menu item associations
  260. $query = 'INSERT INTO #__modules_menu'
  261. . ' SET moduleid = '.(int) $row->id.' , menuid = 0'
  262. ;
  263. $db->setQuery( $query );
  264. if (!$db->query()) {
  265. return JError::raiseWarning( 500, $row->getError() );
  266. }
  267. }
  268. else
  269. {
  270. foreach ($selections as $menuid)
  271. {
  272. // this check for the blank spaces in the select box that have been added for cosmetic reasons
  273. if ( (int) $menuid >= 0 ) {
  274. // assign new module to menu item associations
  275. $query = 'INSERT INTO #__modules_menu'
  276. . ' SET moduleid = '.(int) $row->id .', menuid = '.(int) $menuid
  277. ;
  278. $db->setQuery( $query );
  279. if (!$db->query()) {
  280. return JError::raiseWarning( 500, $row->getError() );
  281. }
  282. }
  283. }
  284. }
  285. $this->setMessage( JText::_( 'Item saved' ) );
  286. switch ($this->getTask())
  287. {
  288. case 'apply':
  289. $this->setRedirect( 'index.php?option=com_modules&client='. $client->id .'&task=edit&id='. $row->id );
  290. break;
  291. }
  292. }
  293. /**
  294. * Compiles information to add or edit a module
  295. * @param string The current GET/POST option
  296. * @param integer The unique id of the record to edit
  297. */
  298. function edit( )
  299. {
  300. // Initialize some variables
  301. $db =& JFactory::getDBO();
  302. $user =& JFactory::getUser();
  303. $client =& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
  304. $module = JRequest::getVar( 'module', '', '', 'cmd' );
  305. $id = JRequest::getVar( 'id', 0, 'method', 'int' );
  306. $cid = JRequest::getVar( 'cid', array( $id ), 'method', 'array' );
  307. JArrayHelper::toInteger($cid, array(0));
  308. $model = &$this->getModel('module');
  309. $model->setState( 'id', $cid[0] );
  310. $model->setState( 'clientId', $client->id );
  311. $lists = array();
  312. $row =& JTable::getInstance('module');
  313. // load the row from the db table
  314. $row->load( (int) $cid[0] );
  315. // fail if checked out not by 'me'
  316. if ($row->isCheckedOut( $user->get('id') )) {
  317. $this->setRedirect( 'index.php?option=com_modules&client='.$client->id );
  318. return JError::raiseWarning( 500, JText::sprintf( 'DESCBEINGEDITTED', JText::_( 'The module' ), $row->title ) );
  319. }
  320. $row->content = htmlspecialchars( str_replace( '&amp;', '&', $row->content ), ENT_COMPAT, 'UTF-8' );
  321. if ( $cid[0] ) {
  322. $row->checkout( $user->get('id') );
  323. }
  324. // if a new record we must still prime the JTableModel object with a default
  325. // position and the order; also add an extra item to the order list to
  326. // place the 'new' record in last position if desired
  327. if ($cid[0] == 0) {
  328. $row->position = 'left';
  329. $row->showtitle = true;
  330. $row->published = 1;
  331. //$row->ordering = $l;
  332. $row->module = $module;
  333. }
  334. if ($client->id == 1)
  335. {
  336. $where = 'client_id = 1';
  337. $lists['client_id'] = 1;
  338. $path = 'mod1_xml';
  339. }
  340. else
  341. {
  342. $where = 'client_id = 0';
  343. $lists['client_id'] = 0;
  344. $path = 'mod0_xml';
  345. }
  346. $query = 'SELECT position, ordering, showtitle, title'
  347. . ' FROM #__modules'
  348. . ' WHERE '. $where
  349. . ' ORDER BY ordering'
  350. ;
  351. $db->setQuery( $query );
  352. if ( !($orders = $db->loadObjectList()) ) {
  353. echo $db->stderr();
  354. return false;
  355. }
  356. $orders2 = array();
  357. $l = 0;
  358. $r = 0;
  359. for ($i=0, $n=count( $orders ); $i < $n; $i++) {
  360. $ord = 0;
  361. if (array_key_exists( $orders[$i]->position, $orders2 )) {
  362. $ord =count( array_keys( $orders2[$orders[$i]->position] ) ) + 1;
  363. }
  364. $orders2[$orders[$i]->position][] = JHTML::_('select.option', $ord, $ord.'::'.addslashes( $orders[$i]->title ) );
  365. }
  366. // get selected pages for $lists['selections']
  367. if ( $cid[0] ) {
  368. $query = 'SELECT menuid AS value'
  369. . ' FROM #__modules_menu'
  370. . ' WHERE moduleid = '.(int) $row->id
  371. ;
  372. $db->setQuery( $query );
  373. $lookup = $db->loadObjectList();
  374. if (empty( $lookup )) {
  375. $lookup = array( JHTML::_('select.option', '-1' ) );
  376. $row->pages = 'none';
  377. } elseif (count($lookup) == 1 && $lookup[0]->value == 0) {
  378. $row->pages = 'all';
  379. } else {
  380. $row->pages = null;
  381. }
  382. } else {
  383. $lookup = array( JHTML::_('select.option', 0, JText::_( 'All' ) ) );
  384. $row->pages = 'all';
  385. }
  386. if ( $row->access == 99 || $row->client_id == 1 || $lists['client_id'] ) {
  387. $lists['access'] = 'Administrator';
  388. $lists['showtitle'] = 'N/A <input type="hidden" name="showtitle" value="1" />';
  389. $lists['selections'] = 'N/A';
  390. } else {
  391. if ( $client->id == '1' ) {
  392. $lists['access'] = 'N/A';
  393. $lists['selections'] = 'N/A';
  394. } else {
  395. $lists['access'] = JHTML::_('list.accesslevel', $row );
  396. $selections = JHTML::_('menu.linkoptions');
  397. $lists['selections'] = JHTML::_('select.genericlist', $selections, 'selections[]', 'class="inputbox" size="15" multiple="multiple"', 'value', 'text', $lookup, 'selections' );
  398. }
  399. $lists['showtitle'] = JHTML::_('select.booleanlist', 'showtitle', 'class="inputbox"', $row->showtitle );
  400. }
  401. // build the html select list for published
  402. $lists['published'] = JHTML::_('select.booleanlist', 'published', 'class="inputbox"', $row->published );
  403. $row->description = '';
  404. $lang =& JFactory::getLanguage();
  405. if ( $client->id != '1' ) {
  406. $lang->load( trim($row->module), JPATH_SITE );
  407. } else {
  408. $lang->load( trim($row->module) );
  409. }
  410. // xml file for module
  411. if ($row->module == 'custom') {
  412. $xmlfile = JApplicationHelper::getPath( $path, 'mod_custom' );
  413. } else {
  414. $xmlfile = JApplicationHelper::getPath( $path, $row->module );
  415. }
  416. $data = JApplicationHelper::parseXMLInstallFile($xmlfile);
  417. if ($data)
  418. {
  419. foreach($data as $key => $value) {
  420. $row->$key = $value;
  421. }
  422. }
  423. // get params definitions
  424. $params = new JParameter( $row->params, $xmlfile, 'module' );
  425. require_once( JApplicationHelper::getPath( 'admin_html' ) );
  426. HTML_modules::edit( $model, $row, $orders2, $lists, $params, $client );
  427. }
  428. /**
  429. * Displays a list to select the creation of a new module
  430. */
  431. function add()
  432. {
  433. global $mainframe;
  434. // Initialize some variables
  435. $modules = array();
  436. $client =& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
  437. // path to search for modules
  438. if ($client->id == '1') {
  439. $path = JPATH_ADMINISTRATOR.DS.'modules';
  440. $langbase = JPATH_ADMINISTRATOR;
  441. } else {
  442. $path = JPATH_ROOT.DS.'modules';
  443. $langbase = JPATH_ROOT;
  444. }
  445. jimport('joomla.filesystem.folder');
  446. $dirs = JFolder::folders( $path );
  447. $lang =& JFactory::getLanguage();
  448. foreach ($dirs as $dir)
  449. {
  450. if (substr( $dir, 0, 4 ) == 'mod_')
  451. {
  452. $files = JFolder::files( $path.DS.$dir, '^([_A-Za-z0-9]*)\.xml$' );
  453. $module = new stdClass;
  454. $module->file = $files[0];
  455. $module->module = str_replace( '.xml', '', $files[0] );
  456. $module->path = $path.DS.$dir;
  457. $modules[] = $module;
  458. $lang->load( $module->module, $langbase );
  459. }
  460. }
  461. require_once( JPATH_COMPONENT.DS.'helpers'.DS.'xml.php' );
  462. ModulesHelperXML::parseXMLModuleFile( $modules, $client );
  463. // sort array of objects alphabetically by name
  464. JArrayHelper::sortObjects( $modules, 'name' );
  465. require_once( JApplicationHelper::getPath( 'admin_html' ) );
  466. HTML_modules::add( $modules, $client );
  467. }
  468. /**
  469. * Deletes one or more modules
  470. *
  471. * Also deletes associated entries in the #__module_menu table.
  472. * @param array An array of unique category id numbers
  473. */
  474. function remove()
  475. {
  476. global $mainframe;
  477. // Check for request forgeries
  478. JRequest::checkToken() or jexit( 'Invalid Token' );
  479. // Initialize some variables
  480. $db =& JFactory::getDBO();
  481. $client =& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
  482. $this->setRedirect( 'index.php?option=com_modules&client='.$client->id );
  483. $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
  484. JArrayHelper::toInteger( $cid );
  485. if (empty( $cid )) {
  486. return JError::raiseWarning( 500, 'No items selected' );
  487. }
  488. $cids = implode( ',', $cid );
  489. // pasamio: Disabled this as it breaks the uninstall ability!
  490. /*$query = 'SELECT id, module, title, iscore, params'
  491. . ' FROM #__modules WHERE id IN ('.$cids.')'
  492. ;
  493. $db->setQuery( $query );
  494. if (!($rows = $db->loadObjectList())) {
  495. return JError::raiseError( 500, $db->getErrorMsg() );
  496. }*/
  497. // remove mappings first (lest we leave orphans)
  498. $query = 'DELETE FROM #__modules_menu'
  499. . ' WHERE moduleid IN ( '.$cids.' )'
  500. ;
  501. $db->setQuery( $query );
  502. if (!$db->query()) {
  503. return JError::raiseError( 500, $db->getErrorMsg() );
  504. }
  505. // remove module
  506. $query = 'DELETE FROM #__modules'
  507. . ' WHERE id IN ('.$cids.')'
  508. ;
  509. $db->setQuery( $query );
  510. if (!$db->query()) {
  511. return JError::raiseError( 500, $db->getErrorMsg() );
  512. }
  513. $this->setMessage( JText::sprintf( 'Items removed', count( $cid ) ) );
  514. }
  515. /**
  516. * Publishes or Unpublishes one or more modules
  517. */
  518. function publish()
  519. {
  520. global $mainframe;
  521. // Check for request forgeries
  522. JRequest::checkToken() or jexit( 'Invalid Token' );
  523. // Initialize some variables
  524. $db =& JFactory::getDBO();
  525. $user =& JFactory::getUser();
  526. $client =& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
  527. $this->setRedirect( 'index.php?option=com_modules&client='.$client->id );
  528. $cache = & JFactory::getCache();
  529. $cache->clean( 'com_content' );
  530. $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
  531. JArrayHelper::toInteger($cid);
  532. $task = $this->getTask();
  533. $publish = ($task == 'publish');
  534. if (empty( $cid )) {
  535. return JError::raiseWarning( 500, 'No items selected' );
  536. }
  537. $cids = implode( ',', $cid );
  538. $query = 'UPDATE #__modules'
  539. . ' SET published = ' . intval( $publish )
  540. . ' WHERE id IN ( '.$cids.' )'
  541. . ' AND ( checked_out = 0 OR ( checked_out = '.(int) $user->get('id').' ) )'
  542. ;
  543. $db->setQuery( $query );
  544. if (!$db->query()) {
  545. return JError::raiseWarning( 500, $db->getErrorMsg() );
  546. }
  547. if (count( $cid ) == 1) {
  548. $row =& JTable::getInstance('module');
  549. $row->checkin( $cid[0] );
  550. }
  551. }
  552. /**
  553. * Cancels an edit operation
  554. */
  555. function cancel()
  556. {
  557. global $mainframe;
  558. // Check for request forgeries
  559. JRequest::checkToken() or jexit( 'Invalid Token' );
  560. // Initialize some variables
  561. $db =& JFactory::getDBO();
  562. $client =& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
  563. $this->setRedirect( 'index.php?option=com_modules&client='.$client->id );
  564. $row =& JTable::getInstance('module');
  565. // ignore array elements
  566. $row->bind(JRequest::get('post'), 'selections params' );
  567. $row->checkin();
  568. }
  569. /**
  570. * Moves the order of a record
  571. */
  572. function reorder()
  573. {
  574. global $mainframe;
  575. // Check for request forgeries
  576. JRequest::checkToken() or jexit( 'Invalid Token' );
  577. // Initialize some variables
  578. $db =& JFactory::getDBO();
  579. $client =& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
  580. $this->setRedirect( 'index.php?option=com_modules&client='.$client->id );
  581. $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
  582. JArrayHelper::toInteger($cid);
  583. $task = $this->getTask();
  584. $inc = ($task == 'orderup' ? -1 : 1);
  585. if (empty( $cid )) {
  586. return JError::raiseWarning( 500, 'No items selected' );
  587. }
  588. $row =& JTable::getInstance('module');
  589. $row->load( (int) $cid[0] );
  590. $row->move( $inc, 'position = '.$db->Quote( $row->position ).' AND client_id='.(int) $client->id );
  591. }
  592. /**
  593. * Changes the access level of a record
  594. */
  595. function access()
  596. {
  597. global $mainframe;
  598. // Check for request forgeries
  599. JRequest::checkToken() or jexit( 'Invalid Token' );
  600. // Initialize some variables
  601. $db =& JFactory::getDBO();
  602. $client =& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
  603. $this->setRedirect( 'index.php?option=com_modules&client='.$client->id );
  604. $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
  605. JArrayHelper::toInteger($cid);
  606. $task = JRequest::getCmd( 'task' );
  607. if (empty( $cid )) {
  608. return JError::raiseWarning( 500, 'No items selected' );
  609. }
  610. switch ( $task )
  611. {
  612. case 'accesspublic':
  613. $access = 0;
  614. break;
  615. case 'accessregistered':
  616. $access = 1;
  617. break;
  618. case 'accessspecial':
  619. $access = 2;
  620. break;
  621. }
  622. $row =& JTable::getInstance('module');
  623. $row->load( (int) $cid[0] );
  624. $row->access = $access;
  625. if ( !$row->check() ) {
  626. JError::raiseWarning( 500, $row->getError() );
  627. }
  628. if ( !$row->store() ) {
  629. JError::raiseWarning( 500, $row->getError() );
  630. }
  631. }
  632. /**
  633. * Saves the orders of the supplied list
  634. */
  635. function saveOrder()
  636. {
  637. // Check for request forgeries
  638. JRequest::checkToken() or jexit( 'Invalid Token' );
  639. // Initialize some variables
  640. $db =& JFactory::getDBO();
  641. $client =& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
  642. $this->setRedirect( 'index.php?option=com_modules&client='.$client->id );
  643. $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
  644. JArrayHelper::toInteger($cid);
  645. if (empty( $cid )) {
  646. return JError::raiseWarning( 500, 'No items selected' );
  647. }
  648. $total = count( $cid );
  649. $row =& JTable::getInstance('module');
  650. $groupings = array();
  651. $order = JRequest::getVar( 'order', array(0), 'post', 'array' );
  652. JArrayHelper::toInteger($order);
  653. // update ordering values
  654. for ($i = 0; $i < $total; $i++)
  655. {
  656. $row->load( (int) $cid[$i] );
  657. // track postions
  658. $groupings[] = $row->position;
  659. if ($row->ordering != $order[$i])
  660. {
  661. $row->ordering = $order[$i];
  662. if (!$row->store()) {
  663. return JError::raiseWarning( 500, $db->getErrorMsg() );
  664. }
  665. }
  666. }
  667. // execute updateOrder for each parent group
  668. $groupings = array_unique( $groupings );
  669. foreach ($groupings as $group){
  670. $row->reorder('position = '.$db->Quote($group).' AND client_id = '.(int) $client->id);
  671. }
  672. $this->setMessage (JText::_( 'New ordering saved' ));
  673. }
  674. function preview()
  675. {
  676. $document =& JFactory::getDocument();
  677. $document->setTitle(JText::_('Module Preview'));
  678. require_once( JApplicationHelper::getPath( 'admin_html' ) );
  679. HTML_modules::preview( );
  680. }
  681. }