PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/e107_plugins/tagwords/tagwords_class.php

https://github.com/CasperGemini/e107
PHP | 895 lines | 626 code | 86 blank | 183 comment | 56 complexity | 3fcb52161b973ce5c630fd1d4c7f7e2b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * e107 website system
  4. *
  5. * Copyright (C) 2008-2013 e107 Inc (e107.org)
  6. * Released under the terms and conditions of the
  7. * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
  8. *
  9. * Tagwords Class
  10. *
  11. */
  12. if(!defined("TAG_TEXTAREA_COLS")){ define("TAG_TEXTAREA_COLS", "70"); }
  13. if(!defined("TAG_TEXTAREA_ROWS")){ define("TAG_TEXTAREA_ROWS", "4"); }
  14. //##### explanation of present $_GET's ----------------------------------------
  15. // $_GET['type'] : the type of tag to show (cloud, list)
  16. // $_GET['sort'] : the sort method for the cloud/list (alpha, freq)
  17. // $_GET['area'] : the area to show cloud/list for (eg news, content)
  18. // $_GET['q'] : the search query to show results for
  19. class tagwords
  20. {
  21. var $tagwords = array();
  22. var $mapper = array();
  23. var $core = array('news', 'page');
  24. var $table = 'tagwords';
  25. var $pref;
  26. var $template;
  27. var $shortcodes;
  28. /*
  29. * constructor (include all e_tagwords.php files)
  30. */
  31. function __construct()
  32. {
  33. global $pref, $TEMPLATE_TAGWORDS, $tagwords_shortcodes;
  34. $this->e107 = e107::getInstance();
  35. //language
  36. e107::includeLan(e_PLUGIN."tagwords/languages/".e_LANGUAGE.".php");
  37. //shortcodes
  38. //require_once(e_PLUGIN.'tagwords/tagwords_shortcodes.php');
  39. $this->shortcodes = e107::getScBatch('tagwords',TRUE);
  40. //$this->shortcodes = $tagwords_shortcodes;
  41. //template
  42. if (is_readable(THEME."tagwords_template.php"))
  43. {
  44. require_once(THEME."tagwords_template.php");
  45. }
  46. else
  47. {
  48. require_once(e_PLUGIN."tagwords/tagwords_template.php");
  49. }
  50. $this->template = $TEMPLATE_TAGWORDS;
  51. $this->pref = $this->get_prefs();
  52. //load plugin and core tagword areas
  53. $this->loadPlugin();
  54. $this->loadCore();
  55. //prepare and assign key'ed tagwords data
  56. $this->mapper();
  57. }
  58. /*
  59. * get all plugin classes
  60. */
  61. function loadPlugin()
  62. {
  63. global $pref;
  64. $list = e107::getPref('e_tagwords_list');
  65. if($list && is_array($list))
  66. {
  67. foreach($list as $e_tag)
  68. {
  69. if(is_readable(e_PLUGIN.$e_tag."/e_tagwords.php"))
  70. {
  71. require_once(e_PLUGIN.$e_tag."/e_tagwords.php");
  72. $name = "e_tagwords_{$e_tag}";
  73. if(class_exists($name))
  74. {
  75. if(!is_object($name))
  76. {
  77. $this->$name = new $name;
  78. }
  79. $this->tagwords[] = $e_tag;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. /*
  86. * get all core classes
  87. */
  88. function loadCore()
  89. {
  90. foreach($this->core as $s)
  91. {
  92. require_once(e_PLUGIN."tagwords/section/e_tagwords_{$s}.php");
  93. $name = "e_tagwords_{$s}";
  94. if(class_exists($name))
  95. {
  96. if(!is_object($name))
  97. {
  98. $this->$name = new $name;
  99. }
  100. $this->tagwords[] = $s;
  101. }
  102. }
  103. }
  104. /*
  105. * map plugin names to tables names
  106. */
  107. function mapper()
  108. {
  109. $ret = array();
  110. foreach($this->tagwords as $area)
  111. {
  112. $ins = "e_tagwords_{$area}";
  113. $table = $this->$ins->settings['table'];
  114. $ret[$table] = $area;
  115. }
  116. $this->mapper = $ret;
  117. }
  118. /*
  119. * display the form element to provide tag words
  120. * @param string $tag_type type
  121. * @param string $tag_itemid itemid
  122. * @return array $caption, $text form element
  123. */
  124. function tagwords_form($tag_type='', $tag_itemid='')
  125. {
  126. $tp = e107::getParser();
  127. $allowed = $this->getAllowedAreas();
  128. if(count($allowed)==0)
  129. {
  130. return;
  131. }
  132. else
  133. {
  134. if(!in_array($tag_type, $allowed))
  135. {
  136. return;
  137. }
  138. }
  139. $this->word = false;
  140. if( $ret = $this->getRecords($tag_type, $tag_itemid) )
  141. {
  142. $this->word = $tp->toForm($ret);
  143. }
  144. $caption = $tp->parseTemplate($this->template['caption'], true, $this->shortcodes);
  145. $text = $tp->parseTemplate($this->template['form'], true, $this->shortcodes);
  146. return array('caption'=>$caption, 'html'=>$text);
  147. }
  148. /*
  149. * retrieve all tag_words for the provided item (type+itemid)
  150. * @param string $tag_type type
  151. * @param string $tag_itemid itemid
  152. * @param boolean $returnwordsonly return comma seperated list of tagwords
  153. * @param boolean $link return hyperlink to taglist on words
  154. * @return array $ret array of tagwords
  155. */
  156. function getRecords($tag_type='', $tag_itemid='', $returnwordsonly=false, $link=true)
  157. {
  158. $tp = e107::getParser();
  159. $sqlgr = new db;
  160. $qry = "
  161. SELECT tag_word FROM #".$this->table."
  162. WHERE tag_type='".$tp->toDB($tag_type)."'
  163. AND tag_itemid='".intval($tag_itemid)."'
  164. ORDER BY tag_word ";
  165. if($sqlgr->gen($qry))
  166. {
  167. $ret=array();
  168. while($row = $sqlgr->fetch())
  169. {
  170. $ret[] = $row['tag_word'];
  171. }
  172. if($returnwordsonly)
  173. {
  174. $arr = array();
  175. foreach($ret as $word)
  176. {
  177. $word = $tp->toHTML($word,true,'no_hook, emotes_off');
  178. $arr[] = ($link ? trim($this->createTagWordLink($word)) : $word);
  179. }
  180. return implode($this->pref['tagwords_word_seperator'], $arr);
  181. }
  182. //return a \n seperated list of tagwords for the specific item
  183. return implode("\n", $ret);
  184. }
  185. return;
  186. }
  187. /*
  188. * used on tagword search; get only those records that contain ALL tagwords that is searched on
  189. * @param array $array the array containing all results for each word seperately
  190. * @return array $combined array of combined tagwords
  191. */
  192. function getUnique($array)
  193. {
  194. //combine all arrays
  195. $combined = array();
  196. foreach($array as $v)
  197. {
  198. foreach($v as $type=>$arr)
  199. {
  200. foreach($arr as $id)
  201. {
  202. if(!array_key_exists($type,$combined))
  203. {
  204. $combined[$type] = array();
  205. }
  206. if(!in_array($id, $combined[$type]))
  207. {
  208. $combined[$type][] = $id;
  209. }
  210. }
  211. }
  212. }
  213. //$combined is combined array of ALL values
  214. foreach($combined as $ntype=>$narr)
  215. {
  216. foreach($narr as $nid)
  217. {
  218. //$array is the array containing all results for each word seperately
  219. foreach($array as $ov)
  220. {
  221. //if an id value is not present in the result $array, remove it from the combined array
  222. if(!in_array($nid, $ov[$ntype]))
  223. {
  224. $key = array_search($nid, $combined[$ntype]);
  225. unset($combined[$ntype][$key]);
  226. }
  227. }
  228. }
  229. }
  230. //finally remove empty areas from the array
  231. foreach($combined as $ntype=>$narr)
  232. {
  233. sort($combined[$ntype]);
  234. if(empty($combined[$ntype]))
  235. {
  236. unset($combined[$ntype]);
  237. }
  238. }
  239. return $combined;
  240. }
  241. /*
  242. * create the tag word hyperlink
  243. * @param string $word word
  244. * @param string $class if present and int, adds the 'level' of the word to the css item
  245. * @return string $text hyperlink to tagword
  246. */
  247. function createTagWordLink($word, $class='')
  248. {
  249. $word = trim($word);
  250. /*$qry = $tp->toDB($word);
  251. $qry = str_replace(' ', '+', $qry);*/
  252. $url = e107::getUrl()->create('tagwords/search', 'q='.$word);
  253. $word = e107::getParser()->toHTML($word,true,'no_hook, emotes_off');
  254. $class = ($class ? "class='tag".intval($class)."'" : "");
  255. return "<a href='".$url."' ".$class." title=''>".$word."</a>";
  256. }
  257. /*
  258. * db entry tagwords
  259. * @param string $tag_type type
  260. * @param string $tag_itemid itemid
  261. * @param string $tag_word word
  262. * @return string $text message
  263. */
  264. function dbTagWords($tag_type='', $tag_itemid='', $tag_word='')
  265. {
  266. $tp = e107::getParser();
  267. $sql = e107::getDb();
  268. $mes = e107::getMessage();
  269. //prepare data
  270. $tag_type = $tp->toDB($tag_type);
  271. $tag_itemid = intval($tag_itemid);
  272. //get existing word records
  273. $existing_array = $this->getRecords($tag_type, $tag_itemid, false);
  274. $existing = explode("\n", $existing_array);
  275. //create array of new posted words
  276. $new = explode("\n", $tag_word);
  277. //delete the differences (delete what has been removed)
  278. $delete_diff = array_diff($existing, $new);
  279. foreach($delete_diff as $word)
  280. {
  281. $word = trim($word);
  282. $word = $tp->toDB($word);
  283. $sql->delete($this->table, "tag_type='".$tag_type."' AND tag_itemid='".$tag_itemid."' AND tag_word='".$word."'");
  284. }
  285. //insert the differences (insert what has been added)
  286. $insert_diff = array_diff($new, $existing);
  287. $count = 0;
  288. // print_a($insert_diff);
  289. return ("Tagword Insert: ".print_a($new,true)); // FIXME
  290. foreach($insert_diff as $word)
  291. {
  292. $word = trim($word);
  293. $word = $tp->toDB($word);
  294. $args = array();
  295. $args['tag_id'] = 0;
  296. $args['tag_type'] = $tag_type;
  297. $args['tag_itemid'] = $tag_itemid;
  298. $args['tag_word'] = $word;
  299. $count += $sql->insert($this->table, $args) ? 1 : 0;
  300. return "a Diff was made";
  301. }
  302. return "<br />".LAN_TAG_3.": ".$count." words."; // FIXME
  303. }
  304. /*
  305. * db delete entries
  306. * @param string $tag_type type
  307. * @param string $tag_itemid itemid
  308. */
  309. function dbDelete($tag_type='', $tag_itemid='')
  310. {
  311. e107::getDb()->delete($this->table, "tag_type='".$tag_type."' AND tag_itemid='".$tag_itemid."' ");
  312. }
  313. /*
  314. * retrieve all tag words (if $_GET['area'], only retrieve tagwords from that area (news, content, ...) )
  315. * @param string $tagarea tagarea
  316. * @return array $ret array of word=>number
  317. */
  318. function getAllTagWords($tagarea='')
  319. {
  320. $sql = e107::getDb();
  321. $tag_type='';
  322. $allowed = $this->getAllowedAreas();
  323. if(count($allowed)>0)
  324. {
  325. $typeqry = " AND tag_type IN ('".implode("','", $this->getAllowedAreas())."') ";
  326. }
  327. if($tagarea!='menu')
  328. {
  329. //if user is able to manually set a area
  330. if(varsettrue($this->pref['tagwords_view_area'])=='1')
  331. {
  332. foreach($this->tagwords as $area)
  333. {
  334. //limit data to only one area, if so, which area
  335. if(varsettrue($_GET['area'])==$area)
  336. {
  337. $ins = "e_tagwords_{$area}";
  338. $typeqry = " AND tag_type='".$this->$ins->settings['table']."'";
  339. break;
  340. }
  341. }
  342. }
  343. }
  344. //menu additions
  345. $menuqry='';
  346. if($tagarea=='menu')
  347. {
  348. if($this->pref['tagwords_menu_min'])
  349. {
  350. $menuqry = "ORDER BY number DESC LIMIT 0,".$this->pref['tagwords_menu_min'];
  351. }
  352. }
  353. $qry = "
  354. SELECT tag_id, tag_itemid, tag_type, tag_word, COUNT(tag_word) as number
  355. FROM #".$this->table."
  356. WHERE tag_word!='' ".$typeqry."
  357. GROUP BY tag_word HAVING COUNT(tag_word)>=".intval($this->pref['tagwords_min'])." ".$menuqry." ";
  358. if($sql->gen($qry))
  359. {
  360. $ret=array();
  361. while($row = $sql->fetch())
  362. {
  363. $word = trim($row['tag_word']);
  364. $word = e107::getParser()->toHTML($word,true,'no_hook, emotes_off');
  365. $ret[$word] = $row['number'];
  366. }
  367. return $ret;
  368. }
  369. return;
  370. }
  371. /*
  372. * Get allowed areas from pref
  373. * @return $array allowed areas
  374. */
  375. function getAllowedAreas()
  376. {
  377. $this->allowed_areas = array();
  378. foreach($this->tagwords as $area)
  379. {
  380. if(array_key_exists($area,$this->pref['tagwords_activeareas']))
  381. {
  382. $ins = "e_tagwords_{$area}";
  383. $this->allowed_areas[] = $this->$ins->settings['table'];
  384. }
  385. }
  386. return $this->allowed_areas;
  387. }
  388. /*
  389. * Sort data array of tag words
  390. * @param string $array the array of words
  391. * @param string $tagarea the area (menu,page)
  392. * @return $array sorted array
  393. */
  394. function TagSort($array, $tagarea='')
  395. {
  396. //MENU
  397. if($tagarea=='menu')
  398. {
  399. if(varsettrue($this->pref['tagwords_menu_default_sort'])==1)
  400. {
  401. //alphabetically
  402. ksort($array, SORT_STRING);
  403. }
  404. else
  405. {
  406. //by size
  407. arsort($array);
  408. }
  409. return $array;
  410. }
  411. //PAGE
  412. //user can set own sort
  413. if(varsettrue($this->pref['tagwords_view_sort'])==1)
  414. {
  415. $s = varset($_GET['sort'],'');
  416. switch($s)
  417. {
  418. //if user has set sort, and is set to alpha
  419. case 'alpha':
  420. ksort($array, SORT_STRING);
  421. break;
  422. //if user has set sort, and is set to by size
  423. case 'freq':
  424. arsort($array);
  425. break;
  426. default:
  427. if(varsettrue($this->pref['tagwords_default_sort'])==1)
  428. {
  429. //alphabetically
  430. ksort($array, SORT_STRING);
  431. }
  432. else
  433. {
  434. //by size
  435. arsort($array);
  436. }
  437. break;
  438. }
  439. //user cannot set sort, so use default sort value
  440. }
  441. else
  442. {
  443. if(varsettrue($this->pref['tagwords_default_sort'])==1)
  444. {
  445. //alphabetically
  446. ksort($array, SORT_STRING);
  447. }
  448. else
  449. {
  450. //by size
  451. arsort($array);
  452. }
  453. }
  454. return $array;
  455. }
  456. /*
  457. * Render Message
  458. * @param string $message message
  459. * @param string $caption caption
  460. * @return string $text (ns,echo,return)
  461. */
  462. function show_message($message, $caption='', $type='ns')
  463. {
  464. switch($type)
  465. {
  466. case 'echo':
  467. echo "<div style='text-align:center'><b>".$message."</b></div>";
  468. break;
  469. case 'return':
  470. return "<div style='text-align:center'><b>".$message."</b></div>";
  471. break;
  472. case 'ns':
  473. default:
  474. e107::getRender()->tablerender($caption, "<div style='text-align:center'><b>".$message."</b></div>");
  475. break;
  476. }
  477. }
  478. //##### show tagcloud/taglist -------------------------------------------------
  479. /*
  480. * Show tagcloud/taglist
  481. * @return tablerender tagcloud/taglist
  482. */
  483. function TagRender()
  484. {
  485. $tp = e107::getParser();
  486. $type = false;
  487. //decide whether to show the taglist or the tagcloud
  488. //user can set own tag style
  489. if(varsettrue($this->pref['tagwords_view_style'])=='1')
  490. {
  491. $t = varset($_GET['type'],'');
  492. switch($t)
  493. {
  494. case 'cloud':
  495. $type='cloud';
  496. break;
  497. case 'list':
  498. $type='list';
  499. break;
  500. default:
  501. $type = (varsettrue($this->pref['tagwords_default_style'])=='1' ? 'cloud' : 'list');
  502. break;
  503. }
  504. }
  505. else
  506. {
  507. //user cannot set tag style, so use default tag style value
  508. $type = (varsettrue($this->pref['tagwords_default_style'])=='1' ? 'cloud' : 'list');
  509. }
  510. //show the taglist or tagcloud
  511. if($type=='list')
  512. {
  513. $text = $tp->parseTemplate($this->template['cloudlist'], true, $this->shortcodes);
  514. e107::getRender()->tablerender(LAN_TAG_17, $text);
  515. }
  516. else
  517. {
  518. $text = $tp->parseTemplate($this->template['cloud'], true, $this->shortcodes);
  519. e107::getRender()->tablerender(LAN_TAG_16, $text);
  520. }
  521. return;
  522. }
  523. /*
  524. * retrieve all records based on tag word
  525. * @param string $word word
  526. * @param string $caption caption
  527. * @return array $type=>$id
  528. */
  529. function getRecordsByTag($word='')
  530. {
  531. $sql = e107::getDb();
  532. $typeqry = '';
  533. $allowed = $this->getAllowedAreas();
  534. if(count($allowed)>0)
  535. {
  536. $typeqry = " AND tag_type IN ('".implode("','", $allowed)."') ";
  537. }
  538. if($sql->gen("SELECT tag_type, tag_itemid FROM #".$this->table." WHERE tag_word REGEXP('".e107::getParser()->toDB($word)."') ".$typeqry." "))
  539. {
  540. $ret = array();
  541. while($row = $sql->fetch())
  542. {
  543. $ret[$row['tag_type']][] = $row['tag_itemid'];
  544. }
  545. return $ret;
  546. }
  547. return;
  548. }
  549. /*
  550. * show list of related content based on the word (=$_GET['q'])
  551. * @param string tablerender
  552. */
  553. function TagSearchResults()
  554. {
  555. $tp = e107::getParser();
  556. global $row;
  557. //the full search query + every single word in the query will be used to search
  558. $_GET['q'] = str_replace('+', ' ', $_GET['q']);
  559. $words=array();
  560. //add each seperate word to search
  561. $words = explode(" ", trim($_GET['q']));
  562. if(count($words)>1)
  563. {
  564. //add full query to search
  565. array_unshift($words, $_GET['q']);
  566. }
  567. $results=array();
  568. foreach($words as $w)
  569. {
  570. if($w!='')
  571. {
  572. if($t = $this->getRecordsByTag($w))
  573. {
  574. $results[] = $t;
  575. }
  576. }
  577. }
  578. //only combine the array if searching on multiple words
  579. //else the array is always the [0] key
  580. if(count($words)==1)
  581. {
  582. $records = $results[0];
  583. ksort($records, SORT_STRING);
  584. }
  585. elseif(count($words) > 1)
  586. {
  587. $records = $this->getUnique($results);
  588. ksort($records, SORT_STRING);
  589. }
  590. $this->num = count($records, 1) - count($records);
  591. $text = $tp->parseTemplate($this->template['intro'], true, $this->shortcodes);
  592. foreach($records as $type=>$ids)
  593. {
  594. if(array_key_exists($type, $this->mapper))
  595. {
  596. $pluginName = $this->mapper[$type];
  597. $ins = "e_tagwords_{$pluginName}";
  598. $this->area = $this->$ins;
  599. //area (news, content, ...)
  600. $text .= $tp->parseTemplate($this->template['area'], true, $this->shortcodes);
  601. //records for found related tagword
  602. $text .= $this->template['link_start'];
  603. foreach($ids as $id)
  604. {
  605. $this->id = $id;
  606. if(method_exists($this->area, 'getRecord'))
  607. {
  608. $this->area->getRecord($this->id);
  609. $text .= $tp->parseTemplate($this->template['link_item'], true, $this->shortcodes);
  610. }
  611. }
  612. $text .= $this->template['link_end'];
  613. }
  614. }
  615. e107::getRender()->tablerender(LAN_TAG_1, $text);
  616. return;
  617. }
  618. /*
  619. * create a tag cloud
  620. * @return string $text
  621. */
  622. function TagCloud($mode='')
  623. {
  624. if($mode=='menu')
  625. {
  626. $t_start = $this->template['menu_cloud_start'];
  627. $t_item = $this->template['menu_cloud_item'];
  628. $t_end = $this->template['menu_cloud_end'];
  629. }
  630. else
  631. {
  632. $t_start = $this->template['cloud_start'];
  633. $t_item = $this->template['cloud_item'];
  634. $t_end = $this->template['cloud_end'];
  635. }
  636. $tags = $this->getAllTagWords($mode);
  637. $tags = $this->TagSort($tags, $mode);
  638. if(!is_array($tags))
  639. {
  640. return $this->show_message(LAN_TAG_18, '', 'return');
  641. }
  642. // tags will be displayed between class 1 to class 5
  643. $min_size = 1;
  644. $max_size = 5;
  645. // get the largest and smallest array values
  646. $min_qty = min(array_values($tags));
  647. $max_qty = max(array_values($tags));
  648. // find the range of values
  649. $spread = $max_qty - $min_qty;
  650. // we don't want to divide by zero
  651. if(0 == $spread)
  652. {
  653. $spread = 1;
  654. }
  655. // determine the increment, this is the increase per tag quantity (times used)
  656. $step = ($max_size - $min_size)/($spread);
  657. $text = $t_start;
  658. // loop through our tag array
  659. foreach ($tags as $key => $value)
  660. {
  661. $class = ceil($min_size + (($value - $min_qty) * $step));
  662. $this->word = $this->createTagWordLink($key, $class);
  663. $this->number = $value;
  664. $text .= e107::getParser()->parseTemplate($t_item, true, $this->shortcodes);
  665. }
  666. $text .= $t_end;
  667. return $text;
  668. }
  669. /*
  670. * create a tag list
  671. * @return string $text tagcloudlist
  672. */
  673. function TagCloudList()
  674. {
  675. $tags = $this->getAllTagWords();
  676. $tags = $this->TagSort($tags);
  677. if(!is_array($tags))
  678. {
  679. return $this->show_message(LAN_TAG_18, '', 'return');
  680. }
  681. $text = $this->template['cloudlist_start'];
  682. foreach($tags as $key => $value)
  683. {
  684. $this->word = $this->createTagWordLink($key);
  685. $this->number = $value;
  686. $text .= e107::getParser()->parseTemplate($this->template['cloudlist_item'], true, $this->shortcodes);
  687. }
  688. $text .= $this->template['cloudlist_end'];
  689. return $text;
  690. }
  691. //##### ADMIN
  692. /*
  693. * default preferences
  694. */
  695. function default_prefs()
  696. {
  697. $ret=array();
  698. $ret['tagwords_min'] = 1;
  699. $ret['tagwords_class'] = 0;
  700. $ret['tagwords_default_sort'] = 1;
  701. $ret['tagwords_default_style'] = 1;
  702. $ret['tagwords_view_sort'] = 1;
  703. $ret['tagwords_view_style'] = 1;
  704. $ret['tagwords_view_area'] = 1;
  705. $ret['tagwords_view_search'] = 1;
  706. $ret['tagwords_view_freq'] = 1;
  707. $ret['tagwords_word_seperator'] = ' / ';
  708. $ret['tagwords_menu_caption'] = 'LAN_TAG_MENU_2';
  709. $ret['tagwords_menu_min'] = 5;
  710. $ret['tagwords_menu_default_sort'] = 1;
  711. $ret['tagwords_menu_view_search'] = 1;
  712. $ret['tagwords_menu_view_freq'] = 0;
  713. //activate core areas by default
  714. $ret['tagwords_activeareas'] = array();
  715. foreach($this->core as $area)
  716. {
  717. $ret['tagwords_activeareas'][$area] = 1;
  718. }
  719. return $ret;
  720. }
  721. /*
  722. * get preferences from db
  723. */
  724. function get_prefs()
  725. {
  726. $sql = e107::getDb();
  727. $num_rows = $sql->gen("SELECT * FROM #core WHERE e107_name='".$this->table."' ");
  728. if($num_rows == 0)
  729. {
  730. $p = $this->default_prefs();
  731. $tmp = $this->e107->arrayStorage->WriteArray($p);
  732. $sql->insert("core", "'".$this->table."', '{$tmp}' ");
  733. $sql->gen("SELECT * FROM #core WHERE e107_name='".$this->table."' ");
  734. }
  735. $row = $sql->fetch();
  736. $p = $this->e107->arrayStorage->ReadArray($row['e107_value']);
  737. //validation
  738. if(!array_key_exists('tagwords_activeareas', $p))
  739. {
  740. $p['tagwords_activeareas'] = array();
  741. }
  742. return $p;
  743. }
  744. /*
  745. * Update preferences
  746. */
  747. function update_prefs()
  748. {
  749. $sql = e107::getDb();
  750. $num_rows = $sql->gen("SELECT * FROM #core WHERE e107_name='".$this->table."' ");
  751. if ($num_rows == 0)
  752. {
  753. $p = $this->default_prefs();
  754. $tmp = $this->e107->arrayStorage->WriteArray($p);
  755. $sql->insert("core", "'".$this->table."', '{$tmp}' ");
  756. }
  757. else
  758. {
  759. $row = $sql->fetch();
  760. //assign new preferences
  761. foreach($_POST as $k => $v)
  762. {
  763. if(preg_match("#^tagwords_#",$k))
  764. {
  765. $tagwords_pref[$k] = e107::getParser()->toDB($v);
  766. }
  767. }
  768. $this->pref = $tagwords_pref;
  769. //create new array of preferences
  770. $tmp = $this->e107->arrayStorage->WriteArray($tagwords_pref);
  771. $sql->update("core", "e107_value = '{$tmp}' WHERE e107_name = '".$this->table."' ");
  772. }
  773. return;
  774. }
  775. /*
  776. * Validate tagword db, removes all non-existing records
  777. * in case a db entry has been deleted but the tagwords were not
  778. */
  779. function validate()
  780. {
  781. $sql = e107::getDb();
  782. global $sql2;
  783. if($sql->gen("SELECT * FROM #".$this->table." GROUP BY tag_type, tag_itemid ORDER BY tag_id"))
  784. {
  785. while($row= $sql->fetch())
  786. {
  787. if(array_key_exists($row['tag_type'], $this->mapper))
  788. {
  789. $name = "e_tagwords_{$this->mapper[$row['tag_type']]}";
  790. if(!$sql2->gen("SELECT * FROM #".$row['tag_type']." WHERE ".$this->$name->settings['db_id']." = '".$row['tag_itemid']."' "))
  791. {
  792. $sql2->delete($this->table, "tag_type='".$row['tag_type']."' AND tag_itemid='".$row['tag_itemid']."' ");
  793. }
  794. }
  795. }
  796. }
  797. }
  798. /*
  799. * Options
  800. * @return string tablerender
  801. */
  802. function tagwords_options()
  803. {
  804. $this->validate();
  805. $text = e107::getParser()->parseTemplate($this->template['admin_options'], true, $this->shortcodes);
  806. e107::getRender()->tablerender(LAN_TAG_OPT_1, $text);
  807. }
  808. } //end class
  809. ?>