PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_easyblog/models/tags.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 574 lines | 350 code | 129 blank | 95 comment | 32 complexity | 3a3305b19c2c543f95eff214b6beb52b MD5 | raw file
  1. <?php
  2. /**
  3. * @package EasyBlog
  4. * @copyright Copyright (C) 2010 Stack Ideas Private Limited. All rights reserved.
  5. * @license GNU/GPL, see LICENSE.php
  6. *
  7. * EasyBlog is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. defined('_JEXEC') or die('Restricted access');
  14. jimport('joomla.application.component.model');
  15. require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'model.php' );
  16. class EasyBlogModelTags extends EasyBlogModel
  17. {
  18. /**
  19. * Category total
  20. *
  21. * @var integer
  22. */
  23. var $_total = null;
  24. /**
  25. * Pagination object
  26. *
  27. * @var object
  28. */
  29. var $_pagination = null;
  30. /**
  31. * Category data array
  32. *
  33. * @var array
  34. */
  35. var $_data = null;
  36. function __construct()
  37. {
  38. parent::__construct();
  39. $mainframe = JFactory::getApplication();
  40. $limit = $mainframe->getUserStateFromRequest( 'com_easyblog.tags.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
  41. $limitstart = JRequest::getInt('limitstart', 0, '' );
  42. $this->setState('limit', $limit);
  43. $this->setState('limitstart', $limitstart);
  44. }
  45. /**
  46. * Method to get the total nr of the categories
  47. *
  48. * @access public
  49. * @return integer
  50. */
  51. function getTotal()
  52. {
  53. // Lets load the content if it doesn't already exist
  54. if (empty($this->_total))
  55. {
  56. $query = $this->_buildQuery();
  57. $this->_total = $this->_getListCount($query);
  58. }
  59. return $this->_total;
  60. }
  61. /**
  62. * Method to get a pagination object for the categories
  63. *
  64. * @access public
  65. * @return integer
  66. */
  67. function getPagination()
  68. {
  69. // Lets load the content if it doesn't already exist
  70. if (empty($this->_pagination))
  71. {
  72. jimport('joomla.html.pagination');
  73. $this->_pagination = EasyBlogHelper::getPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
  74. }
  75. return $this->_pagination;
  76. }
  77. /**
  78. * Method to build the query for the tags
  79. *
  80. * @access private
  81. * @return string
  82. */
  83. function _buildQuery()
  84. {
  85. // Get the WHERE and ORDER BY clauses for the query
  86. $where = $this->_buildQueryWhere();
  87. $orderby = $this->_buildQueryOrderBy();
  88. $db = EasyBlogHelper::db();
  89. $query = 'SELECT * FROM ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote( '#__easyblog_tag' )
  90. . $where . ' '
  91. . $orderby;
  92. return $query;
  93. }
  94. function _buildQueryWhere()
  95. {
  96. $mainframe = JFactory::getApplication();
  97. $db = EasyBlogHelper::db();
  98. $filter_state = $mainframe->getUserStateFromRequest( 'com_easyblog.tags.filter_state', 'filter_state', '', 'word' );
  99. $search = $mainframe->getUserStateFromRequest( 'com_easyblog.tags.search', 'search', '', 'string' );
  100. $search = $db->getEscaped( trim(JString::strtolower( $search ) ) );
  101. $where = array();
  102. if ( $filter_state )
  103. {
  104. if ( $filter_state == 'P' )
  105. {
  106. $where[] = EasyBlogHelper::getHelper( 'SQL' )->nameQuote( 'published' ) . '=' . $db->Quote( '1' );
  107. }
  108. else if ($filter_state == 'U' )
  109. {
  110. $where[] = EasyBlogHelper::getHelper( 'SQL' )->nameQuote( 'published' ) . '=' . $db->Quote( '0' );
  111. }
  112. }
  113. if ($search)
  114. {
  115. $where[] = ' LOWER( title ) LIKE \'%' . $search . '%\' ';
  116. }
  117. $where = ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
  118. return $where;
  119. }
  120. function _buildQueryOrderBy()
  121. {
  122. $mainframe = JFactory::getApplication();
  123. $filter_order = $mainframe->getUserStateFromRequest( 'com_easyblog.tags.filter_order', 'filter_order', 'ordering', 'cmd' );
  124. $filter_order_Dir = $mainframe->getUserStateFromRequest( 'com_easyblog.tags.filter_order_Dir', 'filter_order_Dir', '', 'word' );
  125. $orderby = ' ORDER BY '.$filter_order.' '.$filter_order_Dir.', ordering';
  126. return $orderby;
  127. }
  128. /**
  129. * Method to get categories item data
  130. *
  131. * @access public
  132. * @return array
  133. */
  134. function getData()
  135. {
  136. // Lets load the content if it doesn't already exist
  137. if (empty($this->_data))
  138. {
  139. $query = $this->_buildQuery();
  140. $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
  141. }
  142. return $this->_data;
  143. }
  144. /**
  145. * Method to publish or unpublish tags
  146. *
  147. * @access public
  148. * @return array
  149. */
  150. function publish( $tags = array(), $publish = 1 )
  151. {
  152. if( count( $tags ) > 0 )
  153. {
  154. $db = EasyBlogHelper::db();
  155. $tags = implode( ',' , $tags );
  156. $query = 'UPDATE ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote( '#__easyblog_tag' ) . ' '
  157. . 'SET ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote( 'published' ) . '=' . $db->Quote( $publish ) . ' '
  158. . 'WHERE ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote( 'id' ) . ' IN (' . $tags . ')';
  159. $db->setQuery( $query );
  160. if( !$db->query() )
  161. {
  162. $this->setError($this->_db->getErrorMsg());
  163. return false;
  164. }
  165. return true;
  166. }
  167. return false;
  168. }
  169. function searchTag($title)
  170. {
  171. $db = EasyBlogHelper::db();
  172. $query = 'SELECT ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote('id') . ' '
  173. . 'FROM ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote('#__easyblog_tag') . ' '
  174. . 'WHERE ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote('title') . ' = ' . $db->quote($title) . ' '
  175. . 'LIMIT 1';
  176. $db->setQuery($query);
  177. $result = $db->loadObject();
  178. return $result;
  179. }
  180. /**
  181. * Method to get total tags created so far iregardless the status.
  182. *
  183. * @access public
  184. * @return integer
  185. */
  186. function getTotalTags( $userId = 0)
  187. {
  188. $db = EasyBlogHelper::db();
  189. $where = array();
  190. $query = 'SELECT COUNT(1) FROM ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote( '#__easyblog_tag' );
  191. if(! empty($userId))
  192. $where[] = '`created_by` = ' . $db->Quote($userId);
  193. $extra = ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
  194. $query = $query . $extra;
  195. $db->setQuery( $query );
  196. $result = $db->loadResult();
  197. return (empty($result)) ? 0 : $result;
  198. }
  199. /**
  200. * *********************************************************************
  201. * These part of codes will used in dashboard tags.
  202. * *********************************************************************
  203. */
  204. function _buildQueryByBlogger($bloggerId, $sort = 'title')
  205. {
  206. $db = EasyBlogHelper::db();
  207. $query = 'select a.`id`, a.`title`, a.`alias`, a.`created`, count(b.`id`) as `post_count`, a.`published`';
  208. $query .= ' from #__easyblog_tag as a';
  209. $query .= ' left join #__easyblog_post_tag as b';
  210. $query .= ' on a.`id` = b.`tag_id`';
  211. $query .= ' where a.created_by = ' . $db->Quote($bloggerId);
  212. $query .= ' group by (a.`id`)';
  213. if( $sort == 'post')
  214. $query .= ' order by count(b.`id`) desc';
  215. else
  216. $query .= ' order by a.`title`';
  217. return $query;
  218. }
  219. function getTagsByBlogger($bloggerId, $usePagination = true, $sort = 'title')
  220. {
  221. $db = EasyBlogHelper::db();
  222. $query = $this->_buildQueryByBlogger($bloggerId, $sort);
  223. //$db->setQuery($query);
  224. $result = null;
  225. if( $usePagination )
  226. {
  227. $pg = $this->getPaginationByBlogger($bloggerId, $sort);
  228. $result = $this->_getList($query, $pg->limitstart, $pg->limit);
  229. }
  230. else
  231. {
  232. $result = $this->_getList( $query );
  233. }
  234. return $result;
  235. }
  236. function getPaginationByBlogger($bloggerId, $sort = 'title')
  237. {
  238. jimport('joomla.html.pagination');
  239. $this->_pagination = EasyBlogHelper::getPagination( $this->getTotalByBlogger( $bloggerId , $sort ), $this->getState('limitstart'), $this->getState('limit') );
  240. return $this->_pagination;
  241. }
  242. function getTotalByBlogger($bloggerId, $sort = 'title')
  243. {
  244. // Lets load the content if it doesn't already exist
  245. $query = $this->_buildQueryByBlogger($bloggerId, $sort);
  246. $total = $this->_getListCount($query);
  247. return $total;
  248. }
  249. /**
  250. * *********************************************************************
  251. * END: These part of codes will used in dashboard tags.
  252. * *********************************************************************
  253. */
  254. function isExist($tagName, $excludeTagIds='0')
  255. {
  256. $db = EasyBlogHelper::db();
  257. $query = 'SELECT COUNT(1) FROM #__easyblog_tag';
  258. $query .= ' WHERE `title` = ' . $db->Quote($tagName);
  259. if($excludeTagIds != '0')
  260. $query .= ' AND `id` != ' . $db->Quote($excludeTagIds);
  261. $db->setQuery($query);
  262. $result = $db->loadResult();
  263. return (empty($result)) ? 0 : $result;
  264. }
  265. function getTagCloud($limit='', $order='title', $sort='asc', $checkAccess = false)
  266. {
  267. $db = EasyBlogHelper::db();
  268. $my = JFactory::getUser();
  269. $isBloggerMode = EasyBlogRouter::isBloggerMode();
  270. $queryExclude = '';
  271. $excludeCats = array();
  272. if($checkAccess)
  273. {
  274. // get all private categories id
  275. $excludeCats = EasyBlogHelper::getPrivateCategories();
  276. }
  277. if(! empty($excludeCats))
  278. {
  279. $queryExclude .= ' AND c.`category_id` NOT IN (' . implode(',', $excludeCats) . ')';
  280. }
  281. $query = 'select a.`id`, a.`title`, a.`alias`, a.`created`, count(c.`id`) as `post_count`';
  282. $query .= ' from #__easyblog_tag as a';
  283. $query .= ' left join #__easyblog_post_tag as b';
  284. $query .= ' on a.`id` = b.`tag_id`';
  285. $query .= ' left join #__easyblog_post as c';
  286. $query .= ' on b.post_id = c.id';
  287. $query .= ' and c.`published` = ' . $db->Quote('1');
  288. if($isBloggerMode !== false)
  289. $query .= ' and c.`created_by` = ' . $db->Quote($isBloggerMode);
  290. if($checkAccess)
  291. {
  292. if($my->id == 0)
  293. $query .= ' and c.`private` = ' . $db->Quote(BLOG_PRIVACY_PUBLIC);
  294. $query .= $queryExclude;
  295. }
  296. $query .= ' where a.`published` = ' . $db->Quote('1');
  297. $query .= ' group by (a.`id`)';
  298. //order
  299. switch($order)
  300. {
  301. case 'postcount':
  302. $query .= ' order by `post_count`';
  303. break;
  304. case 'title':
  305. default:
  306. $query .= ' order by a.`title`';
  307. }
  308. //sort
  309. switch($sort)
  310. {
  311. case 'asc':
  312. $query .= ' asc ';
  313. break;
  314. case 'desc':
  315. default:
  316. $query .= ' desc ';
  317. }
  318. //limit
  319. if(!empty($limit))
  320. {
  321. $query .= ' LIMIT ' . (INT)$limit;
  322. }
  323. $db->setQuery($query);
  324. $result = $db->loadObjectList();
  325. return $result;
  326. }
  327. function getTags($count="")
  328. {
  329. $db = EasyBlogHelper::db();
  330. $query = ' SELECT `id`, `title`, `alias` ';
  331. $query .= ' FROM #__easyblog_tag ';
  332. $query .= ' WHERE `published` = 1 ';
  333. $query .= ' ORDER BY `title`';
  334. if(!empty($count))
  335. {
  336. $query .= ' LIMIT ' . $count;
  337. }
  338. $db->setQuery($query);
  339. $result = $db->loadObjectList();
  340. return $result;
  341. }
  342. /**
  343. * *********************************************************************
  344. * These part of codes will used in tag clod tags.
  345. * *********************************************************************
  346. */
  347. function _buildQueryByTagBlogs()
  348. {
  349. $db = EasyBlogHelper::db();
  350. $query = 'select count(a.`tag_id`) as `cnt`, b.*';
  351. $query .= ' from `#__easyblog_post_tag` as a';
  352. $query .= ' inner join `#__easyblog_post` as b on a.`post_id` = b.`id`';
  353. $query .= ' group by (a.`post_id`)';
  354. $query .= ' order by `cnt` desc';
  355. return $query;
  356. }
  357. function getTagBlogs()
  358. {
  359. $db = EasyBlogHelper::db();
  360. $query = $this->_buildQueryByTagBlogs();
  361. $pg = $this->getPaginationByTagBlogs();
  362. //$db->setQuery($query);
  363. $result = $this->_getList($query, $pg->limitstart, $pg->limit);
  364. return $result;
  365. }
  366. function getPaginationByTagBlogs()
  367. {
  368. jimport('joomla.html.pagination');
  369. $this->_pagination = EasyBlogHelper::getPagination( $this->getTotalByTagBlogs(), $this->getState('limitstart'), $this->getState('limit') );
  370. return $this->_pagination;
  371. }
  372. function getTotalByTagBlogs()
  373. {
  374. // Lets load the content if it doesn't already exist
  375. $query = $this->_buildQueryByTagBlogs();
  376. $total = $this->_getListCount($query);
  377. return $total;
  378. }
  379. function getTeamBlogCount( $tagId )
  380. {
  381. $db = EasyBlogHelper::db();
  382. $my = JFactory::getUser();
  383. $config = EasyBlogHelper::getConfig();
  384. $isBloggerMode = EasyBlogRouter::isBloggerMode();
  385. $extraQuery = '';
  386. $query = 'select count(1) from `#__easyblog_post` as a';
  387. $query .= ' inner join `#__easyblog_post_tag` as b';
  388. $query .= ' on a.`id` = b.`post_id`';
  389. $query .= ' and b.`tag_id` = ' . $db->Quote($tagId);
  390. $query .= ' inner join `#__easyblog_team_post` as c';
  391. $query .= ' on a.`id` = c.`post_id`';
  392. if( $config->get( 'main_includeteamblogpost' ) )
  393. {
  394. $teamBlogIds = EasyBlogHelper::getViewableTeamIds();
  395. if( count( $teamBlogIds ) > 0 )
  396. $teamBlogIds = implode( ',' , $teamBlogIds);
  397. }
  398. $query .= ' where a.`issitewide` = ' . $db->Quote('0');
  399. if( !empty($extraQuery) )
  400. $query .= $extraQuery;
  401. if($isBloggerMode !== false)
  402. $query .= ' and a.`created_by` = ' . $db->Quote($isBloggerMode);
  403. $db->setQuery($query);
  404. $result = $db->loadResult();
  405. return (empty($result)) ? '0' : $result;
  406. }
  407. /**
  408. * *********************************************************************
  409. * These part of codes will used in tag clod tags.
  410. * *********************************************************************
  411. */
  412. function getTagPrivateBlogCount( $tagId )
  413. {
  414. $db = EasyBlogHelper::db();
  415. $queryExclude = '';
  416. $excludeCats = array();
  417. $isBloggerMode = EasyBlogRouter::isBloggerMode();
  418. // get all private categories id
  419. $excludeCats = EasyBlogHelper::getPrivateCategories();
  420. if(! empty($excludeCats))
  421. {
  422. $queryExclude .= ' AND a.`category_id` NOT IN (' . implode(',', $excludeCats) . ')';
  423. }
  424. $query = 'select count(1) from `#__easyblog_post` as a';
  425. $query .= ' inner join `#__easyblog_post_tag` as b';
  426. $query .= ' on a.`id` = b.`post_id`';
  427. $query .= ' and b.`tag_id` = ' . $db->Quote($tagId);
  428. $query .= ' where a.`private` = ' . $db->Quote(BLOG_PRIVACY_PRIVATE);
  429. if($isBloggerMode !== false)
  430. $query .= ' and a.`created_by` = ' . $db->Quote($isBloggerMode);
  431. $query .= $queryExclude;
  432. $db->setQuery($query);
  433. $result = $db->loadResult();
  434. return (empty($result)) ? '0' : $result;
  435. }
  436. function getDefaultTags()
  437. {
  438. $db = EasyBlogHelper::db();
  439. $query = 'SELECT `id` FROM `#__easyblog_tag` '
  440. . 'WHERE `default` = 1';
  441. $db->setQuery($query);
  442. $tags = $db->loadResultArray();
  443. return $tags;
  444. }
  445. }