<?php

/**
 * @copyright (C) 2013 iJoomla, Inc. - All rights reserved.
 * @license GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html)
 * @author iJoomla.com <webmaster@ijoomla.com>
 * @url https://www.jomsocial.com/license-agreement
 * The PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript *are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0
 * More info at https://www.jomsocial.com/license-agreement
 */

use Joomla\CMS\Factory;
use Joomla\CMS\Table\Table;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Folder;

defined('_JEXEC') or die('Restricted access');

class CommunityModelFiles extends JCCModel implements CLimitsInterface {

    /**
     * Get List of available Files
     * @param  [string] $type       [is it group,event,profile or etc]
     * @param  [int]    $id         [groupid,eventid,profile id]
     * @param  integer  $limitstart [start limit]
     * @param  integer  $limit      [limit]
     * @param  [string] $extension  [is it doc,img,multimedia]
     * @return [object] result of file
     */
    public function getFileList($type, $id, $limitstart = 0, $limit = 8, $extension = null) {
        $type = $type . 'id';
        $db = Factory::getDBO();
        $defaultextension = array('document', 'archive', 'images', 'multimedia', 'miscellaneous');

        if ($extension && in_array($extension, $defaultextension, true)) {
            $extrasql = ' AND ' . $db->quoteName('type') . ' = ' . $db->Quote($extension);
        } elseif ($extension) {
            $extrasql = ' AND ' . $db->quoteName('name') . ' LIKE ' . $db->Quote('%' . $extension . '%');
        } else {
            $extrasql = '';
        }

        $query = 'SELECT * FROM '
                . $db->quoteName('#__community_files') . ' '
                . 'WHERE ' . $db->quoteName($type) . '=' . $db->Quote($id)
                . $extrasql
                . " ORDER BY id DESC";

        $db->setQuery($query, $limitstart, $limit);
        $result = $db->loadObjectList();

        return $result;
    }

    /**
     * Get Count for groups file listing for each section
     * @param  [int] $groupId   [group id]
     * @param  string $extension [mostdownload,img,doc]
     * @param  string $field     [which field to check]
     * @return [int]  no of file
     */
    public function getGroupFileCount($groupId, $extension = 'mostdownload', $field = 'groupid') {
        $db = Factory::getDBO();

        if ($extension == 'mostdownload') {
            $extrasql = '';
        } else {
            $extrasql = ' AND ' . $db->quoteName('type') . ' = ' . $db->Quote($extension);
        }

        $query = 'SELECT COUNT(*) FROM ' . $db->quoteName('#__community_files')
                . ' WHERE ' . $db->quoteName($field) . ' = ' . $db->Quote($groupId)
                . $extrasql;


        $db->setQuery($query);

        $count = $db->loadResult();

        return $count;
    }

    /**
     * Return total photos for the day for the specific user.
     *
     * @param	string	$userId	The specific userid.
     * */
    function getTotalToday($userId) {
        $db = Factory::getDBO();
        $date = JDate::getInstance();

        $query = 'SELECT COUNT(*) FROM ' . $db->quoteName('#__community_files')
                . ' AS a WHERE ' . $db->quoteName('creator') . '=' . $db->Quote($userId)
                . ' AND TO_DAYS(' . $db->Quote($date->toSql(true)) . ') - TO_DAYS( DATE_ADD( a.'.$db->quoteName('created').' , INTERVAL ' . $date->getOffset() . ' HOUR ) ) = 0 ';

        $db->setQuery($query);
        return $db->loadResult();
    }

    /**
     * Get top download file
     * @param  [int]    $groupId    [group id]
     * @param  integer  $limitstart [query start limit]
     * @param  [int]    $limit      [quer row limit]
     * @param  [string] $type       [which field need to search]
     * @return [object] Object list of top downloaded file
     */
    public function getTopDownload($groupId, $limitstart = 0, $limit = 20, $type = '') {
        $db = Factory::getDBO();

        $query = 'SELECT * FROM ' . $db->quoteName('#__community_files')
                . ' WHERE ' . $db->quoteName($type) . ' = ' . $db->Quote($groupId)
                . ' ORDER BY ' . $db->quoteName('hits') . ' DESC';
        $db->setQuery($query, $limitstart, $limit);

        return $db->loadObjectList();
    }

    /**
     * Delete all files
     * @param  [int] $typeId [group,event,profile id]
     * @param  [string] $type   [group,event,profile]
     */
    public function alldelete($typeId, $type) {
        $db = Factory::getDBO();
        $type = $type . 'id';

        $query = 'SELECT ' . $db->quoteName('id') . ' FROM ' . $db->quoteName('#__community_files')
                . ' WHERE ' . $db->quoteName($type) . ' = ' . $db->Quote($typeId);

        $db->setQuery($query);

        $data = $db->loadObjectList();

        $file = Table::getInstance('File', 'CTable');

        if (!empty($data)) {
            foreach ($data as $_data) {
                $file->load($_data->id);
                $file->delete();
            }
        }
    }

    /**
     * * Get if file is available for that specific type
     * @param $id   [group,event,profile id]
     * @param $type [group,event.profile]
     * @return bool/int
     */
    public function isfileAvailable($id, $type) {
        $db = Factory::getDBO();

        $query = 'SELECT COUNT(*) FROM ' . $db->quoteName('#__community_files')
                . ' WHERE ' . $db->quoteName($type . 'id') . ' = ' . $db->Quote($id);

        $db->setQuery($query);
        $total = $db->loadResult();

        return ($total > 0 ) ? $total : false;
    }

    /**
     * get all the files within the message ids
     * @param array $messageIds
     * @return bool
     */
    public function getMessageFiles($messageIds = array()){

        if(count($messageIds) > 0){
            $db = Factory::getDBO();

            $messageIds = implode(',',$messageIds);

            $query = 'SELECT * FROM ' . $db->quoteName('#__community_files')
                . ' WHERE ' . $db->quoteName('messageid') . ' IN (' . $messageIds.')';

            $db->setQuery($query);
            return $db->loadObjectList();
        }

        return false;
    }


    /**
     * Assign temporary file which belongs to a new message to the message id when the message is created
     * @param $chatId
     * @param $fileId
     * @param bool|false $groupChat
     * @return bool
     */
    public function updateChatFile($chatId, $fileId, $groupChat = false){
        //first verify if the file is valid and still unassigned
        $file = Table::getInstance('File', 'CTable');
        $file->load($fileId);

        if($file->messageid != '-1' || $file->creator != CFactory::getUser()->id){
            //-1 means its unassigned
            return false;
        }

        $folderType =($groupChat) ? '/singlechat/' : '/groupchat/';

        $file->messageid = $chatId;
        $tempPathFile = $file->filepath;
        $file->filepath = str_replace("/temp/", "/".$chatId."/", $file->filepath);

        if (!is_file(JPATH_ROOT . $folderType . $chatId)) {
            Folder::create(JPATH_ROOT . '/images/files' . $folderType . $chatId, (int) octdec(CFactory::getConfig()->get('folderpermissionsphoto')));
            File::copy(JPATH_ROOT . '/components/com_community/index.html', JPATH_ROOT . '/images/files'.$folderType . $chatId . '/index.html');
        }

        //lets move the folder of temp folder to the actual folder
        File::move(JPATH_ROOT . '/' .$tempPathFile, JPATH_ROOT . '/' . $file->filepath);

        $file->store();

        return $file->id;
    }

}
