PageRenderTime 28ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_banners/controllers/banner.php

https://bitbucket.org/bekket/lviveurorent
PHP | 461 lines | 319 code | 78 blank | 64 comment | 45 complexity | ed558211f03388dd4dd5f98468aa16fa MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id: banner.php 19343 2010-11-03 18:12:02Z ian $
  4. * @package Joomla
  5. * @subpackage Banners
  6. * @copyright Copyright (C) 2005 - 2010 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. /**
  18. * @package Joomla
  19. * @subpackage Banners
  20. */
  21. class BannerControllerBanner extends JController
  22. {
  23. /**
  24. * Constructor
  25. */
  26. function __construct( $config = array() )
  27. {
  28. parent::__construct( $config );
  29. // Register Extra tasks
  30. $this->registerTask( 'add', 'edit' );
  31. $this->registerTask( 'apply', 'save' );
  32. $this->registerTask( 'resethits', 'save' );
  33. $this->registerTask( 'unpublish', 'publish' );
  34. }
  35. /**
  36. * Display the list of banners
  37. */
  38. function display()
  39. {
  40. global $mainframe;
  41. $db =& JFactory::getDBO();
  42. $context = 'com_banners.banner.list.';
  43. $filter_order = $mainframe->getUserStateFromRequest( $context.'filter_order', 'filter_order', 'cc.title', 'cmd' );
  44. $filter_order_Dir = $mainframe->getUserStateFromRequest( $context.'filter_order_Dir', 'filter_order_Dir', '', 'word' );
  45. $filter_catid = $mainframe->getUserStateFromRequest( $context.'filter_catid', 'filter_catid', '', 'int' );
  46. $filter_state = $mainframe->getUserStateFromRequest( $context.'filter_state', 'filter_state', '', 'word' );
  47. $search = $mainframe->getUserStateFromRequest( $context.'search', 'search', '', 'string' );
  48. if (strpos($search, '"') !== false) {
  49. $search = str_replace(array('=', '<'), '', $search);
  50. }
  51. $search = JString::strtolower($search);
  52. $limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
  53. $limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' );
  54. $where = array();
  55. if ( $filter_state )
  56. {
  57. if ( $filter_state == 'P' ) {
  58. $where[] = 'b.showBanner = 1';
  59. }
  60. else if ($filter_state == 'U' ) {
  61. $where[] = 'b.showBanner = 0';
  62. }
  63. }
  64. if ($filter_catid) {
  65. $where[] = 'cc.id = ' . (int) $filter_catid;
  66. }
  67. if ($search) {
  68. $where[] = 'LOWER(b.name) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
  69. }
  70. $where = count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';
  71. // sanitize $filter_order
  72. if (!in_array($filter_order, array('b.name', 'c.name', 'cc.title', 'b.showBanner', 'b.ordering', 'b.Sticky', 'b.impmade', 'b.clicks', 'b.bid'))) {
  73. $filter_order = 'cc.title';
  74. }
  75. if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC'))) {
  76. $filter_order_Dir = '';
  77. }
  78. $orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', b.ordering';
  79. // get the total number of records
  80. $query = 'SELECT COUNT(*)'
  81. . ' FROM #__banner AS b'
  82. . ' LEFT JOIN #__categories AS cc ON cc.id = b.catid'
  83. . $where
  84. ;
  85. $db->setQuery( $query );
  86. $total = $db->loadResult();
  87. jimport('joomla.html.pagination');
  88. $pageNav = new JPagination( $total, $limitstart, $limit );
  89. $query = 'SELECT b.*, c.name AS client_name, cc.title AS category_name, u.name AS editor'
  90. . ' FROM #__banner AS b'
  91. . ' INNER JOIN #__bannerclient AS c ON c.cid = b.cid'
  92. . ' LEFT JOIN #__categories AS cc ON cc.id = b.catid'
  93. . ' LEFT JOIN #__users AS u ON u.id = b.checked_out'
  94. . $where
  95. . $orderby
  96. ;
  97. $db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
  98. $rows = $db->loadObjectList();
  99. // build list of categories
  100. $javascript = 'onchange="document.adminForm.submit();"';
  101. $lists['catid'] = JHTML::_('list.category', 'filter_catid', 'com_banner', (int) $filter_catid, $javascript );
  102. // state filter
  103. $lists['state'] = JHTML::_('grid.state', $filter_state );
  104. // table ordering
  105. $lists['order_Dir'] = $filter_order_Dir;
  106. $lists['order'] = $filter_order;
  107. // search filter
  108. $lists['search']= $search;
  109. require_once(JPATH_COMPONENT.DS.'views'.DS.'banner.php');
  110. BannersViewBanner::banners( $rows, $pageNav, $lists );
  111. }
  112. function edit()
  113. {
  114. $db =& JFactory::getDBO();
  115. $user =& JFactory::getUser();
  116. if ($this->_task == 'edit') {
  117. $cid = JRequest::getVar('cid', array(0), 'method', 'array');
  118. $cid = array((int) $cid[0]);
  119. } else {
  120. $cid = array( 0 );
  121. }
  122. $option = JRequest::getCmd('option');
  123. $lists = array();
  124. $row =& JTable::getInstance('banner', 'Table');
  125. $row->load( $cid[0] );
  126. if ($cid[0]) {
  127. $row->checkout( $user->get('id') );
  128. } else {
  129. $row->showBanner = 1;
  130. }
  131. // Build Client select list
  132. $sql = 'SELECT cid, name'
  133. . ' FROM #__bannerclient'
  134. ;
  135. $db->setQuery($sql);
  136. if (!$db->query())
  137. {
  138. $this->setRedirect( 'index.php?option=com_banners' );
  139. return JError::raiseWarning( 500, $db->getErrorMsg() );
  140. }
  141. $banner_params = new JParameter( $row->params );
  142. $lists['width'] = $banner_params->get( 'width');
  143. $lists['height'] = $banner_params->get( 'height');
  144. $clientlist[] = JHTML::_('select.option', '0', JText::_( 'Select Client' ), 'cid', 'name' );
  145. $clientlist = array_merge( $clientlist, $db->loadObjectList() );
  146. $lists['cid'] = JHTML::_('select.genericlist', $clientlist, 'cid', 'class="inputbox" size="1"','cid', 'name', $row->cid );
  147. // Imagelist
  148. $javascript = 'onchange="changeDisplayImage();"';
  149. $directory = '/images/banners';
  150. $lists['imageurl'] = JHTML::_('list.images', 'imageurl', $row->imageurl, $javascript, $directory, "bmp|gif|jpg|png|swf" );
  151. // build list of categories
  152. $lists['catid'] = JHTML::_('list.category', 'catid', 'com_banner', intval( $row->catid ) );
  153. // sticky
  154. $lists['sticky'] = JHTML::_('select.booleanlist', 'sticky', 'class="inputbox"', $row->sticky );
  155. // published
  156. $lists['showBanner'] = JHTML::_('select.booleanlist', 'showBanner', '', $row->showBanner );
  157. require_once(JPATH_COMPONENT.DS.'views'.DS.'banner.php');
  158. BannersViewBanner::banner( $row, $lists );
  159. }
  160. /**
  161. * Save method
  162. */
  163. function save()
  164. {
  165. global $mainframe;
  166. // Check for request forgeries
  167. JRequest::checkToken() or jexit( 'Invalid Token' );
  168. $this->setRedirect( 'index.php?option=com_banners' );
  169. // Initialize variables
  170. $db =& JFactory::getDBO();
  171. $post = JRequest::get( 'post' );
  172. // fix up special html fields
  173. $post['custombannercode'] = JRequest::getVar( 'custombannercode', '', 'post', 'string', JREQUEST_ALLOWRAW );
  174. $row =& JTable::getInstance('banner', 'Table');
  175. // Save params temp fix
  176. $temp1 = array();
  177. $temp2 = array();
  178. $temp1['width'] = (int) $post['width'];
  179. $temp1['height'] = (int) $post['height'];
  180. foreach ($temp1 as $k => $v)
  181. {
  182. if ( $k && strlen($v) )
  183. {
  184. $temp2[] = $k.'='.$v;
  185. }
  186. }
  187. $row->params = implode( "\n", $temp2 );
  188. if (!$row->bind( $post )) {
  189. return JError::raiseWarning( 500, $row->getError() );
  190. }
  191. // Resets clicks when `Reset Clicks` button is used instead of `Save` button
  192. $task = JRequest::getCmd( 'task' );
  193. if ( $task == 'resethits' )
  194. {
  195. $row->clicks = 0;
  196. $msg = JText::_( 'Reset Banner clicks' );
  197. }
  198. // Sets impressions to unlimited when `unlimited` checkbox ticked
  199. $unlimited = JRequest::getBool('unlimited');
  200. if ($unlimited) {
  201. $row->imptotal = 0;
  202. }
  203. if (!$row->check()) {
  204. return JError::raiseWarning( 500, $row->getError() );
  205. }
  206. // if new item order last in appropriate group
  207. if (!$row->bid)
  208. {
  209. $where = 'catid = '.(int) $row->catid;
  210. $row->ordering = $row->getNextOrder( $where );
  211. }
  212. if (!$row->store()) {
  213. return JError::raiseWarning( 500, $row->getError() );
  214. }
  215. $row->checkin();
  216. switch ($task)
  217. {
  218. case 'apply':
  219. $link = 'index.php?option=com_banners&task=edit&cid[]='. $row->bid ;
  220. break;
  221. case 'save':
  222. default:
  223. $link = 'index.php?option=com_banners';
  224. break;
  225. }
  226. $this->setRedirect( $link, JText::_( 'Item Saved' ) );
  227. }
  228. function cancel()
  229. {
  230. // Check for request forgeries
  231. JRequest::checkToken() or jexit( 'Invalid Token' );
  232. $this->setRedirect( 'index.php?option=com_banners' );
  233. // Initialize variables
  234. $db =& JFactory::getDBO();
  235. $post = JRequest::get( 'post' );
  236. $row =& JTable::getInstance('banner', 'Table');
  237. $row->bind( $post );
  238. $row->checkin();
  239. }
  240. /**
  241. * Copies one or more banners
  242. */
  243. function copy()
  244. {
  245. // Check for request forgeries
  246. JRequest::checkToken() or jexit( 'Invalid Token' );
  247. $this->setRedirect( 'index.php?option=com_banners' );
  248. $cid = JRequest::getVar( 'cid', null, 'post', 'array' );
  249. $db =& JFactory::getDBO();
  250. $table =& JTable::getInstance('banner', 'Table');
  251. $user = &JFactory::getUser();
  252. $n = count( $cid );
  253. if ($n > 0)
  254. {
  255. foreach ($cid as $id)
  256. {
  257. if ($table->load( (int)$id ))
  258. {
  259. $table->bid = 0;
  260. $table->name = 'Copy of ' . $table->name;
  261. $table->impmade = 0;
  262. $table->clicks = 0;
  263. $table->showBanner = 0;
  264. $table->date = $db->getNullDate();
  265. if (!$table->store()) {
  266. return JError::raiseWarning( $table->getError() );
  267. }
  268. }
  269. else {
  270. return JError::raiseWarning( 500, $table->getError() );
  271. }
  272. }
  273. }
  274. else {
  275. return JError::raiseWarning( 500, JText::_( 'No items selected' ) );
  276. }
  277. $this->setMessage( JText::sprintf( 'Items copied', $n ) );
  278. }
  279. function publish()
  280. {
  281. // Check for request forgeries
  282. JRequest::checkToken() or jexit( 'Invalid Token' );
  283. $this->setRedirect( 'index.php?option=com_banners' );
  284. // Initialize variables
  285. $db =& JFactory::getDBO();
  286. $user =& JFactory::getUser();
  287. $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
  288. $task = JRequest::getCmd( 'task' );
  289. $publish = ($task == 'publish');
  290. $n = count( $cid );
  291. if (empty( $cid )) {
  292. return JError::raiseWarning( 500, JText::_( 'No items selected' ) );
  293. }
  294. JArrayHelper::toInteger( $cid );
  295. $cids = implode( ',', $cid );
  296. $query = 'UPDATE #__banner'
  297. . ' SET showBanner = ' . (int) $publish
  298. . ' WHERE bid IN ( '. $cids.' )'
  299. . ' AND ( checked_out = 0 OR ( checked_out = ' .(int) $user->get('id'). ' ) )'
  300. ;
  301. $db->setQuery( $query );
  302. if (!$db->query()) {
  303. return JError::raiseWarning( 500, $db->getError() );
  304. }
  305. $this->setMessage( JText::sprintf( $publish ? 'Items published' : 'Items unpublished', $n ) );
  306. }
  307. function remove()
  308. {
  309. // Check for request forgeries
  310. JRequest::checkToken() or jexit( 'Invalid Token' );
  311. $this->setRedirect( 'index.php?option=com_banners' );
  312. // Initialize variables
  313. $db =& JFactory::getDBO();
  314. $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
  315. $n = count( $cid );
  316. JArrayHelper::toInteger( $cid );
  317. if ($n)
  318. {
  319. $query = 'DELETE FROM #__banner'
  320. . ' WHERE bid = ' . implode( ' OR bid = ', $cid )
  321. ;
  322. $db->setQuery( $query );
  323. if (!$db->query()) {
  324. JError::raiseWarning( 500, $db->getError() );
  325. }
  326. }
  327. $this->setMessage( JText::sprintf( 'Items removed', $n ) );
  328. }
  329. /**
  330. * Save the new order given by user
  331. */
  332. function saveOrder()
  333. {
  334. // Check for request forgeries
  335. JRequest::checkToken() or jexit( 'Invalid Token' );
  336. $this->setRedirect( 'index.php?option=com_banners' );
  337. // Initialize variables
  338. $db =& JFactory::getDBO();
  339. $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
  340. $order = JRequest::getVar( 'order', array(), 'post', 'array' );
  341. $row =& JTable::getInstance('banner', 'Table');
  342. $total = count( $cid );
  343. $conditions = array();
  344. if (empty( $cid )) {
  345. return JError::raiseWarning( 500, JText::_( 'No items selected' ) );
  346. }
  347. // update ordering values
  348. for ($i = 0; $i < $total; $i++)
  349. {
  350. $row->load( (int) $cid[$i] );
  351. if ($row->ordering != $order[$i])
  352. {
  353. $row->ordering = $order[$i];
  354. if (!$row->store()) {
  355. return JError::raiseError( 500, $db->getErrorMsg() );
  356. }
  357. // remember to reorder this category
  358. $condition = 'catid = '.(int) $row->catid;
  359. $found = false;
  360. foreach ($conditions as $cond) {
  361. if ($cond[1] == $condition)
  362. {
  363. $found = true;
  364. break;
  365. }
  366. }
  367. if (!$found) {
  368. $conditions[] = array ( $row->bid, $condition );
  369. }
  370. }
  371. }
  372. // execute reorder for each category
  373. foreach ($conditions as $cond)
  374. {
  375. $row->load( $cond[0] );
  376. $row->reorder( $cond[1] );
  377. }
  378. // Clear the component's cache
  379. $cache =& JFactory::getCache('com_banners');
  380. $cache->clean();
  381. $this->setMessage( JText::_('New ordering saved') );
  382. }
  383. }