PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_easyblog/tables/tag.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 338 lines | 240 code | 58 blank | 40 comment | 27 complexity | 2ce2527a3a0bd494dbf8f2451212e822 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. * EasyBlog is free software. This version may have been modified pursuant
  7. * to the GNU General Public License, and as distributed it includes or
  8. * is derivative of works licensed under the GNU General Public License or
  9. * other free or open source software licenses.
  10. * See COPYRIGHT.php for copyright notices and details.
  11. */
  12. defined('_JEXEC') or die('Restricted access');
  13. require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'table.php' );
  14. require_once( JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_easyblog' . DIRECTORY_SEPARATOR . 'constants.php' );
  15. require_once( EBLOG_HELPERS . DIRECTORY_SEPARATOR . 'router.php' );
  16. class EasyBlogTableTag extends EasyBlogTable
  17. {
  18. var $id = null;
  19. var $created_by = null;
  20. var $title = null;
  21. var $alias = null;
  22. var $created = null;
  23. var $status = null;
  24. var $published = null;
  25. var $default = null;
  26. var $ordering = null;
  27. /**
  28. * Constructor for this class.
  29. *
  30. * @return
  31. * @param object $db
  32. */
  33. function __construct(& $db )
  34. {
  35. parent::__construct( '#__easyblog_tag' , 'id' , $db );
  36. }
  37. function load( $id = null , $loadByTitle = false)
  38. {
  39. if( !$loadByTitle)
  40. {
  41. static $titles = null;
  42. if( !isset( $titles[ $id ] ) )
  43. {
  44. $titles[ $id ] = parent::load( $id );
  45. }
  46. return $titles[ $id ];
  47. }
  48. static $tags = null;
  49. if( !isset( $tags[ $id ] ) )
  50. {
  51. $db = EasyBlogHelper::db();
  52. $query = 'SELECT *';
  53. $query .= ' FROM ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote('#__easyblog_tag');
  54. $query .= ' WHERE (' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote('title') . ' = ' . $db->Quote( JString::str_ireplace( ':' , '-' , $id ) );
  55. $query .= ' OR ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote('title') . ' = ' . $db->Quote( JString::str_ireplace( '-' , ' ' , $id ) ) . ' ';
  56. $query .= ' OR ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote('alias') . ' = ' . $db->Quote( JString::str_ireplace( ':' , '-' , $id ) ) . ')';
  57. $query .= ' LIMIT 1';
  58. $db->setQuery($query);
  59. $result = $db->loadObject();
  60. if( $result )
  61. {
  62. $this->id = $result->id;
  63. $this->title = $result->title;
  64. $this->created_by = $result->created_by;
  65. $this->alias = $result->alias;
  66. $this->created = $result->created;
  67. $this->status = $result->status;
  68. $this->published = $result->published;
  69. $this->ordering = $result->ordering;
  70. $tags[ $id ] = true;
  71. }
  72. else
  73. {
  74. $tags[ $id ] = false;
  75. }
  76. }
  77. return $tags[ $id ];
  78. }
  79. function aliasExists()
  80. {
  81. $db = $this->getDBO();
  82. $query = 'SELECT COUNT(1) FROM ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote( '#__easyblog_tag' ) . ' '
  83. . 'WHERE ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote( 'alias' ) . '=' . $db->Quote( $this->alias );
  84. if( $this->id != 0 )
  85. {
  86. $query .= ' AND ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote( 'id' ) . '!=' . $db->Quote( $this->id );
  87. }
  88. $db->setQuery( $query );
  89. return $db->loadResult() > 0 ? true : false;
  90. }
  91. function exists( $title , $isNew = true )
  92. {
  93. $db = EasyBlogHelper::db();
  94. $query = 'SELECT COUNT(1) '
  95. . 'FROM ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote('#__easyblog_tag') . ' '
  96. . 'WHERE ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote('title') . ' = ' . $db->quote($title);
  97. if( !$isNew )
  98. {
  99. $query .= ' AND ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote( 'id' ) . '!=' . $db->Quote( $this->id );
  100. }
  101. $query .= ' LIMIT 1';
  102. $db->setQuery($query);
  103. $result = $db->loadResult() > 0 ? true : false;
  104. return $result;
  105. }
  106. /**
  107. * Overrides parent's bind method to add our own logic.
  108. *
  109. * @param Array $data
  110. **/
  111. function bind( $data, $ignore = array() )
  112. {
  113. parent::bind( $data, $ignore );
  114. if( empty( $this->created ) )
  115. {
  116. $date = EasyBlogHelper::getDate();
  117. $this->created = $date->toMySQL();
  118. }
  119. jimport( 'joomla.filesystem.filter.filteroutput');
  120. $i = 1;
  121. while( $this->aliasExists() || empty($this->alias) )
  122. {
  123. $this->alias = empty($this->alias) ? $this->title : $this->alias . '-' . $i;
  124. $i++;
  125. }
  126. $this->alias = EasyBlogRouter::generatePermalink( $this->alias );
  127. }
  128. /**
  129. * Overrides parent's delete method to add our own logic.
  130. *
  131. * @return boolean
  132. * @param object $db
  133. */
  134. function delete($pk = null)
  135. {
  136. $db = $this->getDBO();
  137. // Ensure that tag associations are removed
  138. $this->deletePostTag();
  139. if( $this->created_by != 0 )
  140. {
  141. JFactory::getLanguage()->load( 'com_easyblog' , JPATH_ROOT );
  142. $config = EasyBlogHelper::getConfig();
  143. // @rule: Integrations with EasyDiscuss
  144. EasyBlogHelper::getHelper( 'EasyDiscuss' )->log( 'easyblog.delete.tag' , $this->created_by , JText::sprintf( 'COM_EASYBLOG_EASYDISCUSS_HISTORY_NEW_TAG' , $this->title ) );
  145. EasyBlogHelper::getHelper( 'EasyDiscuss' )->addPoint( 'easyblog.delete.tag' , $this->created_by );
  146. EasyBlogHelper::getHelper( 'EasyDiscuss' )->addBadge( 'easyblog.delete.tag' , $this->created_by );
  147. if( $config->get('main_jomsocial_userpoint') )
  148. {
  149. $jsUserPoint = JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'userpoints.php';
  150. if( JFile::exists( $jsUserPoint ) )
  151. {
  152. require_once( $jsUserPoint );
  153. CUserPoints::assignPoint( 'com_easyblog.tag.remove' , $this->created_by );
  154. }
  155. }
  156. // AlphaUserPoints
  157. // since 1.2
  158. if( EasyBlogHelper::isAUPEnabled() )
  159. {
  160. AlphaUserPointsHelper::newpoints( 'plgaup_easyblog_delete_tag', AlphaUserPointsHelper::getAnyUserReferreID( $this->created_by ) , '', JText::sprintf('COM_EASYBLOG_AUP_TAG_DELETED', $this->title) );
  161. }
  162. }
  163. $my = JFactory::getUser();
  164. //activity logging.
  165. $activity = new stdClass();
  166. $activity->actor_id = $my->id;
  167. $activity->target_id = '0';
  168. $activity->context_type = 'tag';
  169. $activity->context_id = $this->id;
  170. $activity->verb = 'delete';
  171. $activity->uuid = $this->title;
  172. $state = parent::delete();
  173. if( $state )
  174. {
  175. EasyBlogHelper::activityLog( $activity );
  176. }
  177. return $state;
  178. }
  179. // method to delete all the blog post that associated with the current tag
  180. function deletePostTag()
  181. {
  182. $db = $this->getDBO();
  183. $query = 'DELETE FROM ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote( '#__easyblog_post_tag' ) . ' '
  184. . 'WHERE ' . EasyBlogHelper::getHelper( 'SQL' )->nameQuote( 'tag_id' ) . '=' . $db->Quote( $this->id );
  185. $db->setQuery( $query );
  186. if($db->query($db))
  187. {
  188. return true;
  189. }
  190. else
  191. {
  192. return false;
  193. }
  194. }
  195. function getPostCount()
  196. {
  197. $db = $this->getDBO();
  198. $query = 'select count(1) from `#__easyblog_post_tag`';
  199. $query .= ' where `tag_id` = ' . $db->Quote( $this->id );
  200. $db->setQuery( $query );
  201. $result = $db->loadResult();
  202. return ( empty( $result ) ) ? 0 : $result;
  203. }
  204. public function store($updateNulls = false)
  205. {
  206. JFactory::getLanguage()->load( 'com_easyblog' , JPATH_ROOT );
  207. // @rule: Check for empty title
  208. if( empty( $this->title ) )
  209. {
  210. $this->setError( JText::_( 'COM_EASYBLOG_INVALID_TAG' ) );
  211. return false;
  212. }
  213. // @rule: Check if such tag exists.
  214. if( $this->exists( $this->title , !$this->id ) )
  215. {
  216. $this->setError( JText::_( 'COM_EASYBLOG_TAG_ALREADY_EXISTS' ) );
  217. return false;
  218. }
  219. // @task: If alias is null, we need to generate them here.
  220. jimport( 'joomla.filesystem.filter.filteroutput');
  221. $i = 1;
  222. while( $this->aliasExists() || empty($this->alias) )
  223. {
  224. $this->alias = empty($this->alias) ? $this->title : $this->alias . '-' . $i;
  225. $i++;
  226. }
  227. $this->alias = EasyBlogRouter::generatePermalink( $this->alias );
  228. if( !empty( $this->created ))
  229. {
  230. $offset = EasyBlogDateHelper::getOffSet();
  231. $newDate = EasyBlogHelper::getDate($this->created, $offset);
  232. $this->created = $newDate->toMySQL();
  233. }
  234. else
  235. {
  236. $newDate = EasyBlogHelper::getDate();
  237. $this->created = $newDate->toMySQL();
  238. }
  239. $isNew = !$this->id;
  240. $state = parent::store();
  241. $my = JFactory::getUser();
  242. if( $isNew && $my->id != 0 )
  243. {
  244. JFactory::getLanguage()->load( 'com_easyblog' , JPATH_ROOT );
  245. $config = EasyBlogHelper::getConfig();
  246. // @rule: Integrations with EasyDiscuss
  247. EasyBlogHelper::getHelper( 'EasyDiscuss' )->log( 'easyblog.new.tag' , $my->id , JText::sprintf( 'COM_EASYBLOG_EASYDISCUSS_HISTORY_NEW_TAG' , $this->title ) );
  248. EasyBlogHelper::getHelper( 'EasyDiscuss' )->addPoint( 'easyblog.new.tag' , $my->id );
  249. EasyBlogHelper::getHelper( 'EasyDiscuss' )->addBadge( 'easyblog.new.tag' , $my->id );
  250. if( $config->get('main_jomsocial_userpoint') )
  251. {
  252. $jsUserPoint = JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'userpoints.php';
  253. if( JFile::exists( $jsUserPoint ) )
  254. {
  255. require_once( $jsUserPoint );
  256. CUserPoints::assignPoint( 'com_easyblog.tag.add' , $my->id );
  257. }
  258. }
  259. // AlphaUserPoints
  260. // since 1.2
  261. if( EasyBlogHelper::isAUPEnabled() )
  262. {
  263. AlphaUserPointsHelper::newpoints( 'plgaup_easyblog_add_tag', '', 'easyblog_add_tag_' . $this->id, JText::sprintf('COM_EASYBLOG_AUP_TAG_ADDED', $this->title) );
  264. }
  265. }
  266. if( $state )
  267. {
  268. //activity logging.
  269. $activity = new stdClass();
  270. $activity->actor_id = $my->id;
  271. $activity->target_id = '0';
  272. $activity->context_type = 'tag';
  273. $activity->context_id = $this->id;
  274. $activity->verb = ( $isNew ) ? 'add' : 'update';
  275. $activity->uuid = $this->title;
  276. EasyBlogHelper::activityLog( $activity );
  277. }
  278. return $state;
  279. }
  280. }