View file upload/src/addons/XenCentral/Feedback/Repository/Tools.php

File size: 4.78Kb
<?php
/**
 * @package XenCentral Feedback System
 * @author DNF Technology
 * @copyright Drnoyan & Nalyan LDA, Portugal, EU
 * @license http://dnf.technology/terms/
 * @link http://customers.dnf.technology
 * @version 2.0.0 Beta 10
 * @revision 12
 */

namespace XenCentral\Feedback\Repository;

use XF\Db\Exception;
use XF\Mvc\Entity\Finder;
use XF\Mvc\Entity\Repository;

class Tools extends Repository
{
    use FeedbackAbstractRepository;

    /**
     * @param $query String to match against thread title
     * @param array $users Two-value array containing parties of feedback
     * @param int $threadId Used to validate thread in data writer
     * @return array Array of threads found
     */
    public function getThreadSuggestions($query, $users = array(), $threadId = 0)
    {

        $starterOption = $this->_getOptionsModel()->_getOption('check_thread_starter');

        $validForumIDs = $this->_getOptionsModel()->getThreadValidForums();

        if (!empty($validForumIDs)) {
            $forumcondition = "thread.node_id IN(" . implode(',', $validForumIDs) . ")";
        } else {
            $forumcondition = '1=1';
        }

        $where = array($forumcondition);

        if ($threadId) {
            $where[] = 'thread.thread_id=' . $threadId;
        }

        if ($query) {
            $where[] = "thread.title LIKE " ."'" .\XF::app()->db()->escapeLike($query, '%?%') . "'";
        }

        $joins = array();

        if (!empty($users)) {
            $where[] = "thread.user_id IN(" . implode(',', $users) . ")";
            if ($threadId == 0 AND $this->_getOptionsModel()->getDisableThreadLiveCheck()) {
                // no join in case we are just searching for thread and performance fix is enabled
            } else {
                // strict validation for thread
                $joins[] = "
                    INNER JOIN xf_post AS post1 ON post1.thread_id=thread.thread_id AND post1.user_id=" . $users[0] . "
                    INNER JOIN xf_post AS post2 ON post2.thread_id=thread.thread_id AND post2.user_id=" . $users[1] . "
                ";
            }

        }

        $threads[0] = $this->db()->fetchAll("
			SELECT thread.* FROM xf_thread AS thread
			" . implode("\n", $joins) . "
			WHERE " . implode(" AND ", $where) . "
			GROUP BY thread.thread_id
			ORDER BY thread.post_date DESC
			LIMIT 30
		");

        if (isset($users) && $starterOption) {
            array_shift($users);

        }

        if ($this->checkIfAddonIsEnabled('XFRM')) {
            $whereRes = array();
            $validResources = $this->_getOptionsModel()->getValidResources();

            if (!empty($validResources)) {
                $rescondition = "resource.resource_category_id IN(" . implode(',', $validResources) . ")";
            } else {
                $rescondition = '1=1';
            }
            $whereRes[] = $rescondition;

            if ($threadId) {
                $whereRes[] = 'resource.resource_id=' . $threadId;
            }

            if ($query) {

                $whereRes[] = "resource.title LIKE " . "'" .\XF::app()->db()->escapeLike($query, '%?%') . "'";
            }
            if (!empty($users)) {
                $whereRes[] = "resource.user_id IN(" . implode(',', $users) . ")";
            }
            $threads[1] = $this->db()->fetchAll("
			SELECT resource.* FROM xf_rm_resource AS resource
			WHERE " . implode(" AND ", $whereRes) . "
			GROUP BY resource.resource_id
			ORDER BY resource.resource_date DESC
			LIMIT 30
		");

        }
        if ($this->checkIfAddonIsEnabled('XFMG')) {
            $whereMedia = array();
            $validMedia = $this->_getOptionsModel()->getValidMedia();

            if (!empty($validMedia)) {
                $mediacondition = "media.category_id IN(" . implode(',', $validMedia) . ")";
            } else {
                $mediacondition = '1=1';
            }
            $whereMedia[] = $mediacondition;

            if ($threadId) {
                $whereMedia[] = 'media.media_id=' . $threadId;

            }

            if ($query) {
                $whereMedia[] = "media.title LIKE " . "'" .\XF::app()->db()->escapeLike($query, '%?%') ."'" ;
            }
            if (!empty($users)) {
                $whereMedia[] = "media.user_id IN(" . implode(',', $users) . ")";
            }
            $threads[2] = $this->db()->fetchAll("
			SELECT media.* FROM xf_mg_media_item AS media
			WHERE " . implode(" AND ", $whereMedia) . "
			GROUP BY media.media_id
			ORDER BY media.media_date DESC
			LIMIT 30
		");

        }
        return $threads;
    }

    protected function checkIfAddonIsEnabled($addonid)
    {
        $addon = \XF::repository('XF:AddOn')->getEnabledAddOns();

            if (array_key_exists($addonid, $addon)) {
                return true;

            } else {

                return false;
            }
    }
}