PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_zoo/tables/item.php

https://github.com/shlomsky/ivo
PHP | 492 lines | 230 code | 82 blank | 180 comment | 25 complexity | abf3bb4cf8d64dfefeab8379aecba95f MD5 | raw file
  1. <?php
  2. /**
  3. * @package ZOO Component
  4. * @file item.php
  5. * @version 2.3.0 December 2010
  6. * @author YOOtheme http://www.yootheme.com
  7. * @copyright Copyright (C) 2007 - 2011 YOOtheme GmbH
  8. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  9. */
  10. /*
  11. Class: TableApplication
  12. The table class for items.
  13. */
  14. class ItemTable extends YTable {
  15. protected function __construct() {
  16. parent::__construct('Item', ZOO_TABLE_ITEM, 'id');
  17. }
  18. /*
  19. Function: save
  20. Override. Save object to database table.
  21. Returns:
  22. Boolean.
  23. */
  24. public function save($object) {
  25. if (!is_string($object->type) || empty($object->type)) {
  26. throw new ItemTableException('Invalid type id');
  27. }
  28. if ($object->name == '') {
  29. throw new ItemTableException('Invalid name');
  30. }
  31. if ($object->alias == '' || $object->alias != YString::sluggify($object->alias)) {
  32. throw new ItemTableException('Invalid slug');
  33. }
  34. if (ItemHelper::checkAliasExists($object->alias, $object->id)) {
  35. throw new ItemTableException('Alias already exists, please choose a unique alias');
  36. }
  37. // first save to get id
  38. if (empty($object->id)) {
  39. parent::save($object);
  40. }
  41. // init vars
  42. $db = $this->getDBO();
  43. $search_data = array();
  44. $element_data = array();
  45. foreach ($object->getElements() as $id => $element) {
  46. // get element data
  47. $element_data[] = $element->toXML();
  48. // get search data
  49. if ($data = $element->getSearchData()) {
  50. $search_data[] = "(".$object->id.", ".$db->quote($id).", ".$db->quote($data).")";
  51. }
  52. }
  53. // set element data
  54. $object->elements = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<elements>\n".implode("\n", $element_data)."\n</elements>";
  55. // delete old search data
  56. $query = "DELETE FROM ".ZOO_TABLE_SEARCH
  57. ." WHERE item_id = ".(int) $object->id;
  58. $db->query($query);
  59. // insert new search data
  60. if (count($search_data)) {
  61. $query = "INSERT INTO ".ZOO_TABLE_SEARCH
  62. ." VALUES ".implode(", ", $search_data);
  63. $db->query($query);
  64. }
  65. // save tags
  66. YTable::getInstance('tag')->save($object->id, $object->getTags());
  67. return parent::save($object);
  68. }
  69. /*
  70. Function: delete
  71. Override. Delete object from database table.
  72. Returns:
  73. Boolean.
  74. */
  75. public function delete($object) {
  76. // get database
  77. $db = $this->getDBO();
  78. // delete item to category relations
  79. $query = "DELETE FROM ".ZOO_TABLE_CATEGORY_ITEM
  80. ." WHERE item_id = ".(int) $object->id;
  81. $db->query($query);
  82. // delete related comments
  83. $query = "DELETE FROM ".ZOO_TABLE_COMMENT
  84. ." WHERE item_id = ".(int) $object->id;
  85. $db->query($query);
  86. // delete related search data rows
  87. $query = "DELETE FROM ".ZOO_TABLE_SEARCH
  88. ." WHERE item_id = ". (int) $object->id;
  89. $db->query($query);
  90. // delete related rating data rows
  91. $query = "DELETE FROM ".ZOO_TABLE_RATING
  92. ." WHERE item_id = ". (int) $object->id;
  93. $db->query($query);
  94. return parent::delete($object);
  95. }
  96. /*
  97. Function: hit
  98. Increment item hits.
  99. Returns:
  100. Boolean.
  101. */
  102. public function hit($object) {
  103. // get database
  104. $db = $this->getDBO();
  105. $key = $this->getKeyName();
  106. // increment hits
  107. if ($object->$key) {
  108. $query = "UPDATE ".$this->getTableName()
  109. ." SET hits = (hits + 1)"
  110. ." WHERE $key = ".(int) $object->$key;
  111. $db->query($query);
  112. $object->hits++;
  113. return true;
  114. }
  115. return false;
  116. }
  117. /*
  118. Function: getByType
  119. Method to get types related items.
  120. Parameters:
  121. $type_id - Type
  122. Returns:
  123. Array - Items
  124. */
  125. public function getByType($type_id, $application_id = false){
  126. // get database
  127. $db = $this->getDBO();
  128. $query = "SELECT a.*"
  129. ." FROM ".$this->getTableName()." AS a"
  130. ." WHERE a.type = ".$db->Quote($type_id)
  131. .($application_id !== false ? " AND a.application_id = ".(int) $application_id : "");
  132. return $this->_queryObjectList($query);
  133. }
  134. /*
  135. Function: getApplicationItemCount
  136. Method to get application related item count.
  137. Parameters:
  138. $application_id - Application id
  139. Returns:
  140. Int
  141. */
  142. public function getApplicationItemCount($application_id) {
  143. $query = "SELECT count(a.id) AS item_count"
  144. ." FROM ".$this->getTableName()." AS a"
  145. ." WHERE a.application_id = ".(int) $application_id;
  146. return (int) $this->_queryResult($query);
  147. }
  148. /*
  149. Function: getTypeItemCount
  150. Method to get types related item count.
  151. Parameters:
  152. $type - Type
  153. Returns:
  154. Int
  155. */
  156. public function getTypeItemCount($type){
  157. // get database
  158. $db = $this->getDBO();
  159. $group = $type->getApplication()->getGroup();
  160. $query = "SELECT count(a.id) AS item_count"
  161. ." FROM ".$this->getTableName()." AS a"
  162. ." JOIN ".ZOO_TABLE_APPLICATION." AS b ON a.application_id = b.id"
  163. ." WHERE a.type = ".$db->Quote($type->id)
  164. ." AND b.application_group = ".$db->Quote($group);
  165. return (int) $this->_queryResult($query);
  166. }
  167. /*
  168. Function: findAll
  169. Method to retrieve all items.
  170. Parameters:
  171. $application_id - Application id
  172. $published - Get published items only
  173. $user - check access level of this user, else current user is used
  174. $options - additional options
  175. Returns:
  176. Array - Array of items
  177. */
  178. public function findAll($application_id = false, $published = false, $user = null, $options = array()) {
  179. // get database
  180. $db = $this->getDBO();
  181. // get user from session, if not set
  182. if (empty($user)) {
  183. $user = JFactory::getUser();
  184. }
  185. // get user access id
  186. $access_id = $user->get('aid', 0);
  187. // get date
  188. $date = JFactory::getDate();
  189. $now = $db->Quote($date->toMySQL());
  190. $null = $db->Quote($db->getNullDate());
  191. // set query options
  192. $conditions =
  193. ($application_id !== false ? "application_id = ".(int) $application_id : "")
  194. ." AND access <= ".(int) $access_id
  195. .($published == true ? " AND state = 1"
  196. ." AND (publish_up = ".$null." OR publish_up <= ".$now.")"
  197. ." AND (publish_down = ".$null." OR publish_down >= ".$now.")": "");
  198. return $this->find('all', array_merge(compact('conditions'), $options));
  199. }
  200. /*
  201. Function: getByCharacter
  202. Method to retrieve all items starting with a certain character.
  203. Parameters:
  204. $application_id - Application id
  205. $char - Character(s)
  206. $not_in - Use not in for matching multiple characters
  207. Returns:
  208. Array - Array of items
  209. */
  210. public function getByCharacter($application_id, $char, $not_in = false, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0){
  211. // get database
  212. $db = $this->getDBO();
  213. // get user from session, if not set
  214. if (empty($user)) {
  215. $user = JFactory::getUser();
  216. }
  217. // get user access id
  218. $access_id = $user->get('aid', 0);
  219. $date = JFactory::getDate();
  220. $now = $db->Quote($date->toMySQL());
  221. $null = $db->Quote($db->getNullDate());
  222. // escape and quote character array
  223. if (is_array($char)) {
  224. foreach ($char as $key => $val) {
  225. $char[$key] = "'".$db->getEscaped($val)."'";
  226. }
  227. }
  228. $query = "SELECT a.*"
  229. ." FROM ".ZOO_TABLE_CATEGORY_ITEM." AS ci"
  230. ." JOIN ".$this->getTableName()." AS a ON a.id = ci.item_id"
  231. ." WHERE a.application_id = ".(int) $application_id
  232. ." AND a.access <= ".(int) $access_id
  233. .($published == true ? " AND a.state = 1"
  234. ." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
  235. ." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
  236. ." AND BINARY LOWER(LEFT(a.name, 1)) ".(is_array($char) ? ($not_in ? "NOT" : null)." IN (".implode(",", $char).")" : " = '".$db->getEscaped($char)."'")
  237. ." ORDER BY a.priority DESC ".($orderby != "" ? ", ".$orderby : "")
  238. .(($limit ? " LIMIT ".(int)$offset.",".(int)$limit : ""));
  239. return $this->_queryObjectList($query);
  240. }
  241. /*
  242. Function: getByTag
  243. Method to retrieve all items matching a certain tag.
  244. Parameters:
  245. $tag - Tag name
  246. $application_id - Application id
  247. Returns:
  248. Array - Array of items
  249. */
  250. public function getByTag($application_id, $tag, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0){
  251. // get database
  252. $db = $this->getDBO();
  253. // get user from session, if not set
  254. if (empty($user)) {
  255. $user = JFactory::getUser();
  256. }
  257. // get user access id
  258. $access_id = $user->get('aid', 0);
  259. // get dates
  260. $date = JFactory::getDate();
  261. $now = $db->Quote($date->toMySQL());
  262. $null = $db->Quote($db->getNullDate());
  263. $query = "SELECT a.*"
  264. ." FROM ".$this->getTableName()." AS a "
  265. ." LEFT JOIN ".ZOO_TABLE_TAG." AS b ON a.id = b.item_id"
  266. ." WHERE a.application_id = ".(int) $application_id
  267. ." AND b.name = '".$db->getEscaped($tag)."'"
  268. ." AND a.access <= ".(int) $access_id
  269. .($published == true ? " AND a.state = 1"
  270. ." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
  271. ." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
  272. ." GROUP BY a.id"
  273. ." ORDER BY a.priority DESC ".($orderby != "" ? ", ".$orderby : "")
  274. .(($limit ? " LIMIT ".(int)$offset.",".(int)$limit : ""));
  275. return $this->_queryObjectList($query);
  276. }
  277. /*
  278. Function: getFromCategory
  279. Method to retrieve all items of a category.
  280. Parameters:
  281. $category_id - Category id(s)
  282. Returns:
  283. Array - Array of items
  284. */
  285. public function getFromCategory($application_id, $category_id, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0){
  286. // get database
  287. $db = $this->getDBO();
  288. // get user from session, if not set
  289. if (empty($user)) {
  290. $user = JFactory::getUser();
  291. }
  292. // get user access id
  293. $access_id = $user->get('aid', 0);
  294. // get dates
  295. $date = JFactory::getDate();
  296. $now = $db->Quote($date->toMySQL());
  297. $null = $db->Quote($db->getNullDate());
  298. $query = "SELECT a.*"
  299. ." FROM ".$this->getTableName()." AS a"
  300. ." LEFT JOIN ".ZOO_TABLE_CATEGORY_ITEM." AS b ON a.id = b.item_id"
  301. ." WHERE a.application_id = ".(int) $application_id
  302. ." AND b.category_id ".(is_array($category_id) ? " IN (".implode(",", $category_id).")" : " = ".(int) $category_id)
  303. ." AND a.access <= ".(int) $access_id
  304. .($published == true ? " AND a.state = 1"
  305. ." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
  306. ." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
  307. ." GROUP BY a.id"
  308. ." ORDER BY a.priority DESC ".($orderby != "" ? ", ".$orderby : "")
  309. .(($limit ? " LIMIT ".(int)$offset.",".(int)$limit : ""));
  310. return $this->_queryObjectList($query);
  311. }
  312. /*
  313. Function: search
  314. Method to retrieve all items matching search data.
  315. Parameters:
  316. $search_string - the search string
  317. $app_id - specify an application id to limit the search scope
  318. Returns:
  319. Array - Array of items
  320. */
  321. public function search($search_string, $app_id = 0) {
  322. // get database
  323. $db = $this->getDBO();
  324. $db_search = $db->Quote('%'.$db->getEscaped( $search_string, true ).'%', false);
  325. $query = "SELECT a.*"
  326. ." FROM ".$this->getTableName()." AS a"
  327. ." LEFT JOIN ".ZOO_TABLE_SEARCH." AS b ON a.id = b.item_id"
  328. ." WHERE (LOWER(b.value) LIKE LOWER(" . $db_search . ")"
  329. ." OR LOWER(a.name) LIKE LOWER(" . $db_search . "))"
  330. . (empty($app_id) ? "" : " AND application_id = " . $app_id)
  331. ." AND a.searchable=1"
  332. ." GROUP BY a.id";
  333. return $this->_queryObjectList($query);
  334. }
  335. /*
  336. Function: searchElements
  337. Method to retrieve all items matching search data.
  338. Parameters:
  339. $elements_array - key = element_name, value = search string
  340. $app_id - specify an application id to limit the search scope
  341. Returns:
  342. Array - Array of items
  343. */
  344. public function searchElements($elements_array, $app_id = 0) {
  345. // get database
  346. $db = $this->getDBO();
  347. $i = 0;
  348. $join = array();
  349. $where = array();
  350. foreach ($elements_array as $name => $search_string) {
  351. $as = "table" . $i;
  352. $db_name = $db->Quote($db->getEscaped( $name, true ), false);
  353. $db_search = $db->Quote('%'.$db->getEscaped( $search_string, true ).'%', false);
  354. $join[] = " LEFT JOIN ".ZOO_TABLE_SEARCH." AS " . $as . " ON a.id = " . $as . ".item_id";
  355. $where[] = $as.".element_id = ".$db_name." AND LOWER(".$as.".value) LIKE LOWER(".$db_search.")";
  356. $i++;
  357. }
  358. $query = "SELECT a.*"
  359. ." FROM ".$this->getTableName()." AS a "
  360. . implode(" ", $join)
  361. ." WHERE "
  362. . implode(" AND ", $where)
  363. . (empty($app_id) ? "" : " AND application_id = " . $app_id)
  364. ." AND a.searchable=1"
  365. ." GROUP BY a.id";
  366. return $this->_queryObjectList($query);
  367. }
  368. /*
  369. Function: getUsers
  370. Method to get users of items
  371. Parameters:
  372. $app_id - Application id
  373. Returns:
  374. Array - Array of items
  375. */
  376. public function getUsers($app_id) {
  377. $query = 'SELECT DISTINCT u.id, u.name'
  378. .' FROM '.$this->getTableName().' AS i'
  379. .' JOIN #__users AS u ON i.created_by = u.id'
  380. . ((empty($app_id)) ? "" : " WHERE i.application_id = ".$app_id);
  381. return $this->getDBO()->queryObjectList($query, 'id');
  382. }
  383. }
  384. /*
  385. Class: ItemTableException
  386. */
  387. class ItemTableException extends YException {}