Перейти к содержимому


Фотография
- - - - -

[Оптимизация] Zoo под большие контентные сайты и не только ¯\_(ツ)_/¯

zoo оптимизация костыль денис_молодец женя_тоже_ничего forumlvlup likeaboss никто_не_читает_теги

Сообщений в теме: 12

#11 CB9TOIIIA

CB9TOIIIA

Отправлено 13 March 2017 - 14:25

Не уверен, но возможно и другие хаки в файле
  • 0

#12 Шингисович

Шингисович

Отправлено 13 March 2017 - 16:06

CB9TOIIIA сказал(а) 13 Мар 2017 - 13:25:

Не уверен, но возможно и другие хаки в файле

 

Точно. Оказалось, было отключено и подсчитывание хитов, а это для проекта моего нужное дело.

 

в итоге нужный мне файл подправил и теперь он считает хиты, решая вопрос основного пункта оптимизации по части базы данных:

  1. <?php
  2. /**
  3. * @package com_zoo
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8.  
  9. /*
  10. Class: ItemTable
  11. The table class for items.
  12. */
  13. class ItemTable extends AppTable {
  14.  
  15. public function __construct($app) {
  16. parent::__construct($app, ZOO_TABLE_ITEM);
  17. }
  18.  
  19. protected function _initObject($object) {
  20.  
  21. parent::_initObject($object);
  22.  
  23. // workaround for php bug, which calls constructor before filling values
  24. if (is_string($object->params) || is_null($object->params)) {
  25. // decorate data as object
  26. $object->params = $this->app->parameter->create($object->params);
  27. }
  28.  
  29. if (is_string($object->elements) || is_null($object->elements)) {
  30. // decorate data as object
  31. $object->elements = $this->app->data->create($object->elements);
  32. }
  33.  
  34. // add to cache
  35. $key_name = $this->key;
  36.  
  37. if ($object->$key_name && !key_exists($object->$key_name, $this->_objects)) {
  38. $this->_objects[$object->$key_name] = $object;
  39. }
  40.  
  41. // trigger init event
  42. $this->app->event->dispatcher->notify($this->app->event->create($object, 'item:init'));
  43.  
  44. return $object;
  45. }
  46.  
  47. /*
  48. Function: save
  49. Override. Save object to database table.
  50.  
  51. Returns:
  52. Boolean.
  53. */
  54. public function save($object) {
  55.  
  56. if (!($application = $object->getApplication())) {
  57. throw new ItemTableException('Invalid application id');
  58. }
  59.  
  60. if (!is_string($object->type) || empty($object->type)) {
  61. throw new ItemTableException('Invalid type id');
  62. }
  63.  
  64. if ($object->name == '') {
  65. throw new ItemTableException('Invalid name');
  66. }
  67.  
  68. if ($object->alias == '' || $object->alias != $this->app->string->sluggify($object->alias)) {
  69. throw new ItemTableException('Invalid slug');
  70. }
  71.  
  72. if ($this->app->alias->item->checkAliasExists($object->alias, $object->id)) {
  73. throw new ItemTableException('Alias already exists, please choose a unique alias');
  74. }
  75.  
  76. $new = !(bool) $object->id;
  77.  
  78. // first save to get id
  79. if (empty($object->id)) {
  80. parent::save($object);
  81. }
  82.  
  83. // init vars
  84. $db = $this->database;
  85. $search_data = array();
  86.  
  87. foreach ($object->getElements() as $id => $element) {
  88. // get search data
  89. if ($data = $element->getSearchData()) {
  90. $search_data[] = "(".$object->id.", ".$db->quote($id).", ".$db->quote($data).")";
  91. }
  92. }
  93.  
  94. // delete old search data
  95. $query = "DELETE FROM ".ZOO_TABLE_SEARCH
  96. ." WHERE item_id = ".(int) $object->id;
  97. $db->query($query);
  98.  
  99. // insert new search data
  100. if (count($search_data)) {
  101. $query = "INSERT INTO ".ZOO_TABLE_SEARCH
  102. ." VALUES ".implode(", ", $search_data);
  103. $db->query($query);
  104. }
  105.  
  106. // save tags
  107. $this->app->table->tag->save($object);
  108.  
  109. $result = parent::save($object);
  110.  
  111. // trigger save event
  112. $this->app->event->dispatcher->notify($this->app->event->create($object, 'item:saved', compact('new')));
  113.  
  114. return $result;
  115. }
  116.  
  117. /*
  118. Function: delete
  119. Override. Delete object from database table.
  120.  
  121. Returns:
  122. Boolean.
  123. */
  124. public function delete($object) {
  125.  
  126. // Check ACL
  127. if (!$object->canDelete()) {
  128. throw new ItemTableException('Invalid Access Permission');
  129. }
  130.  
  131. // get database
  132. $db = $this->database;
  133.  
  134. // delete item to category relations
  135. $query = "DELETE FROM ".ZOO_TABLE_CATEGORY_ITEM
  136. ." WHERE item_id = ".(int) $object->id;
  137. $db->query($query);
  138.  
  139. // delete related comments
  140. $query = "DELETE FROM ".ZOO_TABLE_COMMENT
  141. ." WHERE item_id = ".(int) $object->id;
  142. $db->query($query);
  143.  
  144. // delete related search data rows
  145. $query = "DELETE FROM ".ZOO_TABLE_SEARCH
  146. ." WHERE item_id = ". (int) $object->id;
  147. $db->query($query);
  148.  
  149. // delete related rating data rows
  150. $query = "DELETE FROM ".ZOO_TABLE_RATING
  151. ." WHERE item_id = ". (int) $object->id;
  152. $db->query($query);
  153.  
  154. // delete related tag data rows
  155. $query = "DELETE FROM ".ZOO_TABLE_TAG
  156. ." WHERE item_id = ". (int) $object->id;
  157. $db->query($query);
  158.  
  159. $result = parent::delete($object);
  160.  
  161. // trigger deleted event
  162. $this->app->event->dispatcher->notify($this->app->event->create($object, 'item:deleted'));
  163.  
  164. return $result;
  165. }
  166.  
  167. /*
  168. Function: hit
  169. Increment item hits.
  170.  
  171. Returns:
  172. Boolean.
  173. */
  174. public function hit($object) {
  175.  
  176. // get database
  177. $db = $this->database;
  178. $key = $this->key;
  179.  
  180. // increment hits
  181. if ($object->$key) {
  182. $query = "UPDATE ".$this->name
  183. ." SET hits = (hits + 1)"
  184. ." WHERE $key = ".(int) $object->$key;
  185. $db->query($query);
  186. $object->hits++;
  187. return true;
  188. }
  189.  
  190. return false;
  191. }
  192.  
  193. /*
  194. Function: getApplicationItemCount
  195. Method to get application related item count.
  196.  
  197. Parameters:
  198. $application_id - Application id
  199.  
  200. Returns:
  201. Int
  202. */
  203. public function getApplicationItemCount($application_id) {
  204. $query = "SELECT count(a.id) AS item_count"
  205. ." FROM ".$this->name." AS a"
  206. ." WHERE a.application_id = ".(int) $application_id;
  207.  
  208. return (int) $this->_queryResult($query);
  209. }
  210.  
  211. /*
  212. Function: getTypeItemCount
  213. Method to get types related item count.
  214.  
  215. Parameters:
  216. $type - Type
  217.  
  218. Returns:
  219. Int
  220. */
  221. public function getTypeItemCount($type){
  222.  
  223. // get database
  224. $db = $this->database;
  225.  
  226. $group = $type->getApplication()->getGroup();
  227. $query = "SELECT count(a.id) AS item_count"
  228. ." FROM ".$this->name." AS a"
  229. ." JOIN ".ZOO_TABLE_APPLICATION." AS b ON a.application_id = b.id"
  230. ." WHERE a.type = ".$db->Quote($type->id)
  231. ." AND b.application_group = ".$db->Quote($group);
  232.  
  233. return (int) $this->_queryResult($query);
  234.  
  235. }
  236.  
  237. /*
  238. @deprecated version 2.5 beta
  239. */
  240. public function findAll($application_id = false, $published = false, $user = null, $options = array()) {
  241.  
  242. // get database
  243. $db = $this->database;
  244.  
  245. // get date
  246. $date = $this->app->date->create();
  247. $now = $db->Quote($date->toSQL());
  248. $null = $db->Quote($db->getNullDate());
  249.  
  250. // set query options
  251. $conditions =
  252. ($application_id !== false ? "application_id = ".(int) $application_id : "")
  253. ." AND ".$this->app->user->getDBAccessString($user)
  254. .($published == true ? " AND state = 1" : "");
  255. //." AND (publish_up = ".$null." OR publish_up <= ".$now.")"
  256. //." AND (publish_down = ".$null." OR publish_down >= ".$now.")": "");
  257.  
  258. return $this->all(array_merge(compact('conditions'), $options));
  259. }
  260.  
  261. /*
  262. Function: getByCharacter
  263. Method to retrieve all items starting with a certain character.
  264.  
  265. Parameters:
  266. $application_id - Application id
  267. $char - Character(s)
  268. $not_in - Use not in for matching multiple characters
  269.  
  270. Returns:
  271. Array - Array of items
  272. */
  273. public function getByIds($ids, $published = false, $user = null, $orderby = ''){
  274.  
  275. $ids = (array) $ids;
  276.  
  277. if (empty($ids)) {
  278. return array();
  279. }
  280.  
  281. // get database
  282. $db = $this->database;
  283.  
  284. // get dates
  285. $date = $this->app->date->create();
  286. $now = $db->Quote($date->toSQL());
  287. $null = $db->Quote($db->getNullDate());
  288.  
  289. // get item ordering
  290. list($join, $order) = $this->_getItemOrder($orderby);
  291.  
  292. $query = "SELECT a.*"
  293. ." FROM ".$this->name." AS a"
  294. .($join ? $join : "")
  295. ." WHERE a.id IN (".implode(",", $ids).")"
  296. ." AND a.".$this->app->user->getDBAccessString($user)
  297. .($published == true ? " AND a.state = 1" : "")
  298. //." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
  299. //." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
  300. ." ORDER BY a.priority DESC".($order ? ", $order" : "");
  301.  
  302. return $this->_queryObjectList($query);
  303. }
  304.  
  305. /*
  306. Function: getByCharacter
  307. Method to retrieve all items starting with a certain character.
  308.  
  309. Parameters:
  310. $application_id - Application id
  311. $char - Character(s)
  312. $not_in - Use not in for matching multiple characters
  313. $published
  314. $user
  315. $orderby
  316. $offset
  317. $limit
  318.  
  319. Returns:
  320. Array - Array of items
  321. */
  322. public function getByCharacter($application_id, $char, $not_in = false, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0){
  323.  
  324. // get database
  325. $db = $this->database;
  326.  
  327. // get dates
  328. $date = $this->app->date->create();
  329. $now = $db->Quote($date->toSQL());
  330. $null = $db->Quote($db->getNullDate());
  331.  
  332. // escape and quote character array
  333. if (is_array($char)) {
  334. foreach ($char as $key => $val) {
  335. $char[$key] = "'".$db->escape($val)."'";
  336. }
  337. }
  338.  
  339. // get item ordering
  340. list($join, $order) = $this->_getItemOrder($orderby);
  341.  
  342. $query = "SELECT a.*"
  343. ." FROM ".ZOO_TABLE_CATEGORY_ITEM." AS ci"
  344. ." JOIN ".$this->name." AS a ON a.id = ci.item_id"
  345. .($join ? $join : "")
  346. ." WHERE a.application_id = ".(int) $application_id
  347. ." AND a.".$this->app->user->getDBAccessString($user)
  348. .($published == true ? " AND a.state = 1" : "")
  349. //." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
  350. //." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
  351. ." AND BINARY LOWER(LEFT(a.name, 1)) ".(is_array($char) ? ($not_in ? "NOT" : null)." IN (".implode(",", $char).")" : " = '".$db->escape($char)."'")
  352. ." ORDER BY a.priority DESC".($order ? ", $order" : "")
  353. .(($limit ? " LIMIT ".(int) $offset.",".(int) $limit : ""));
  354.  
  355. return $this->_queryObjectList($query);
  356. }
  357.  
  358. /*
  359. Function: getByTag
  360. Method to retrieve all items matching a certain tag.
  361.  
  362. Parameters:
  363. $tag - Tag name
  364. $application_id - Application id
  365.  
  366. Returns:
  367. Array - Array of items
  368. */
  369. public function getByTag($application_id, $tag, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0){
  370.  
  371. // get database
  372. $db = $this->database;
  373.  
  374. // get dates
  375. $date = $this->app->date->create();
  376. $now = $db->Quote($date->toSQL());
  377. $null = $db->Quote($db->getNullDate());
  378.  
  379. // get item ordering
  380. list($join, $order) = $this->_getItemOrder($orderby);
  381.  
  382. $query = "SELECT a.*"
  383. ." FROM ".$this->name." AS a "
  384. ." LEFT JOIN ".ZOO_TABLE_TAG." AS b ON a.id = b.item_id"
  385. .($join ? $join : "")
  386. ." WHERE a.application_id = ".(int) $application_id
  387. ." AND b.name = '".$db->escape($tag)."'"
  388. ." AND a.".$this->app->user->getDBAccessString($user)
  389. .($published == true ? " AND a.state = 1" : "")
  390. //." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
  391. //." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
  392. ." GROUP BY a.id"
  393. ." ORDER BY a.priority DESC".($order ? ", $order" : "")
  394. .(($limit ? " LIMIT ".(int) $offset.",".(int) $limit : ""));
  395.  
  396. return $this->_queryObjectList($query);
  397. }
  398.  
  399. /*
  400. Function: getByType
  401. Method to get types related items.
  402.  
  403. Parameters:
  404. $type_id - Type
  405.  
  406. Returns:
  407. Array - Items
  408. */
  409. public function getByType($type_id, $application_id = false, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0){
  410.  
  411. // get database
  412. $db = $this->database;
  413.  
  414. // get dates
  415. $date = $this->app->date->create();
  416. $now = $db->Quote($date->toSQL());
  417. $null = $db->Quote($db->getNullDate());
  418.  
  419. // get item ordering
  420. list($join, $order) = $this->_getItemOrder($orderby);
  421.  
  422. $query = "SELECT a.*"
  423. ." FROM ".$this->name." AS a"
  424. .($join ? $join : "")
  425. ." WHERE a.type = ".$db->Quote($type_id)
  426. .($application_id !== false ? " AND a.application_id = ".(int) $application_id : "")
  427. ." AND a.".$this->app->user->getDBAccessString($user)
  428. .($published == true ? " AND a.state = 1" : "")
  429. //." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
  430. //." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
  431. ." GROUP BY a.id"
  432. // ." ORDER BY a.priority DESC"
  433. ." ORDER BY a.priority DESC".($order ? ", $order" : "")
  434. .(($limit ? " LIMIT ".(int) $offset.",".(int) $limit : ""));
  435.  
  436. return $this->_queryObjectList($query);
  437. }
  438.  
  439. /*
  440. Function: getByCategory
  441. Method to retrieve all items of a category.
  442.  
  443. Parameters:
  444. $category_id - Category id(s)
  445.  
  446. Returns:
  447. Array - Array of items
  448. */
  449. public function getByCategory($application_id, $category_id, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0, $ignore_order_priority = false) {
  450.  
  451. // get database
  452. $db = $this->database;
  453.  
  454. // get dates
  455. $date = $this->app->date->create();
  456. $now = $db->Quote($date->toSQL());
  457. $null = $db->Quote($db->getNullDate());
  458.  
  459. // get item ordering
  460. list($join, $order) = $this->_getItemOrder($orderby, $ignore_order_priority);
  461.  
  462. $query = "SELECT a.id"
  463. ." FROM ".$this->name." AS a"
  464. ." LEFT JOIN ".ZOO_TABLE_CATEGORY_ITEM." AS b ON a.id = b.item_id"
  465. .($join ? $join : "")
  466. ." WHERE a.application_id = ".(int) $application_id
  467. ." AND a.".$this->app->user->getDBAccessString($user)
  468. .($published == true ? " AND a.state = 1"
  469. ." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
  470. ." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
  471. ." AND b.category_id ".(is_array($category_id) ? " IN (".implode(",", $category_id).")" : " = ".(int) $category_id)
  472. ." GROUP BY a.id"
  473. .($order ? " ORDER BY " . $order : "")
  474. .($limit ? " LIMIT ".(int) $offset.",".(int) $limit : "");
  475.  
  476. $list = $this->app->database->queryAssocList($query);
  477. $itemOrig = array_reduce($list, function($acc, $item){
  478. $acc[] = $item['id'];
  479. return $acc;
  480. }, []);
  481.  
  482. $list = JBModelItem::model()->getZooItemsByIds($itemOrig);
  483.  
  484. $list = $this->app->jbarray->sortByArray($list, $itemOrig);
  485.  
  486. return $list;
  487. }
  488.  
  489.  
  490. /**
  491. * Gets adjacent items
  492. *
  493. * @return array first value is previous item, second value is next item
  494. */
  495. public function getPrevNext($application_id, $category_id, $item_id, $published = false, $user = null, $orderby = "", $ignore_order_priority = false) {
  496.  
  497. // init vars
  498. $prev = null;
  499. $next = null;
  500.  
  501. // get database
  502. $db = $this->database;
  503.  
  504. // get dates
  505. $date = $this->app->date->create();
  506. $now = $db->Quote($date->toSQL());
  507. $null = $db->Quote($db->getNullDate());
  508.  
  509. // get item ordering
  510. list($join, $order) = $this->_getItemOrder($orderby);
  511.  
  512. $query = "SELECT ROWNUM"
  513. ." FROM ("
  514. ."SELECT @rownum:= @rownum+1 ROWNUM, id"
  515. ." FROM"
  516. ." (SELECT @rownum:=0) r,"
  517. ." (SELECT id"
  518. ." FROM ".$this->name." AS a"
  519. ." LEFT JOIN ".ZOO_TABLE_CATEGORY_ITEM." AS b ON a.id = b.item_id"
  520. .($join ? $join : "")
  521. ." WHERE a.application_id = ".(int) $application_id
  522. ." AND b.category_id ".(is_array($category_id) ? " IN (".implode(",", $category_id).")" : " = ".(int) $category_id)
  523. ." AND a.".$this->app->user->getDBAccessString($user)
  524. .($published == true ? " AND a.state = 1" : "")
  525. //." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
  526. //." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
  527. ." GROUP BY a.id"
  528. ." ORDER BY ". (!$ignore_order_priority ? "a.priority DESC, " : "") . $order
  529. .") t1"
  530. .") t2"
  531. ." WHERE id = ".(int) $item_id;
  532.  
  533. if ($row = $this->_queryResult($query)) {
  534.  
  535. $query = "SELECT ROWNUM, id"
  536. ." FROM ("
  537. ."SELECT @rownum:= @rownum+1 ROWNUM, id"
  538. ." FROM"
  539. ." (SELECT @rownum:=0) r,"
  540. ." (SELECT id"
  541. ." FROM ".$this->name." AS a"
  542. ." LEFT JOIN ".ZOO_TABLE_CATEGORY_ITEM." AS b ON a.id = b.item_id"
  543. .($join ? $join : "")
  544. ." WHERE a.application_id = ".(int) $application_id
  545. ." AND b.category_id ".(is_array($category_id) ? " IN (".implode(",", $category_id).")" : " = ".(int) $category_id)
  546. ." AND a.".$this->app->user->getDBAccessString($user)
  547. .($published == true ? " AND a.state = 1" : "")
  548. //." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
  549. //." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
  550. ." GROUP BY a.id"
  551. ." ORDER BY ". (!$ignore_order_priority ? "a.priority DESC, " : "") . $order
  552. .") t1"
  553. .") t2"
  554. ." WHERE ROWNUM = ".((int) $row-1) ." OR ROWNUM = ".((int) $row+1);
  555.  
  556. if ($objects = $db->queryObjectList($query)) {
  557. foreach ($objects as $object) {
  558. if ($object->ROWNUM > $row) {
  559. $next = $this->get($object->id);
  560. } else {
  561. $prev = $this->get($object->id);
  562. }
  563. }
  564. }
  565. }
  566.  
  567. return array($prev, $next);
  568.  
  569. }
  570.  
  571. public function getByUser($application_id, $user_id, $type = null, $search = null, $order = null, $offset = null, $limit = null, $published = false) {
  572.  
  573. $options = $this->_getByUserOptions($application_id, $user_id, $type, $search, $published);
  574.  
  575. // Order
  576. $orders = array(
  577. 'date' => 'a.created ASC',
  578. 'rdate' => 'a.created DESC',
  579. 'alpha' => 'a.name ASC',
  580. 'ralpha' => 'a.name DESC',
  581. 'hits' => 'a.hits DESC',
  582. 'rhits' => 'a.hits ASC');
  583.  
  584. $options['order'] = ($order && isset($orders[$order])) ? $orders[$order] : $orders['rdate'];
  585.  
  586. $options = $limit ? array_merge($options, array('offset' => $offset, 'limit' => $limit)) : $options;
  587.  
  588. return $this->all($options);
  589. }
  590.  
  591. public function getItemCountByUser($application_id, $user_id, $type = null, $search = null, $published = false) {
  592. return $this->count(array_merge($this->_getByUserOptions($application_id, $user_id, $type, $search, $published), array('select' => 'a.id')));
  593. }
  594.  
  595. protected function _getByUserOptions($application_id, $user_id, $type = null, $search = null, $published = false) {
  596.  
  597. // select
  598. $select = 'a.*';
  599.  
  600. // get from
  601. $from = $this->name.' AS a';
  602.  
  603. // get data from the table
  604. $where = array();
  605.  
  606. // application filter
  607. $where[] = 'a.application_id = ' . (int) $application_id;
  608.  
  609. // author filter
  610. $where[] = 'a.created_by = ' . (int) $user_id;
  611.  
  612. // user rights
  613. $where[] = 'a.'.$this->app->user->getDBAccessString($this->app->user->get((int) $user_id));
  614.  
  615. // published
  616. if ($published) {
  617.  
  618. $db = $this->database;
  619.  
  620. // get dates
  621. $date = $this->app->date->create();
  622. $now = $db->Quote($date->toSQL());
  623. $null = $db->Quote($db->getNullDate());
  624.  
  625. // Add filters for publishing
  626. $where[] = 'a.state = 1';
  627. //$where[] = "(a.publish_up = ".$null." OR a.publish_up <= ".$now.")";
  628. //$where[] = "(a.publish_down = ".$null." OR a.publish_down >= ".$now.")";
  629. }
  630.  
  631. // Type
  632. if ($type) {
  633. if (is_array($type)) {
  634. $where[] = 'a.type IN ("' . implode('", "', array_keys($type)) . '")';
  635. } else {
  636. $where[] = 'a.type = "' . (string) $type . '"';
  637. }
  638. }
  639.  
  640. // Search
  641. if ($search) {
  642. $from .= ' LEFT JOIN '.ZOO_TABLE_TAG.' AS t ON a.id = t.item_id';
  643. $where[] = '(LOWER(a.name) LIKE '.$this->app->database->Quote('%'.$this->app->database->escape($search, true).'%', false)
  644. . ' OR LOWER(t.name) LIKE '.$this->app->database->Quote('%'.$this->app->database->escape($search, true).'%', false).')';
  645. }
  646.  
  647. // conditions
  648. $conditions = array(implode(' AND ', $where));
  649.  
  650. return compact('select', 'from', 'conditions');
  651.  
  652. }
  653.  
  654. /*
  655. @deprecated ZOO version 2.5 beta, use getByCategory instead
  656. */
  657. public function getFromCategory($application_id, $category_id, $published = false, $user = null, $orderby = "", $offset = 0, $limit = 0) {
  658. return $this->getByCategory($application_id, $category_id, $published, $user, $orderby, $offset, $limit);
  659. }
  660.  
  661. /*
  662. Function: getItemCountFromCategory
  663. Method to retrieve items count of a category.
  664.  
  665. Parameters:
  666. $category_id - Category id(s)
  667.  
  668. Returns:
  669. Array - Array of items
  670. */
  671. public function getItemCountFromCategory($application_id, $category_id, $published = false, $user = null){
  672.  
  673. // get database
  674. $db = $this->database;
  675.  
  676. // get dates
  677. $date = $this->app->date->create();
  678. $now = $db->Quote($date->toSQL());
  679. $null = $db->Quote($db->getNullDate());
  680.  
  681. $query = "SELECT a.id"
  682. ." FROM ".$this->name." AS a"
  683. ." LEFT JOIN ".ZOO_TABLE_CATEGORY_ITEM." AS b ON a.id = b.item_id"
  684. ." WHERE a.application_id = ".(int) $application_id
  685. ." AND b.category_id ".(is_array($category_id) ? " IN (".implode(",", $category_id).")" : " = ".(int) $category_id)
  686. ." AND a.".$this->app->user->getDBAccessString($user)
  687. .($published == true ? " AND a.state = 1" : "")
  688. //." AND (a.publish_up = ".$null." OR a.publish_up <= ".$now.")"
  689. //." AND (a.publish_down = ".$null." OR a.publish_down >= ".$now.")": "")
  690. ." GROUP BY a.id";
  691.  
  692. $db->query($query);
  693.  
  694. return $db->getNumRows();
  695.  
  696. }
  697.  
  698. /*
  699. Function: search
  700. Method to retrieve all items matching search data.
  701.  
  702. Parameters:
  703. $search_string - the search string
  704. $app_id - specify an application id to limit the search scope
  705.  
  706. Returns:
  707. Array - Array of items
  708. */
  709. public function search($search_string, $app_id = 0) {
  710.  
  711. // get database
  712. $db = $this->database;
  713. $db_search = $db->Quote('%'.$db->escape( $search_string, true ).'%', false);
  714.  
  715. $query = "SELECT a.*"
  716. ." FROM ".$this->name." AS a"
  717. ." LEFT JOIN ".ZOO_TABLE_SEARCH." AS b ON a.id = b.item_id"
  718. ." WHERE (LOWER(b.value) LIKE LOWER(" . $db_search . ")"
  719. ." OR LOWER(a.name) LIKE LOWER(" . $db_search . "))"
  720. . (empty($app_id) ? "" : " AND application_id = " . $app_id)
  721. ." AND a.searchable=1"
  722. ." GROUP BY a.id";
  723.  
  724. return $this->_queryObjectList($query);
  725. }
  726.  
  727. /*
  728. Function: searchElements
  729. Method to retrieve all items matching search data.
  730.  
  731. Parameters:
  732. $elements_array - key = element_name, value = search string
  733. $app_id - specify an application id to limit the search scope
  734.  
  735. Returns:
  736. Array - Array of items
  737. */
  738. public function searchElements($elements_array, $app_id = 0) {
  739.  
  740. // get database
  741. $db = $this->database;
  742.  
  743. $i = 0;
  744. $join = array();
  745. $where = array();
  746. foreach ($elements_array as $name => $search_string) {
  747. $as = "table" . $i;
  748. $db_name = $db->Quote($db->escape( $name, true ), false);
  749. $db_search = $db->Quote('%'.$db->escape( $search_string, true ).'%', false);
  750. $join[] = " LEFT JOIN ".ZOO_TABLE_SEARCH." AS " . $as . " ON a.id = " . $as . ".item_id";
  751. $where[] = $as.".element_id = ".$db_name." AND LOWER(".$as.".value) LIKE LOWER(".$db_search.")";
  752. $i++;
  753. }
  754.  
  755. $query = "SELECT a.*"
  756. ." FROM ".$this->name." AS a "
  757. . implode(" ", $join)
  758. ." WHERE "
  759. . implode(" AND ", $where)
  760. . (empty($app_id) ? "" : " AND application_id = " . $app_id)
  761. ." AND a.searchable=1"
  762. ." GROUP BY a.id";
  763.  
  764. return $this->_queryObjectList($query);
  765. }
  766.  
  767. /*
  768. Function: getUsers
  769. Method to get users of items
  770.  
  771. Parameters:
  772. $app_id - Application id
  773.  
  774. Returns:
  775. Array - Array of items
  776. */
  777. public function getUsers($app_id) {
  778. $query = 'SELECT DISTINCT u.id, u.name'
  779. .' FROM '.$this->name.' AS i'
  780. .' JOIN #__users AS u ON i.created_by = u.id'
  781. . ((empty($app_id)) ? "" : " WHERE i.application_id = ".$app_id);
  782.  
  783. return $this->database->queryObjectList($query, 'id');
  784. }
  785.  
  786. /*
  787. @deprecated version 2.5.5, use parents has() function
  788. */
  789. public function isInitialized($key) {
  790. return $this->has($key);
  791. }
  792.  
  793. protected function _getItemOrder($order) {
  794.  
  795. // if string, try to convert ordering
  796. if (is_string($order)) {
  797. $order = $this->app->itemorder->convert($order);
  798. }
  799.  
  800. $result = array(null, null);
  801. $order = (array) $order;
  802.  
  803. // remove empty and duplicate values
  804. $order = array_unique(array_filter($order));
  805.  
  806. // if random return immediately
  807. if (in_array('_random', $order)) {
  808. $result[1] = 'RAND()';
  809. return $result;
  810. }
  811.  
  812. // get order dir
  813. if (($index = array_search('_reversed', $order)) !== false) {
  814. $reversed = 'DESC';
  815. unset($order[$index]);
  816. } else {
  817. $reversed = 'ASC';
  818. }
  819.  
  820. // get ordering type
  821. $alphanumeric = false;
  822. if (($index = array_search('_alphanumeric', $order)) !== false) {
  823. $alphanumeric = true;
  824. unset($order[$index]);
  825. }
  826.  
  827. // set default ordering attribute
  828. if (empty($order)) {
  829. $order[] = '_itemname';
  830. }
  831.  
  832. // if there is a none core element present, ordering will only take place for those elements
  833. if (count($order) > 1) {
  834. $order = array_filter($order, create_function('$a', 'return strpos($a, "_item") === false;'));
  835. }
  836.  
  837. // order by core attribute
  838. foreach ($order as $element) {
  839. if (strpos($element, '_item') === 0) {
  840. $var = str_replace('_item', '', $element);
  841. if ($alphanumeric) {
  842. $result[1] = $reversed == 'ASC' ? "a.$var+0<>0 DESC, a.$var+0, a.$var" : "a.$var+0<>0, a.$var+0 DESC, a.$var DESC";
  843. } else {
  844. $result[1] = $reversed == 'ASC' ? "a.$var" : "a.$var DESC";
  845. }
  846.  
  847. }
  848. }
  849.  
  850. // else order by elements
  851. if (!isset($result[1])) {
  852. $result[0] = " LEFT JOIN ".ZOO_TABLE_SEARCH." AS s ON a.id = s.item_id AND s.element_id IN ('".implode("', '", $order)."')";
  853. if ($alphanumeric) {
  854. $result[1] = $reversed == 'ASC' ? "ISNULL(s.value), s.value+0<>0 DESC, s.value+0, s.value" : "s.value+0<>0, s.value+0 DESC, s.value DESC";
  855. } else {
  856. $result[1] = $reversed == 'ASC' ? "s.value" : "s.value DESC";
  857. }
  858. }
  859.  
  860. // trigger init event
  861. $this->app->event->dispatcher->notify($this->app->event->create($order, 'item:orderquery', array('result' => &$result)));
  862.  
  863. return $result;
  864.  
  865. }
  866.  
  867. }
  868.  
  869. /*
  870. Class: ItemTableException
  871. */
  872. class ItemTableException extends AppException {}

Чуток тяжелее стал. Спасибо за предостережение!


  • 1

Делаю сайты в Казахстане, Астане, webmarka.kz


#13 CB9TOIIIA

CB9TOIIIA

Отправлено 13 March 2017 - 21:18

Хиты - да там отключены: https://github.com/J...s/item.php#L168


  • 0





Темы с аналогичным тегами zoo, оптимизация, костыль, денис_молодец, женя_тоже_ничего, forumlvlup, likeaboss, никто_не_читает_теги

Click to return to top of page in style!