1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22: 23: 24:
25: class ArticleForumCollection extends ItemCollection {
26:
27: 28: 29: 30:
31: protected $cfg = 0;
32:
33: 34: 35: 36:
37: protected $db = 0;
38:
39: 40: 41: 42:
43: protected $item = 0;
44:
45:
46: 47: 48: 49:
50: protected $languageSync = 0;
51:
52: 53: 54: 55:
56: protected $idContentType = 0;
57:
58: 59:
60: public function __construct() {
61:
62: date_default_timezone_set('Europe/Berlin');
63: $this->db = cRegistry::getDb();
64: $this->cfg = cRegistry::getConfig();
65:
66: parent::__construct($this->cfg['tab']['user_forum'], 'id_user_forum');
67: $this->_setItemClass('ArticleForum');
68: $this->item = new ArticleForumItem();
69: $this->idContentType = $this->getIdUserForumContenType();
70: }
71:
72: 73: 74: 75:
76: public function getAllCommentedArticles() {
77:
78: $idclient = cRegistry::getClientId();
79:
80:
81: $this->db->query("-- ArticleForumCollection->getAllCommentedArticles()
82: SELECT DISTINCT
83: art_lang.title
84: , art_lang.idart
85: , f.idcat
86: FROM
87: `{$this->cfg['tab']['art_lang']}` AS art_lang
88: , `$this->table` AS f
89: WHERE
90: art_lang.idart = f.idart
91: AND art_lang.idlang = f.idlang
92: AND idclient = " . $idclient . "
93: ORDER BY
94: id_user_forum ASC
95: ;");
96:
97: $data = array();
98: while ($this->db->nextRecord()) {
99: array_push($data, $this->db->toArray());
100: }
101:
102: return $data;
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112: 113:
114: public function deleteHierarchy($keyPost, $level, $idart, $idcat, $lang) {
115: $comments = $this->_getCommentHierarchy($idcat, $idart, $lang);
116:
117: $arri = array();
118:
119: foreach ($comments as $key => $com) {
120: $com['key'] = $key;
121: $arri[] = $com;
122: }
123: $idEntry = 0;
124: $id_user_forum = array();
125: $lastLevel = 0;
126: for ($i = 0; $i < count($arri); $i++) {
127:
128: if ($arri[$i]['key'] == $keyPost) {
129: $idEntry = $arri[$i]['id_user_forum'];
130: if ($arri[$i]['level'] < $arri[$i + 1]['level']) {
131:
132: for ($j = $i + 1; $j < $arri[$j]; $j++) {
133: if ($arri[$i]['level'] < $arri[$j]['level']) {
134: $id_user_forum[] = $arri[$j]['id_user_forum'];
135: }
136: }
137: }
138: }
139: }
140:
141: if (empty($id_user_forum)) {
142: $this->deleteBy('id_user_forum', $idEntry);
143: } else {
144: $this->deleteBy('id_user_forum', $idEntry);
145: foreach ($id_user_forum as $com) {
146: $this->deleteBy('id_user_forum', $com);
147: }
148: }
149: }
150:
151: 152: 153: 154: 155: 156: 157: 158:
159: protected function ($id_cat, $id_art, $id_lang) {
160: $this->query();
161: while (false != $field = $this->next()) {
162: $arrUsers[$field->get('userid')]['email'] = $field->get('email');
163: $arrUsers[$field->get('userid')]['realname'] = $field->get('realname');
164: }
165: $arrforum = array();
166: $this->getTreeLevel($id_cat, $id_art, $id_lang, $arrUsers, $arrforum);
167: $result = array();
168: $this->normalizeArray($arrforum, $result);
169:
170: return $result;
171: }
172:
173: 174: 175: 176: 177: 178:
179: public function normalizeArray($arrforum, &$result, $level = 0) {
180: if (is_array($arrforum)) {
181: foreach ($arrforum as $key => $value) {
182: $value['level'] = $level;
183: unset($value['children']);
184: $result[$key] = $value;
185: $this->normalizeArray($arrforum[$key]['children'], $result, $level + 1);
186: }
187: }
188: }
189:
190: 191: 192: 193: 194: 195: 196: 197: 198: 199:
200: public function getTreeLevel($id_cat, $id_art, $id_lang, &$arrUsers, &$arrforum, $parent = 0, $frontend = false) {
201: $db = cRegistry::getDb();
202: if ($frontend) {
203:
204: $db->query("-- ArticleForumCollection->getTreeLevel()
205: SELECT
206: *
207: FROM
208: `{$this->cfg['tab']['user_forum']}`
209: WHERE
210: idart = $id_art
211: AND idcat = $id_cat
212: AND idlang = $id_lang
213: AND id_user_forum_parent = $parent
214: AND online = 1
215: ORDER BY
216: timestamp DESC
217: ;");
218: } else {
219:
220: $db->query("-- ArticleForumCollection->getTreeLevel()
221: SELECT
222: *
223: FROM
224: `{$this->cfg['tab']['user_forum']}`
225: WHERE
226: idart = $id_art
227: AND idcat = $id_cat
228: AND idlang = $id_lang
229: AND id_user_forum_parent = $parent
230: ORDER BY
231: timestamp DESC
232: ;");
233: }
234:
235: while ($db->nextRecord()) {
236: $arrforum[$db->f('id_user_forum')]['userid'] = $db->f('userid');
237:
238: if (array_key_exists($db->f('userid'), $arrUsers)) {
239: $arrforum[$db->f('id_user_forum')]['email'] = $arrUsers[$db->f('userid')]['email'];
240: $arrforum[$db->f('id_user_forum')]['realname'] = $arrUsers[$db->f('userid')]['realname'];
241: } else {
242: $arrforum[$db->f('id_user_forum')]['email'] = $db->f('email');
243: $arrforum[$db->f('id_user_forum')]['realname'] = $db->f('realname');
244: }
245: $arrforum[$db->f('id_user_forum')]['forum'] = str_replace(chr(13) . chr(10), '<br />', $db->f('forum'));
246: $arrforum[$db->f('id_user_forum')]['forum_quote'] = str_replace(chr(13) . chr(10), '<br />', $db->f('forum_quote'));
247: $arrforum[$db->f('id_user_forum')]['timestamp'] = $db->f('timestamp');
248: $arrforum[$db->f('id_user_forum')]['like'] = $db->f('like');
249: $arrforum[$db->f('id_user_forum')]['dislike'] = $db->f('dislike');
250:
251: $arrforum[$db->f('id_user_forum')]['editedat'] = $db->f('editedat');
252: $arrforum[$db->f('id_user_forum')]['editedby'] = $db->f('editedby');
253:
254:
255: $arrforum[$db->f('id_user_forum')]['idcat'] = $db->f('idcat');
256: $arrforum[$db->f('id_user_forum')]['idart'] = $db->f('idart');
257: $arrforum[$db->f('id_user_forum')]['id_user_forum'] = $db->f('id_user_forum');
258: $arrforum[$db->f('id_user_forum')]['online'] = $db->f('online');
259: $arrforum[$db->f('id_user_forum')]['editedat'] = $db->f('editedat');
260: $arrforum[$db->f('id_user_forum')]['editedby'] = $db->f('editedby');
261:
262: $this->getTreeLevel($id_cat, $id_art, $id_lang, $arrUsers, $arrforum[$db->f('id_user_forum')]['children'], $db->f('id_user_forum'), $frontend);
263: }
264: }
265:
266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276:
277: public function updateValues($id_user_forum, $name, $email, $like, $dislike, $forum, $online) {
278: $uuid = cRegistry::getAuth()->isAuthenticated();
279:
280: $this->item->loadByPrimaryKey($id_user_forum);
281:
282: if ($this->item->getField('realname') == $name && $this->item->getField('email') == $email && $this->item->getField('forum') == $forum) {
283:
284:
285:
286: if ($this->item->getField('editedat') === "0000-00-00 00:00:00") {
287:
288: $timeStamp = "0000-00-00 00:00:00";
289: } else {
290: $timeStamp = $this->item->getField('editedat');
291: }
292: } else {
293:
294: $timeStamp = date('Y-m-d H:i:s', time());
295: }
296:
297: if (preg_match('/\D/', $like)) {
298: $like = $this->item->getField('like');
299: }
300:
301: if (preg_match('/\D/', $dislike)) {
302: $dislike = $this->item->getField('dislike');
303: }
304:
305:
306:
307:
308:
309:
310:
311:
312: $fields = array(
313: 'realname' => $name,
314: 'editedby' => $uuid,
315: 'email' => $email,
316: 'forum' => $forum,
317: 'editedat' => $timeStamp,
318: 'like' => $like,
319: 'dislike' => $dislike,
320: 'online' => $online,
321:
322: 'moderated' => 1
323: );
324:
325: $whereClauses = array(
326: 'id_user_forum' => $id_user_forum
327: );
328: $statement = $this->db->buildUpdate($this->table, $fields, $whereClauses);
329: $this->db->query($statement);
330: }
331:
332: 333: 334: 335: 336: 337:
338: public function toggleOnlineState($onlineState, $id_user_forum, $idart = NULL) {
339:
340:
341: $onlineState = ($onlineState == 0) ? 1 : 0;
342:
343: if (isset($idart)) {
344: $fields = array(
345: 'online' => $onlineState,
346: 'moderated' => 1,
347: );
348: } else {
349: $fields = array(
350: 'online' => $onlineState
351: );
352: }
353:
354: $whereClauses = array(
355: 'id_user_forum' => (int)$id_user_forum
356: );
357: $statement = $this->db->buildUpdate($this->table, $fields, $whereClauses);
358: $this->db->query($statement);
359: }
360:
361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373:
374: public function mailToModerator($realname, $email, $forum, $idart, $lang, $forum_quote = 0) {
375:
376:
377: $ar = $this->getArticleTitle($idart, $lang);
378:
379: $mail = new cMailer();
380: $mail->setCharset('UTF-8');
381:
382:
383: $message = $this->languageSync['NEWENTRYTEXT'] . " " . $this->languageSync['ARTICLE'] . $ar[0]["title"] . "\n" . "\n";
384: $message .= $this->languageSync['USER'] . ' : ' . $realname . "\n";
385: $message .= $this->languageSync['EMAIL'] . ' : ' . $email . "\n" . "\n";
386: $message .= $this->languageSync['COMMENT'] . ' : ' . "\n" . $forum . "\n";
387: if ($forum_quote != 0) {
388: $message .= UserForum::i18n('QUOTE') . ' : ' . $forum_quote . "\n";
389: }
390:
391:
392: if ($this->getModEmail($idart) != NULL) {
393: $mail->sendMail(getEffectiveSetting("userforum", "mailfrom"), $this->getModEmail($idart), $this->languageSync['NEWENTRY'], $message);
394: }
395: }
396:
397: 398: 399: 400: 401: 402: 403:
404: public function getArticleTitle($idart, $idlang) {
405: $this->db->query("-- ArticleForumCollection->getArticleTitle()
406: SELECT DISTINCT
407: title
408: FROM
409: `{$this->cfg['tab']['art_lang']}` AS art_lang
410: WHERE
411: idart = $idart
412: AND idlang = $idlang
413: ;");
414:
415: $data = array();
416: while ($this->db->nextRecord()) {
417: array_push($data, $this->db->toArray());
418: }
419:
420: return $data;
421: }
422:
423: 424: 425:
426: public function getExistingforum() {
427: $userColl = new cApiUserCollection();
428: $userColl->query();
429:
430: while (($field = $userColl->next()) != false) {
431: $arrUsers[$field->get('user_id')]['email'] = $field->get('email');
432: $arrUsers[$field->get('user_id')]['realname'] = $field->get('realname');
433: }
434:
435: return $arrUsers;
436: }
437:
438: 439: 440: 441: 442: 443:
444: function selectNameAndNameByForumId($idquote) {
445: $ar = array();
446: $this->item->loadByPrimaryKey($this->db->escape($idquote));
447: $ar[] = $this->item->get('realname');
448:
449: return $ar;
450: }
451:
452: 453: 454: 455: 456:
457: public function selectUser($userid) {
458: return $this->item->loadByPrimaryKey($this->db->escape($userid));
459: }
460:
461: 462: 463: 464: 465: 466:
467: public function incrementLike($forum_user_id) {
468: $db = cRegistry::getDb();
469:
470: $this->item->loadByPrimaryKey($db->escape($forum_user_id));
471: $ar = $this->item->toArray();
472: $current = $ar['like'];
473:
474: $current += 1;
475:
476: $fields = array(
477: 'like' => $current
478: );
479: $whereClauses = array(
480: 'id_user_forum' => $forum_user_id
481: );
482:
483: $statement = $this->db->buildUpdate($this->table, $fields, $whereClauses);
484: $this->db->query($statement);
485: }
486:
487: 488: 489: 490: 491: 492:
493: public function incrementDislike($forum_user_id) {
494: $db = cRegistry::getDb();
495:
496: $this->item->loadByPrimaryKey($db->escape($forum_user_id));
497: $ar = $this->item->toArray();
498: $current = $ar['dislike'];
499:
500: $current += 1;
501:
502: $fields = array(
503: 'dislike' => $current
504: );
505: $whereClauses = array(
506: 'id_user_forum' => $forum_user_id
507: );
508:
509: $statement = $this->db->buildUpdate($this->table, $fields, $whereClauses);
510: $this->db->query($statement);
511: }
512:
513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525:
526: public function insertValues($parent, $idart, $idcat, $lang, $userid, $email, $realname, $forum, $forum_quote) {
527: $db = cRegistry::getDb();
528:
529:
530: $modCheck = $this->getModModeActive($idart);
531: $online = $modCheck ? 0 : 1;
532:
533:
534: $fields = array(
535: 'id_user_forum' => NULL,
536: 'id_user_forum_parent' => $db->escape($parent),
537: 'idart' => $db->escape($idart),
538: 'idcat' => $db->escape($idcat),
539: 'idlang' => $db->escape($lang),
540: 'userid' => $db->escape($userid),
541: 'email' => $db->escape($email),
542: 'realname' => $db->escape($realname),
543: 'forum' => ($forum),
544: 'forum_quote' => ($forum_quote),
545: 'idclient' => cRegistry::getClientId(),
546: 'like' => 0,
547: 'dislike' => 0,
548: 'editedat' => NULL,
549: 'editedby' => NULL,
550: 'timestamp' => date('Y-m-d H:i:s'),
551: 'online' => $online
552: );
553:
554: $db->insert($this->table, $fields);
555:
556:
557:
558:
559: if ($modCheck) {
560: $this->mailToModerator($realname, $email, $forum, $idart, $lang, $forum_quote = 0);
561: }
562: }
563:
564: 565: 566: 567: 568:
569: public function ($idart) {
570: $this->deleteBy('idart', (int)$idart);
571: }
572:
573: 574: 575: 576: 577: 578: 579: 580: 581:
582: public function getExistingforumFrontend($id_cat, $id_art, $id_lang, $frontend) {
583: $userColl = new cApiUserCollection();
584: $userColl->query();
585:
586: while (($field = $userColl->next()) != false) {
587: $arrUsers[$field->get('user_id')]['email'] = $field->get('email');
588: $arrUsers[$field->get('user_id')]['realname'] = $field->get('realname');
589: }
590:
591: $arrforum = array();
592: $this->getTreeLevel($id_cat, $id_art, $id_lang, $arrUsers, $arrforum, 0, $frontend);
593:
594: $result = array();
595: $this->normalizeArray($arrforum, $result);
596:
597: return $result;
598: }
599:
600: 601: 602: 603: 604: 605: 606:
607: public function getModEmail($idart) {
608: $data = $this->readXML();
609: for ($i = 0; $i < count($data); $i++) {
610: if ($data[$i]['idart'] == $idart) {
611: return $data[$i]["email"];
612: }
613: }
614:
615: return NULL;
616: }
617:
618: 619: 620: 621: 622: 623: 624:
625: public function getModModeActive($idart) {
626: $data = $this->readXML();
627: for ($i = 0; $i < count($data); $i++) {
628: if ($data[$i]['idart'] == $idart) {
629: if ($data[$i]["modactive"] === 'false') {
630: return false;
631: }
632: }
633: }
634:
635: return true;
636: }
637:
638: 639: 640: 641: 642: 643: 644:
645: public function getQuoteState($idart) {
646:
647: $data = $this->readXML();
648: for ($i = 0; $i < count($data); $i++) {
649: if ($data[$i]['idart'] == $idart) {
650: if ($data[$i]["subcomments"] === 'false') {
651: return false;
652: }
653: }
654: }
655:
656: return true;
657: }
658:
659: 660: 661: 662: 663: 664: 665:
666: public function readXML() {
667:
668: $idtype = $this->idContentType;
669:
670: try {
671: $this->db->query("-- ArticleForumCollection->readXML()
672: SELECT
673: art_lang.idart
674: , content.value
675: FROM
676: `{$this->cfg[tab][art_lang]}` AS art_lang
677: , `{$this->cfg[tab][content]}` AS content
678: WHERE
679: art_lang.idartlang = content.idartlang
680: AND content.idtype = $idtype
681: ;");
682:
683: $data = array();
684: while ($this->db->nextRecord()) {
685: array_push($data, $this->db->toArray());
686: }
687:
688: $array = array();
689: for ($i = 0; $i < count($data); $i++) {
690: $array[$i] = cXmlBase::xmlStringToArray($data[$i]['value']);
691:
692: $array[$i]['idart'] = $data[$i]['idart'];
693: }
694: } catch (Exception $e) {
695:
696: }
697:
698: return $array;
699: }
700:
701: 702: 703: 704: 705: 706: 707:
708: public function languageSync(array &$str) {
709: $this->languageSync = $str;
710: }
711:
712: 713: 714: 715:
716: public function getlanguageSync() {
717: if ($this->languageSync != 0) {
718: return $this->languageSync;
719: } else {
720: return array();
721: }
722: }
723:
724: 725: 726: 727: 728: 729:
730: public function ($id_user_forum) {
731: $item = $this->loadItem($id_user_forum);
732:
733: return array(
734: 'name' => $item->get("realname"),
735: 'content' => $item->get("forum")
736: );
737: }
738:
739: 740: 741: 742:
743: protected function getIdUserForumContenType() {
744: $this->db->query("-- ArticleForumCollection->getIdUserForumContenType()
745: SELECT
746: idtype
747: FROM
748: `{$this->cfg[tab][type]}`
749: WHERE
750: type = 'CMS_USERFORUM'
751: ;");
752: if ($this->db->nextRecord()) {
753: return $this->db->f('idtype');
754: } else {
755: return false;
756: }
757: }
758:
759:
760: public function () {
761:
762: $comments = array();
763:
764: $idlang = cRegistry::getLanguageId();
765: $idclient = cRegistry::getClientId();
766:
767: $db = cRegistry::getDb();
768: $db->query("-- ArticleForumCollection->getUnmoderatedComments()
769: SELECT
770: *
771: FROM
772: `{$this->cfg['tab']['user_forum']}`
773: WHERE
774: moderated = 0
775: AND idclient = $idclient
776: AND idlang = $idlang
777: AND online = 0
778: ORDER BY
779: timestamp DESC
780: ;");
781:
782: while ($db->nextRecord()) {
783:
784:
785: $modCheck = $this->getModModeActive($db->f('idart'));
786: if(isset($modCheck)) {
787: $comments[] = $db->getRecord();
788: }
789: }
790: return $comments;
791: }
792: }
793:
794: ?>