/lib/directory/dirlib.php

https://gitlab.com/ElvisAns/tiki · PHP · 863 lines · 572 code · 72 blank · 219 comment · 68 complexity · 7a4a6b0e07f01b3d754fb55f67bc0777 MD5 · raw file

  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. // \todo extract HTML from here !!
  8. //this script may only be included - so its better to die if called directly.
  9. if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
  10. header("location: index.php");
  11. exit;
  12. }
  13. /**
  14. *
  15. */
  16. class DirLib extends TikiLib
  17. {
  18. // Path functions
  19. /**
  20. * @param $categId
  21. * @return string
  22. */
  23. public function dir_get_category_path_admin($categId)
  24. {
  25. global $prefs;
  26. $info = $this->dir_get_category($categId);
  27. $path = '<a class="link" href="tiki-directory_admin_categories.php?parent=' . $info["categId"] . '">' . $info["name"] . '</a>';
  28. while ($info["parent"] != 0) {
  29. $info = $this->dir_get_category($info["parent"]);
  30. $path = '<a class="link" href="tiki-directory_admin_categories.php?parent=' . $info["categId"] . '">' . $info["name"] . '</a>' . $prefs['site_crumb_seper'] . $path;
  31. }
  32. return $path;
  33. }
  34. /**
  35. * @param $categId
  36. * @return string
  37. */
  38. public function dir_get_path_text($categId)
  39. {
  40. global $prefs;
  41. $info = $this->dir_get_category($categId);
  42. $path = $info["name"];
  43. while ($info["parent"] != 0) {
  44. $info = $this->dir_get_category($info["parent"]);
  45. $path = $info["name"] . $prefs['site_crumb_seper'] . $path;
  46. }
  47. return $path;
  48. }
  49. /**
  50. * @param $categId
  51. * @return string
  52. */
  53. public function dir_get_category_path_browse($categId)
  54. {
  55. global $prefs;
  56. $path = '';
  57. $info = $this->dir_get_category($categId);
  58. $path = '<a class="dirlink" href="tiki-directory_browse.php?parent=' . $info["categId"] . '">' . htmlspecialchars($info["name"]) . '</a>';
  59. while ($info["parent"] != 0) {
  60. $info = $this->dir_get_category($info["parent"]);
  61. $path = '<a class="dirlink" href="tiki-directory_browse.php?parent=' . $info["categId"] . '">' . htmlspecialchars($info["name"]) . '</a> ' . $prefs['site_crumb_seper'] . ' ' . $path;
  62. }
  63. return $path;
  64. }
  65. /**
  66. * @param $categId
  67. * @return array|null
  68. */
  69. public function dir_build_breadcrumb_trail($categId)
  70. {
  71. $crumbs = [];
  72. $info = $this->dir_get_category($categId);
  73. if (isset($info["name"])) {
  74. $crumbs[] = new Breadcrumb($info["name"], '', 'tiki-directory_browse.php?parent=' . $info["categId"], '', '');
  75. }
  76. while ($info["parent"] != 0) {
  77. $info = $this->dir_get_category($info["parent"]);
  78. $crumbs[] = new Breadcrumb($info["name"], '', 'tiki-directory_browse.php?parent=' . $info["categId"], '', '');
  79. }
  80. return empty($crumbs) ? null : array_reverse($crumbs);
  81. }
  82. // Stats functions
  83. // get stats (valid sites, invalid sites, categories, searches)
  84. // Functions to manage categories
  85. /**
  86. * @param $parent
  87. * @param $cant
  88. * @return array
  89. */
  90. public function get_random_subcats($parent, $cant)
  91. {
  92. //Return an array of 'cant' random subcategories
  93. $count = $this->getOne("select count(*) from `tiki_directory_categories` where `parent`=?", [(int)$parent]);
  94. if ($count < $cant) {
  95. $cant = $count;
  96. }
  97. $ret = [];
  98. while (count($ret) < $cant) {
  99. $x = mt_rand(0, $count);
  100. if (! in_array($x, $ret)) {
  101. $ret[] = $x;
  102. }
  103. }
  104. $ret = [];
  105. foreach ($ret as $r) {
  106. $query = "select * from `tiki_directory_categories`";
  107. $result = $this->query($query, [], 1, $r);
  108. $ret[] = $result->fetchRow();
  109. }
  110. return $ret;
  111. }
  112. // List
  113. /**
  114. * @param $parent
  115. * @param $offset
  116. * @param $maxRecords
  117. * @param $sort_mode
  118. * @param $find
  119. * @return array
  120. */
  121. public function dir_list_categories($parent, $offset, $maxRecords, $sort_mode, $find)
  122. {
  123. $bindvars = [(int)$parent];
  124. if ($find) {
  125. $findesc = '%' . $find . '%';
  126. $mid = " and (`name` like ? or `description` like ?)";
  127. $bindvars[] = $findesc;
  128. $bindvars[] = $findesc;
  129. } else {
  130. $mid = "";
  131. }
  132. $query = "select * from `tiki_directory_categories` where `parent`=? $mid order by " . $this->convertSortMode($sort_mode);
  133. $query_cant = "select count(*) from `tiki_directory_categories` where `parent`=? $mid";
  134. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  135. $cant = $this->getOne($query_cant, $bindvars);
  136. $ret = [];
  137. while ($res = $result->fetchRow()) {
  138. $res["sites"] = $this->getOne("select count(*) from `tiki_category_sites` where `categId`=?", [(int)$res["categId"]]);
  139. // TODO : Any permission to check? Used to verify view_categorized when categorized, what is the real permission?
  140. $ret[] = $res;
  141. }
  142. $retval = [];
  143. $retval["data"] = $ret;
  144. $retval["cant"] = $cant;
  145. return $retval;
  146. }
  147. // List all categories
  148. /**
  149. * @param $offset
  150. * @param $maxRecords
  151. * @param $sort_mode
  152. * @param $find
  153. * @return array
  154. */
  155. public function dir_list_all_categories($offset, $maxRecords, $sort_mode, $find)
  156. {
  157. $bindvars = [];
  158. if ($find) {
  159. $findesc = '%' . $find . '%';
  160. $mid = " where (`name` like ? or `description` like ?)";
  161. $bindvars[] = $findesc;
  162. $bindvars[] = $findesc;
  163. } else {
  164. $mid = "";
  165. }
  166. $query = "select * from `tiki_directory_categories` $mid order by " . $this->convertSortMode($sort_mode);
  167. $query_cant = "select count(*) from `tiki_directory_categories` $mid";
  168. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  169. $cant = $this->getOne($query_cant, $bindvars);
  170. $ret = [];
  171. while ($res = $result->fetchRow()) {
  172. $res["sites"] = $this->getOne("select count(*) from `tiki_category_sites` where `categId`=?", [(int)$res["categId"]]);
  173. //$res["path"]=$this->dir_get_path_text($res["categId"]);
  174. $ret[] = $res;
  175. }
  176. $retval = [];
  177. $retval["data"] = $ret;
  178. $retval["cant"] = $cant;
  179. return $retval;
  180. }
  181. /**
  182. * @param $parent
  183. * @param int $offset
  184. * @param $maxRecords
  185. * @param string $sort_mode
  186. * @param string $find
  187. * @param string $isValid
  188. * @return array
  189. */
  190. public function dir_list_sites($parent, $offset = 0, $maxRecords = -1, $sort_mode = 'hits_desc', $find = '', $isValid = 'y')
  191. {
  192. $bindvars = [(int)$parent];
  193. if ($find) {
  194. $findesc = '%' . $find . '%';
  195. $mid = " and (`name` like ? or `description` like ?)";
  196. $bindvars[] = $findesc;
  197. $bindvars[] = $findesc;
  198. } else {
  199. $mid = "";
  200. }
  201. if ($isValid) {
  202. $mid .= " and `isValid`=? ";
  203. $bindvars[] = $isValid;
  204. }
  205. $query = "select * from `tiki_directory_sites` tds, `tiki_category_sites` tcs where tds.`siteId`=tcs.`siteId` and tcs.`categId`=? $mid order by " . $this->convertSortMode($sort_mode);
  206. $query_cant = "select count(*) from `tiki_directory_sites` tds, `tiki_category_sites` tcs where tds.`siteId`=tcs.`siteId` and tcs.`categId`=? $mid";
  207. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  208. $cant = $this->getOne($query_cant, $bindvars);
  209. $ret = [];
  210. while ($res = $result->fetchRow()) {
  211. $res["cats"] = $this->dir_get_site_categories($res["siteId"]);
  212. $res["description"] = TikiLib::lib('parser')->parse_data($res["description"]);
  213. $ret[] = $res;
  214. }
  215. $retval = [];
  216. $retval["data"] = $ret;
  217. $retval["cant"] = $cant;
  218. return $retval;
  219. }
  220. /**
  221. * @param $offset
  222. * @param $maxRecords
  223. * @param $sort_mode
  224. * @param $find
  225. * @return array
  226. */
  227. public function dir_list_invalid_sites($offset, $maxRecords, $sort_mode, $find)
  228. {
  229. $bindvars = ["n"];
  230. if ($find) {
  231. $findesc = '%' . $find . '%';
  232. $mid = " and (`name` like ? or `description` like ?)";
  233. $bindvars[] = $findesc;
  234. $bindvars[] = $findesc;
  235. } else {
  236. $mid = "";
  237. }
  238. $query = "select * from `tiki_directory_sites` where `isValid`=? $mid order by " . $this->convertSortMode($sort_mode);
  239. $query_cant = "select count(*) from `tiki_directory_sites` where `isValid`=? $mid";
  240. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  241. $cant = $this->getOne($query_cant, $bindvars);
  242. $ret = [];
  243. while ($res = $result->fetchRow()) {
  244. $res["cats"] = $this->dir_get_site_categories($res["siteId"]);
  245. $ret[] = $res;
  246. }
  247. $retval = [];
  248. $retval["data"] = $ret;
  249. $retval["cant"] = $cant;
  250. return $retval;
  251. }
  252. /**
  253. * @param $siteId
  254. * @return array
  255. */
  256. public function dir_get_site_categories($siteId)
  257. {
  258. $query = "select tdc.`name`,tcs.`categId` from `tiki_category_sites` tcs,`tiki_directory_categories` tdc where tcs.`siteId`=? and tcs.`categId`=tdc.`categId`";
  259. $result = $this->query($query, [(int)$siteId]);
  260. $ret = [];
  261. while ($res = $result->fetchRow()) {
  262. $res["path"] = $this->dir_get_path_text($res["categId"]);
  263. $ret[] = $res;
  264. }
  265. return $ret;
  266. }
  267. /**
  268. * @param $offset
  269. * @param $maxRecords
  270. * @param $sort_mode
  271. * @param $find
  272. * @return array
  273. */
  274. public function dir_list_all_sites($offset, $maxRecords, $sort_mode, $find)
  275. {
  276. $bindvars = [];
  277. if ($find) {
  278. $findesc = '%' . $find . '%';
  279. $mid = " and (`name` like ? or `description` like ?)";
  280. $bindvars[] = $findesc;
  281. $bindvars[] = $findesc;
  282. } else {
  283. $mid = "";
  284. }
  285. $query = "select * from `tiki_directory_sites` $mid order by " . $this->convertSortMode($sort_mode);
  286. $query_cant = "select count(*) from `tiki_directory_sites` $mid";
  287. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  288. $cant = $this->getOne($query_cant, $bindvars);
  289. $ret = [];
  290. while ($res = $result->fetchRow()) {
  291. $res["cats"] = $this->dir_get_site_categories($res["siteId"]);
  292. $ret[] = $res;
  293. }
  294. $retval = [];
  295. $retval["data"] = $ret;
  296. $retval["cant"] = $cant;
  297. return $retval;
  298. }
  299. /**
  300. * @param $offset
  301. * @param $maxRecords
  302. * @param $sort_mode
  303. * @param $find
  304. * @return array
  305. */
  306. public function dir_list_all_valid_sites($offset, $maxRecords, $sort_mode, $find)
  307. {
  308. $bindvars = ['y'];
  309. $mid = " where `isValid`=? ";
  310. if ($find) {
  311. $findesc = '%' . $find . '%';
  312. $mid .= " and (`name` like ? or `description` like ?)";
  313. $bindvars[] = $findesc;
  314. $bindvars[] = $findesc;
  315. }
  316. $query = "select * from `tiki_directory_sites` $mid order by " . $this->convertSortMode($sort_mode);
  317. $query_cant = "select count(*) from `tiki_directory_sites` $mid";
  318. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  319. $cant = $this->getOne($query_cant, $bindvars);
  320. $ret = [];
  321. while ($res = $result->fetchRow()) {
  322. $res["cats"] = $this->dir_get_site_categories($res["siteId"]);
  323. $ret[] = $res;
  324. }
  325. $retval = [];
  326. $retval["data"] = $ret;
  327. $retval["cant"] = $cant;
  328. return $retval;
  329. }
  330. /**
  331. * @param $offset
  332. * @param $maxRecords
  333. * @param $sort_mode
  334. * @param $find
  335. * @param int $siteId
  336. * @return array
  337. */
  338. public function dir_get_all_categories($offset, $maxRecords, $sort_mode, $find, $siteId = 0)
  339. {
  340. $bindvars = [];
  341. if ($find) {
  342. $findesc = '%' . $find . '%';
  343. $mid = " and (`title` like ? or `data` like ?)";
  344. $bindvars[] = $findesc;
  345. $bindvars[] = $findesc;
  346. } else {
  347. $mid = "";
  348. }
  349. $query = "select * from `tiki_directory_categories` $mid order by " . $this->convertSortMode($sort_mode);
  350. $query_cant = "select count(*) from `tiki_directory_categories` $mid";
  351. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  352. $cant = $this->getOne($query_cant, $bindvars);
  353. $ret = [];
  354. while ($res = $result->fetchRow()) {
  355. $res["path"] = $this->dir_get_path_text($res["categId"]);
  356. $res["belongs"] = 'n';
  357. if ($siteId) {
  358. $belongs = $this->getOne("select count(*) from `tiki_category_sites` where `siteId`=? and `categId`=?", [(int)$siteId,(int)$res["categId"]]);
  359. if ($belongs) {
  360. $res["belongs"] = 'y';
  361. }
  362. }
  363. $ret[] = $res;
  364. }
  365. usort($ret, 'compare_paths');
  366. return $ret;
  367. }
  368. /**
  369. * @param $offset
  370. * @param $maxRecords
  371. * @param $sort_mode
  372. * @param $find
  373. * @param $parent
  374. * @return array
  375. */
  376. public function dir_get_all_categories_np($offset, $maxRecords, $sort_mode, $find, $parent)
  377. {
  378. $bindvars = [(int)$parent];
  379. if ($find) {
  380. $findesc = '%' . $find . '%';
  381. $mid = " and (`title` like ? or `data` like ?)";
  382. $bindvars[] = $findesc;
  383. $bindvars[] = $findesc;
  384. } else {
  385. $mid = "";
  386. }
  387. $query = "select * from `tiki_directory_categories` where `categId`<>? $mid order by " . $this->convertSortMode($sort_mode);
  388. $query_cant = "select count(*) from `tiki_directory_categories` where `categId`<>? $mid";
  389. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  390. $cant = $this->getOne($query_cant, $bindvars);
  391. $ret = [];
  392. while ($res = $result->fetchRow()) {
  393. $res["path"] = $this->dir_get_path_text($res["categId"]);
  394. $ret[] = $res;
  395. }
  396. usort($ret, 'compare_paths');
  397. return $ret;
  398. }
  399. /**
  400. * @param $offset
  401. * @param $maxRecords
  402. * @param $sort_mode
  403. * @param $find
  404. * @param int $siteId
  405. * @return array
  406. */
  407. public function dir_get_all_categories_accept_sites($offset, $maxRecords, $sort_mode, $find, $siteId = 0)
  408. {
  409. $bindvars = ['y'];
  410. if ($find) {
  411. $findesc = '%' . $find . '%';
  412. $mid = " and (`title` like ? or `data` like ?)";
  413. $bindvars[] = $findesc;
  414. $bindvars[] = $findesc;
  415. } else {
  416. $mid = "";
  417. }
  418. $query = "select * from `tiki_directory_categories` where `allowSites`=? $mid ";
  419. $query_cant = "select count(*) from `tiki_directory_categories` where `allowSites`=? $mid";
  420. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  421. $cant = $this->getOne($query_cant, $bindvars);
  422. $ret = [];
  423. while ($res = $result->fetchRow()) {
  424. $res["sites"] = $this->getOne("select count(*) from `tiki_category_sites` where `categId`=" . $res["categId"]);
  425. $res["path"] = $this->dir_get_path_text($res["categId"]);
  426. $res["belongs"] = 'n';
  427. if ($siteId) {
  428. $belongs = $this->getOne("select count(*) from `tiki_category_sites` where `siteId`=? and `categId`=?", [(int)$siteId, (int)$res["categId"]]);
  429. if ($belongs) {
  430. $res["belongs"] = 'y';
  431. }
  432. }
  433. $ret[] = $res;
  434. }
  435. usort($ret, 'compare_paths');
  436. return $ret;
  437. }
  438. /**
  439. * @param $siteId
  440. */
  441. public function dir_validate_site($siteId)
  442. {
  443. $query = "update `tiki_directory_sites` set `isValid`=? where `siteId`=?";
  444. $this->query($query, ["y", (int)$siteId]);
  445. }
  446. /**
  447. * @param $siteId
  448. * @param $name
  449. * @param $description
  450. * @param $url
  451. * @param $country
  452. * @param $isValid
  453. * @return mixed
  454. */
  455. public function dir_replace_site($siteId, $name, $description, $url, $country, $isValid)
  456. {
  457. global $prefs;
  458. $name = TikiFilter::get('striptags')->filter($name);
  459. $description = TikiFilter::get('striptags')->filter($description);
  460. $url = TikiFilter::get('url')->filter($url);
  461. $country = TikiFilter::get('word')->filter($country);
  462. if ($siteId) {
  463. $query = "update `tiki_directory_sites` set `name`=?, `description`=?, `url`=?, `country`=?, `isValid`=?, `lastModif`=? where `siteId`=?";
  464. $this->query($query, [$name,$description,$url,$country,$isValid,(int)$this->now,(int)$siteId]);
  465. } else {
  466. $query = "insert into `tiki_directory_sites`(`name`,`description`,`url`,`country`,`isValid`,`hits`,`created`,`lastModif`) values(?,?,?,?,?,?,?,?)";
  467. $this->query($query, [$name,$description,$url,$country,$isValid,0,(int)$this->now,(int)$this->now]);
  468. $siteId = $this->getOne("select max(siteId) from `tiki_directory_sites` where `created`=? and `name`=?", [(int)$this->now,$name]);
  469. if ($prefs['cachepages'] == 'y') {
  470. $this->cache_url($url);
  471. }
  472. }
  473. require_once('lib/search/refresh-functions.php');
  474. refresh_index('directory_sites', $siteId);
  475. return $siteId;
  476. }
  477. // Replace
  478. /**
  479. * @param $parent
  480. * @param $categId
  481. * @param $name
  482. * @param $description
  483. * @param $childrenType
  484. * @param $viewableChildren
  485. * @param $allowSites
  486. * @param $showCount
  487. * @param $editorGroup
  488. * @return mixed
  489. */
  490. public function dir_replace_category($parent, $categId, $name, $description, $childrenType, $viewableChildren, $allowSites, $showCount, $editorGroup)
  491. {
  492. if ($categId) {
  493. $query = "update `tiki_directory_categories` set `name`=?, `parent`=?, `description`=?, `childrenType`=?, `viewableChildren`=?, `allowSites`=?, `showCount`=?, `editorGroup`=? where `categId`=?";
  494. $this->query($query, [$name,(int)$parent,$description,$childrenType,(int)$viewableChildren,$allowSites,$showCount,$editorGroup,(int)$categId]);
  495. } else {
  496. $query = "insert into `tiki_directory_categories`(`parent`,`hits`,`name`,`description`,`childrenType`,`viewableChildren`,`allowSites`,`showCount`,`editorGroup`,`sites`) values(?,?,?,?,?,?,?,?,?,?)";
  497. $this->query($query, [(int)$parent,0,$name,$description,$childrenType,(int)$viewableChildren,$allowSites,$showCount,$editorGroup,0]);
  498. $categId = $this->getOne("select max(`categId`) from `tiki_directory_categories` where `name`=?", [$name]);
  499. }
  500. require_once('lib/search/refresh-functions.php');
  501. refresh_index('directory_categories', $categId);
  502. return $categId;
  503. }
  504. // Get
  505. /**
  506. * @param $siteId
  507. * @return bool
  508. */
  509. public function dir_get_site($siteId)
  510. {
  511. $query = "select * from `tiki_directory_sites` where `siteId`=?";
  512. $result = $this->query($query, [(int)$siteId]);
  513. if (! $result->numRows()) {
  514. return false;
  515. }
  516. $res = $result->fetchRow();
  517. return $res;
  518. }
  519. /**
  520. * @param $categId
  521. * @return bool
  522. */
  523. public function dir_get_category($categId)
  524. {
  525. $query = "select * from `tiki_directory_categories` where `categId`=?";
  526. $result = $this->query($query, [(int)$categId]);
  527. if (! $result->numRows()) {
  528. return false;
  529. }
  530. $res = $result->fetchRow();
  531. return $res;
  532. }
  533. /**
  534. * @param $siteId
  535. */
  536. public function dir_remove_site($siteId)
  537. {
  538. $query = "delete from `tiki_directory_sites` where `siteId`=?";
  539. $this->query($query, [(int)$siteId]);
  540. $query = "delete from `tiki_category_sites` where `siteId`=?";
  541. $this->query($query, [(int)$siteId]);
  542. }
  543. /**
  544. * @param $siteId
  545. * @param $categId
  546. */
  547. public function dir_add_site_to_category($siteId, $categId)
  548. {
  549. $query = "delete from `tiki_category_sites` where `siteId`=? and `categId`=?";
  550. $this->query($query, [(int)$siteId,(int)$categId]);
  551. $query = "insert into `tiki_category_sites`(`siteId`,`categId`) values(?,?)";
  552. $this->query($query, [(int)$siteId,(int)$categId]);
  553. }
  554. /**
  555. * @param $siteId
  556. */
  557. public function remove_site_from_categories($siteId)
  558. {
  559. $query = "delete from `tiki_category_sites` where `siteId`=?";
  560. $this->query($query, [(int)$siteId]);
  561. }
  562. /**
  563. * @param $siteId
  564. * @param $categId
  565. */
  566. public function remove_site_from_category($siteId, $categId)
  567. {
  568. $query = "delete from `tiki_category_sites` where `siteId`=? and `categId`=?";
  569. $this->query($query, [(int)$siteId,(int)$categId]);
  570. }
  571. /**
  572. * @param $categId
  573. */
  574. public function dir_remove_category($categId)
  575. {
  576. $parent_categId = $categId;
  577. $query = "select * from `tiki_directory_categories` where `parent`=?";
  578. $result = $this->query($query, [(int)$categId]);
  579. while ($res = $result->fetchRow()) {
  580. $categId = $res["categId"];
  581. $this->dir_remove_category($res["categId"]);
  582. $query2 = "select * from `tiki_category_sites` where `categId`=?";
  583. $result2 = $this->query($query2, [(int)$categId]);
  584. while ($res2 = $result2->fetchRow()) {
  585. $siteId = $res2["siteId"];
  586. $query3 = "delete from `tiki_category_sites` where `siteId`=? and `categId`=?";
  587. $result3 = $this->query($query3, [(int)$siteId,(int)$categId]);
  588. $cant = $this->getOne("select count(*) from `tiki_category_sites` where `siteId`=?", [(int)$siteId]);
  589. if (! $cant) {
  590. $this->dir_remove_site($siteId);
  591. }
  592. }
  593. $query4 = "delete from `tiki_related_categories` where `categId`=? or `relatedTo`=?";
  594. $result4 = $this->query($query4, [(int)$categId,(int)$categId]);
  595. }
  596. $query = "delete from `tiki_directory_categories` where `categId`=?";
  597. $result = $this->query($query, [(int)$parent_categId]);
  598. $query = "delete from `tiki_category_sites` where `categId`=?";
  599. $result = $this->query($query, [(int)$parent_categId]);
  600. }
  601. /**
  602. * @param $parent
  603. * @param $related
  604. */
  605. public function dir_remove_related($parent, $related)
  606. {
  607. $query = "delete from `tiki_related_categories` where `categId`=? and `relatedTo`=?";
  608. $this->query($query, [(int)$parent,(int)$related]);
  609. }
  610. /**
  611. * @param $parent
  612. * @param $offset
  613. * @param $maxRecords
  614. * @param $soet_mode
  615. * @param $find
  616. * @return array
  617. */
  618. public function dir_list_related_categories($parent, $offset, $maxRecords, $soet_mode, $find)
  619. {
  620. $query = "select * from `tiki_related_categories` where `categId`=?";
  621. $query_cant = "select count(*) from `tiki_related_categories` where `categId`=?";
  622. $result = $this->query($query, [(int)$parent], $maxRecords, $offset);
  623. $cant = $this->getOne($query_cant, [(int)$parent]);
  624. $ret = [];
  625. while ($res = $result->fetchRow()) {
  626. $res["path"] = $this->dir_get_path_text($res["relatedTo"]);
  627. $ret[] = $res;
  628. }
  629. $retval = [];
  630. usort($ret, 'compare_paths');
  631. $retval["data"] = $ret;
  632. $retval["cant"] = $cant;
  633. return $retval;
  634. }
  635. /**
  636. * @param $parent
  637. * @param $categ
  638. */
  639. public function dir_add_categ_rel($parent, $categ)
  640. {
  641. $query = "delete from `tiki_related_categories` where `categId`=? and `relatedTo`=?";
  642. $this->query($query, [(int)$parent,(int)$categ]);
  643. $query = "insert into `tiki_related_categories`(`categId`,`relatedTo`) values(?,?)";
  644. $this->query($query, [(int)$parent,(int)$categ]);
  645. }
  646. /**
  647. * @param $url
  648. * @return mixed
  649. */
  650. public function dir_url_exists($url)
  651. {
  652. $cant = $this->getOne("select count(*) from `tiki_directory_sites` where `url`=?", [$url]);
  653. return $cant;
  654. }
  655. /**
  656. * @param $siteId
  657. */
  658. public function dir_add_site_hit($siteId)
  659. {
  660. global $prefs, $user;
  661. if (StatsLib::is_stats_hit()) {
  662. $query = "update `tiki_directory_sites` set `hits`=`hits`+1 where `siteId`=?";
  663. $this->query($query, [(int)$siteId]);
  664. }
  665. }
  666. /**
  667. * @param $categId
  668. */
  669. public function dir_add_category_hit($categId)
  670. {
  671. global $prefs, $user;
  672. if (StatsLib::is_stats_hit()) {
  673. $query = "update `tiki_directory_categories` set `hits`=`hits`+1 where `categId`=?";
  674. $this->query($query, [(int)$categId]);
  675. }
  676. }
  677. /**
  678. * @param $words
  679. * @param string $how
  680. * @param int $offset
  681. * @param $maxRecords
  682. * @param string $sort_mode
  683. * @return array
  684. */
  685. public function dir_search($words, $how = 'or', $offset = 0, $maxRecords = -1, $sort_mode = 'hits_desc')
  686. {
  687. // First of all split the words by whitespaces building the query string
  688. // we'll search by name, url, description and cache, the relevance will be calculated using hits
  689. $words = explode(' ', $words);
  690. $bindvars = ['y'];
  691. for ($i = 0, $icount_words = count($words); $i < $icount_words; $i++) {
  692. $word = trim($words[$i]);
  693. if (! empty($word)) {
  694. // Check if the term is in the stats then add it or increment it
  695. if ($this->getOne("select count(*) from `tiki_directory_search` where `term`=?", [$word])) {
  696. $query = "update `tiki_directory_search` set `hits`=`hits`+1 where `term`=?";
  697. $this->query($query, [$word]);
  698. } else {
  699. $query = "insert into `tiki_directory_search`(`term`,`hits`) values(?,?)";
  700. $this->query($query, [$word,1]);
  701. }
  702. }
  703. $like[$i] = " ((`name` like ?) or (`description` like ?) or (`url` like ?) or (`cache` like ?)) ";
  704. $bindvars[] = "%$word%";
  705. $bindvars[] = "%$word%";
  706. $bindvars[] = "%$word%";
  707. $bindvars[] = "%$word%";
  708. }
  709. $how = in_array($how, ['or', 'and']) ? $how : 'or';
  710. $likestr = implode($how, $like);
  711. $query = "select * from `tiki_directory_sites` where `isValid`=? and $likestr order by "
  712. . $this->convertSortMode($sort_mode);
  713. $cant = $this->getOne("select count(*) from tiki_directory_sites where `isValid`=? and $likestr", $bindvars);
  714. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  715. $ret = [];
  716. while ($res = $result->fetchRow()) {
  717. $res["cats"] = $this->dir_get_site_categories($res["siteId"]);
  718. $ret[] = $res;
  719. }
  720. $retval = [];
  721. $retval["data"] = $ret;
  722. $retval["cant"] = $cant;
  723. return $retval;
  724. }
  725. /**
  726. * @param $parent
  727. * @param $words
  728. * @param string $how
  729. * @param int $offset
  730. * @param $maxRecords
  731. * @param string $sort_mode
  732. * @return array
  733. */
  734. public function dir_search_cat($parent, $words, $how = 'or', $offset = 0, $maxRecords = -1, $sort_mode = 'hits_desc')
  735. {
  736. // First of all split the words by whitespaces building the query string
  737. // we'll search by name, url, description and cache, the relevance will be calculated using hits
  738. $words = explode(' ', $words);
  739. $bindvars = ['y',(int)$parent];
  740. for ($i = 0, $icount_words = count($words); $i < $icount_words; $i++) {
  741. $word = trim($words[$i]);
  742. // Check if the term is in the stats then add it or increment it
  743. if ($this->getOne("select count(*) from `tiki_directory_search` where `term`=?", [$word])) {
  744. $query = "update `tiki_directory_search` set `hits`=`hits`+1 where `term`=?";
  745. $this->query($query, [$word]);
  746. } else {
  747. $query = "insert into `tiki_directory_search`(`term`,`hits`) values(?,?)";
  748. $this->query($query, [$word,1]);
  749. }
  750. $like[$i] = " ((tds.`name` like ?) or (tds.`description` like ?) or (tds.`url` like ?) or (`cache` like ?)) ";
  751. $bindvars[] = "%$word%";
  752. $bindvars[] = "%$word%";
  753. $bindvars[] = "%$word%";
  754. $bindvars[] = "%$word%";
  755. }
  756. $how = in_array($how, ['or', 'and']) ? $how : 'or';
  757. $likestr = implode($how, $like);
  758. $query = "select distinct tds.`name`, tds.`siteId`, tds.`description`, tds.`url`, tds.`country`, tds.`hits`, ";
  759. $query .= " tds.`created`, tds.`lastModif` from `tiki_directory_sites` tds, `tiki_category_sites` tcs,
  760. `tiki_directory_categories` tdc ";
  761. $query .= " where tds.`siteId`=tcs.`siteId` and tcs.`categId`=tdc.`categId` and `isValid`=? and tdc.`categId`=?
  762. and $likestr order by " . $this->convertSortMode($sort_mode);
  763. $cant = $this->getOne(
  764. "select count(*) from `tiki_directory_sites` tds,`tiki_category_sites` tcs,`tiki_directory_categories` tdc
  765. where tds.`siteId`=tcs.`siteId` and tcs.`categId`=tdc.`categId` and `isValid`=? and tdc.`categId`=?
  766. and $likestr",
  767. $bindvars
  768. );
  769. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  770. $ret = [];
  771. while ($res = $result->fetchRow()) {
  772. $res["cats"] = $this->dir_get_site_categories($res["siteId"]);
  773. $ret[] = $res;
  774. }
  775. $retval = [];
  776. $retval["data"] = $ret;
  777. $retval["cant"] = $cant;
  778. return $retval;
  779. }
  780. }
  781. $dirlib = new DirLib();
  782. /**
  783. * @param $p1
  784. * @param $p2
  785. * @return int
  786. */
  787. function compare_paths($p1, $p2)
  788. {
  789. // must be case insentive to have the same than dir_mist_sites
  790. return strcasecmp($p1["path"], $p2["path"]);
  791. }