PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_banners/models/tracks.php

https://github.com/rietn/minima
PHP | 461 lines | 282 code | 74 blank | 105 comment | 44 complexity | f9bbd36d8281618923759b7b14707e65 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: tracks.php 20267 2011-01-11 03:44:44Z eddieajau $
  4. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. defined('_JEXEC') or die;
  8. jimport('joomla.application.component.modellist');
  9. /**
  10. * Methods supporting a list of tracks.
  11. *
  12. * @package Joomla.Administrator
  13. * @subpackage com_banners
  14. * @since 1.6
  15. */
  16. class BannersModelTracks extends JModelList
  17. {
  18. /**
  19. * Constructor.
  20. *
  21. * @param array An optional associative array of configuration settings.
  22. * @see JController
  23. * @since 1.6
  24. */
  25. public function __construct($config = array())
  26. {
  27. if (empty($config['filter_fields'])) {
  28. $config['filter_fields'] = array(
  29. 'name', 'b.name',
  30. 'cl.name', 'client_name',
  31. 'cat.title', 'category_title',
  32. 'track_type', 'a.track_type',
  33. 'count', 'a.count',
  34. 'track_date', 'a.track_date',
  35. );
  36. }
  37. parent::__construct($config);
  38. }
  39. /**
  40. * @since 1.6
  41. */
  42. protected $basename;
  43. /**
  44. * Method to auto-populate the model state.
  45. *
  46. * Note. Calling getState in this method will result in recursion.
  47. *
  48. * @since 1.6
  49. */
  50. protected function populateState($ordering = null, $direction = null)
  51. {
  52. // Initialise variables.
  53. $app = JFactory::getApplication('administrator');
  54. // Load the filter state.
  55. $type = $this->getUserStateFromRequest($this->context.'.filter.type', 'filter_type');
  56. $this->setState('filter.type', $type);
  57. $begin = $this->getUserStateFromRequest($this->context.'.filter.begin', 'filter_begin', '', 'string');
  58. $this->setState('filter.begin', $begin);
  59. $end = $this->getUserStateFromRequest($this->context.'.filter.end', 'filter_end', '', 'string');
  60. $this->setState('filter.end', $end);
  61. $categoryId = $this->getUserStateFromRequest($this->context.'.filter.category_id', 'filter_category_id', '');
  62. $this->setState('filter.category_id', $categoryId);
  63. $clientId = $this->getUserStateFromRequest($this->context.'.filter.client_id', 'filter_client_id', '');
  64. $this->setState('filter.client_id', $clientId);
  65. // Load the parameters.
  66. $params = JComponentHelper::getParams('com_banners');
  67. $this->setState('params', $params);
  68. // List state information.
  69. parent::populateState('name', 'asc');
  70. }
  71. /**
  72. * Build an SQL query to load the list data.
  73. *
  74. * @return JDatabaseQuery
  75. * @since 1.6
  76. */
  77. protected function getListQuery()
  78. {
  79. // Get the application object
  80. $app = JFactory::getApplication();
  81. require_once JPATH_COMPONENT.'/helpers/banners.php';
  82. // Create a new query object.
  83. $db = $this->getDbo();
  84. $query = $db->getQuery(true);
  85. // Select the required fields from the table.
  86. $query->select(
  87. 'a.track_date as track_date,'.
  88. 'a.track_type as track_type,'.
  89. 'a.`count` as `count`'
  90. );
  91. $query->from('`#__banner_tracks` AS a');
  92. // Join with the banners
  93. $query->join('LEFT','`#__banners` as b ON b.id=a.banner_id');
  94. $query->select('b.name as name');
  95. // Join with the client
  96. $query->join('LEFT','`#__banner_clients` as cl ON cl.id=b.cid');
  97. $query->select('cl.name as client_name');
  98. // Join with the category
  99. $query->join('LEFT','`#__categories` as cat ON cat.id=b.catid');
  100. $query->select('cat.title as category_title');
  101. // Filter by type
  102. $type = $this->getState('filter.type');
  103. if (!empty($type)) {
  104. $query->where('a.track_type = '.(int) $type);
  105. }
  106. // Filter by client
  107. $clientId = $this->getState('filter.client_id');
  108. if (is_numeric($clientId)) {
  109. $query->where('b.cid = '.(int) $clientId);
  110. }
  111. // Filter by category
  112. $catedoryId = $this->getState('filter.category_id');
  113. if (is_numeric($catedoryId)) {
  114. $query->where('b.catid = '.(int) $catedoryId);
  115. }
  116. // Filter by begin date
  117. $begin = $this->getState('filter.begin');
  118. if (!empty($begin)) {
  119. $query->where('a.track_date >= '.$db->Quote($begin));
  120. }
  121. // Filter by end date
  122. $end = $this->getState('filter.end');
  123. if (!empty($end)) {
  124. $query->where('a.track_date <= '.$db->Quote($end));
  125. }
  126. // Add the list ordering clause.
  127. $orderCol = $this->getState('list.ordering', 'name');
  128. $query->order($db->getEscaped($orderCol).' '.$db->getEscaped($this->getState('list.direction', 'ASC')));
  129. return $query;
  130. }
  131. /**
  132. * Method to delete rows.
  133. *
  134. * @param array An array of item ids.
  135. *
  136. * @return boolean Returns true on success, false on failure.
  137. */
  138. public function delete()
  139. {
  140. // Initialise variables
  141. $user = JFactory::getUser();
  142. $categoryId = $this->getState('category_id');
  143. // Access checks.
  144. if ($categoryId) {
  145. $allow = $user->authorise('core.delete', 'com_banners.category.'.(int) $categoryId);
  146. } else {
  147. $allow = $user->authorise('core.delete', 'com_banners');
  148. }
  149. if ($allow) {
  150. // Delete tracks from this banner
  151. $db = $this->getDbo();
  152. $query = $db->getQuery(true);
  153. $query->delete();
  154. $query->from('`#__banner_tracks`');
  155. // Filter by type
  156. $type = $this->getState('filter.type');
  157. if (!empty($type)) {
  158. $query->where('track_type = '.(int) $type);
  159. }
  160. // Filter by begin date
  161. $begin = $this->getState('filter.begin');
  162. if (!empty($begin)) {
  163. $query->where('track_date >= '.$db->Quote($begin));
  164. }
  165. // Filter by end date
  166. $end = $this->getState('filter.end');
  167. if (!empty($end)) {
  168. $query->where('track_date <= '.$db->Quote($end));
  169. }
  170. $where = '1';
  171. // Filter by client
  172. $clientId = $this->getState('filter.client_id');
  173. if (!empty($clientId)) {
  174. $where.=' AND cid = '.(int) $clientId;
  175. }
  176. // Filter by category
  177. if (!empty($categoryId)) {
  178. $where.=' AND catid = '.(int) $categoryId;
  179. }
  180. $query->where('banner_id IN (SELECT id FROM `#__banners` WHERE '.$where.')');
  181. $db->setQuery((string)$query);
  182. $this->setError((string)$query);
  183. $db->query();
  184. // Check for a database error.
  185. if ($db->getErrorNum()) {
  186. $this->setError($db->getErrorMsg());
  187. return false;
  188. }
  189. } else {
  190. JError::raiseWarning(403, JText::_('JERROR_CORE_DELETE_NOT_PERMITTED'));
  191. }
  192. return true;
  193. }
  194. /**
  195. * Get file name
  196. *
  197. * @return string The file name
  198. * @since 1.6
  199. */
  200. public function getBaseName()
  201. {
  202. if (!isset($this->basename)) {
  203. $app = JFactory::getApplication();
  204. $basename = $this->getState('basename');
  205. $basename = str_replace('__SITE__',$app->getCfg('sitename'),$basename);
  206. $categoryId = $this->getState('filter.category_id');
  207. if (is_numeric($categoryId)) {
  208. if ($categoryId > 0) {
  209. $basename = str_replace('__CATID__',$categoryId,$basename);
  210. } else {
  211. $basename = str_replace('__CATID__','',$basename);
  212. }
  213. $categoryName = $this->getCategoryName();
  214. $basename = str_replace('__CATNAME__',$categoryName,$basename);
  215. } else {
  216. $basename = str_replace('__CATID__','',$basename);
  217. $basename = str_replace('__CATNAME__','',$basename);
  218. }
  219. $clientId = $this->getState('filter.client_id');
  220. if (is_numeric($clientId)) {
  221. if ($clientId > 0) {
  222. $basename = str_replace('__CLIENTID__',$clientId,$basename);
  223. } else {
  224. $basename = str_replace('__CLIENTID__','',$basename);
  225. }
  226. $clientName = $this->getClientName();
  227. $basename = str_replace('__CLIENTNAME__',$clientName,$basename);
  228. } else {
  229. $basename = str_replace('__CLIENTID__','',$basename);
  230. $basename = str_replace('__CLIENTNAME__','',$basename);
  231. }
  232. $type = $this->getState('filter.type');
  233. if ($type > 0) {
  234. $basename = str_replace('__TYPE__',$type,$basename);
  235. $typeName = JText::_('COM_BANNERS_TYPE'.$type);
  236. $basename = str_replace('__TYPENAME__',$typeName,$basename);
  237. } else {
  238. $basename = str_replace('__TYPE__','',$basename);
  239. $basename = str_replace('__TYPENAME__','',$basename);
  240. }
  241. $begin = $this->getState('filter.begin');
  242. if (!empty($begin)) {
  243. $basename = str_replace('__BEGIN__',$begin,$basename);
  244. } else {
  245. $basename = str_replace('__BEGIN__','',$basename);
  246. }
  247. $end = $this->getState('filter.end');
  248. if (!empty($end)) {
  249. $basename = str_replace('__END__',$end,$basename);
  250. } else {
  251. $basename = str_replace('__END__','',$basename);
  252. }
  253. $this->basename = $basename;
  254. }
  255. return $this->basename;
  256. }
  257. /**
  258. * Get the category name.
  259. *
  260. * @return string The category name
  261. * @since 1.6
  262. */
  263. protected function getCategoryName()
  264. {
  265. $categoryId = $this->getState('filter.category_id');
  266. if ($categoryId) {
  267. $db = $this->getDbo();
  268. $query = $db->getQuery(true);
  269. $query->select('title');
  270. $query->from('`#__categories`');
  271. $query->where('`id`='.$db->quote($categoryId));
  272. $db->setQuery((string)$query);
  273. $name = $db->loadResult();
  274. if ($db->getErrorNum()) {
  275. $this->setError($db->getErrorMsg());
  276. return false;
  277. }
  278. } else {
  279. $name = JText::_('COM_BANNERS_NOCATEGORYNAME');
  280. }
  281. return $name;
  282. }
  283. /**
  284. * Get the category name
  285. *
  286. * @return string The category name.
  287. * @since 1.6
  288. */
  289. protected function getClientName()
  290. {
  291. $clientId = $this->getState('filter.client_id');
  292. if ($clientId) {
  293. $db = $this->getDbo();
  294. $query = $db->getQuery(true);
  295. $query->select('name');
  296. $query->from('`#__banner_clients`');
  297. $query->where('`id`='.$db->quote($clientId));
  298. $db->setQuery((string)$query);
  299. $name = $db->loadResult();
  300. if ($db->getErrorNum()) {
  301. $this->setError($db->getErrorMsg());
  302. return false;
  303. }
  304. } else {
  305. $name = JText::_('COM_BANNERS_NOCLIENTNAME');
  306. }
  307. return $name;
  308. }
  309. /**
  310. * Get the file type.
  311. *
  312. * @return string The file type
  313. * @since 1.6
  314. */
  315. public function getFileType()
  316. {
  317. return $this->getState('compressed') ? 'zip' : 'csv';
  318. }
  319. /**
  320. * Get the mime type.
  321. *
  322. * @return string The mime type.
  323. * @since 1.6
  324. */
  325. public function getMimeType()
  326. {
  327. return $this->getState('compressed') ? 'application/zip' : 'text/csv';
  328. }
  329. /**
  330. * Get the content
  331. *
  332. * @return string The content.
  333. * @since 1.6
  334. */
  335. public function getContent()
  336. {
  337. if (!isset($this->content)) {
  338. $this->content = '';
  339. $this->content.=
  340. '"'.str_replace('"','""',JText::_('COM_BANNERS_HEADING_NAME')).'","'.
  341. str_replace('"','""',JText::_('COM_BANNERS_HEADING_CLIENT')).'","'.
  342. str_replace('"','""',JText::_('JCATEGORY')).'","'.
  343. str_replace('"','""',JText::_('COM_BANNERS_HEADING_TYPE')).'","'.
  344. str_replace('"','""',JText::_('COM_BANNERS_HEADING_COUNT')).'","'.
  345. str_replace('"','""',JText::_('JDATE')).'"'."\n";
  346. foreach($this->getItems() as $item) {
  347. $this->content.=
  348. '"'.str_replace('"','""',$item->name).'","'.
  349. str_replace('"','""',$item->client_name).'","'.
  350. str_replace('"','""',$item->category_title).'","'.
  351. str_replace('"','""',($item->track_type==1 ? JText::_('COM_BANNERS_IMPRESSION'): JText::_('COM_BANNERS_CLICK'))).'","'.
  352. str_replace('"','""',$item->count).'","'.
  353. str_replace('"','""',$item->track_date).'"'."\n";
  354. }
  355. if ($this->getState('compressed')) {
  356. $app = JFactory::getApplication('administrator');
  357. $files = array();
  358. $files['track']=array();
  359. $files['track']['name'] = $this->getBasename() . '.csv';
  360. $files['track']['data'] = $this->content;
  361. $files['track']['time'] = time();
  362. $ziproot = $app->getCfg('tmp_path').'/' . uniqid('banners_tracks_') . '.zip';
  363. // run the packager
  364. jimport('joomla.filesystem.folder');
  365. jimport('joomla.filesystem.file');
  366. jimport('joomla.filesystem.archive');
  367. $delete = JFolder::files($app->getCfg('tmp_path').'/', uniqid('banners_tracks_'),false,true);
  368. if (!empty($delete)) {
  369. if (!JFile::delete($delete)) {
  370. // JFile::delete throws an error
  371. $this->setError(JText::_('COM_BANNERS_ERR_ZIP_DELETE_FAILURE'));
  372. return false;
  373. }
  374. }
  375. if (!$packager = JArchive::getAdapter('zip')) {
  376. $this->setError(JText::_('COM_BANNERS_ERR_ZIP_ADAPTER_FAILURE'));
  377. return false;
  378. } else if (!$packager->create($ziproot, $files)) {
  379. $this->setError(JText::_('COM_BANNERS_ERR_ZIP_CREATE_FAILURE'));
  380. return false;
  381. }
  382. $this->content = file_get_contents($ziproot);
  383. }
  384. }
  385. return $this->content;
  386. }
  387. }