@copyright Copyright (C) 2021. All Rights Reserved. @license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html ____ _____ _____ __ __ __ __ ___ _____ __ __ ____ _____ _ _ ____ _ _ ____ (_ _)( _ )( _ )( \/ )( ) /__\ / __)( _ )( \/ )( _ \( _ )( \( )( ___)( \( )(_ _) .-_)( )(_)( )(_)( ) ( )(__ /(__)\ ( (__ )(_)( ) ( )___/ )(_)( ) ( )__) ) ( )( \____) (_____)(_____)(_/\/\_)(____)(__)(__) \___)(_____)(_/\/\_)(__) (_____)(_)\_)(____)(_)\_) (__) /------------------------------------------------------------------------------------------------------*/ // No direct access to this file defined('_JEXEC') or die('Restricted access'); use Joomla\CMS\MVC\Model\ListModel; use Joomla\Utilities\ArrayHelper; /** * Subscriptions List Model */ class ExtensiondistributorModelSubscriptions extends ListModel { public function __construct($config = array()) { if (empty($config['filter_fields'])) { $config['filter_fields'] = array( 'a.id','id', 'a.published','published', 'a.ordering','ordering', 'a.created_by','created_by', 'a.modified_by','modified_by', 'a.user','user', 'g.name','package', 'a.active','active', 'a.date_start','date_start', 'a.date_expire','date_expire' ); } parent::__construct($config); } /** * Method to auto-populate the model state. * * Note. Calling getState in this method will result in recursion. * * @param string $ordering An optional ordering field. * @param string $direction An optional direction (asc|desc). * * @return void * */ protected function populateState($ordering = null, $direction = null) { $app = JFactory::getApplication(); // Adjust the context to support modal layouts. if ($layout = $app->input->get('layout')) { $this->context .= '.' . $layout; } // Check if the form was submitted $formSubmited = $app->input->post->get('form_submited'); $access = $this->getUserStateFromRequest($this->context . '.filter.access', 'filter_access', 0, 'int'); if ($formSubmited) { $access = $app->input->post->get('access'); $this->setState('filter.access', $access); } $published = $this->getUserStateFromRequest($this->context . '.filter.published', 'filter_published', ''); $this->setState('filter.published', $published); $created_by = $this->getUserStateFromRequest($this->context . '.filter.created_by', 'filter_created_by', ''); $this->setState('filter.created_by', $created_by); $created = $this->getUserStateFromRequest($this->context . '.filter.created', 'filter_created'); $this->setState('filter.created', $created); $sorting = $this->getUserStateFromRequest($this->context . '.filter.sorting', 'filter_sorting', 0, 'int'); $this->setState('filter.sorting', $sorting); $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search'); $this->setState('filter.search', $search); $user = $this->getUserStateFromRequest($this->context . '.filter.user', 'filter_user'); if ($formSubmited) { $user = $app->input->post->get('user'); $this->setState('filter.user', $user); } $package = $this->getUserStateFromRequest($this->context . '.filter.package', 'filter_package'); if ($formSubmited) { $package = $app->input->post->get('package'); $this->setState('filter.package', $package); } $active = $this->getUserStateFromRequest($this->context . '.filter.active', 'filter_active'); if ($formSubmited) { $active = $app->input->post->get('active'); $this->setState('filter.active', $active); } $date_start = $this->getUserStateFromRequest($this->context . '.filter.date_start', 'filter_date_start'); if ($formSubmited) { $date_start = $app->input->post->get('date_start'); $this->setState('filter.date_start', $date_start); } $date_expire = $this->getUserStateFromRequest($this->context . '.filter.date_expire', 'filter_date_expire'); if ($formSubmited) { $date_expire = $app->input->post->get('date_expire'); $this->setState('filter.date_expire', $date_expire); } // List state information. parent::populateState($ordering, $direction); } /** * Method to get an array of data items. * * @return mixed An array of data items on success, false on failure. */ public function getItems() { // Check in items $this->checkInNow(); // load parent items $items = parent::getItems(); // Set values to display correctly. if (ExtensiondistributorHelper::checkArray($items)) { // Get the user object if not set. if (!isset($user) || !ExtensiondistributorHelper::checkObject($user)) { $user = JFactory::getUser(); } foreach ($items as $nr => &$item) { // Not needed // Not needed } } // set selection value to a translatable value if (ExtensiondistributorHelper::checkArray($items)) { foreach ($items as $nr => &$item) { // convert active $item->active = $this->selectionTranslation($item->active, 'active'); } } // return items return $items; } /** * Method to convert selection values to translatable string. * * @return translatable string */ public function selectionTranslation($value,$name) { // Array of active language strings if ($name === 'active') { $activeArray = array( 1 => 'COM_EXTENSIONDISTRIBUTOR_SUBSCRIPTION_YES', 0 => 'COM_EXTENSIONDISTRIBUTOR_SUBSCRIPTION_NO' ); // Now check if value is found in this array if (isset($activeArray[$value]) && ExtensiondistributorHelper::checkString($activeArray[$value])) { return $activeArray[$value]; } } return $value; } /** * Method to build an SQL query to load the list data. * * @return string An SQL query */ protected function getListQuery() { // Get the user object. $user = JFactory::getUser(); // Create a new query object. $db = JFactory::getDBO(); $query = $db->getQuery(true); // Select some fields $query->select('a.*'); // From the extensiondistributor_item table $query->from($db->quoteName('#__extensiondistributor_subscription', 'a')); // From the extensiondistributor_package table. $query->select($db->quoteName('g.name','package_name')); $query->join('LEFT', $db->quoteName('#__extensiondistributor_package', 'g') . ' ON (' . $db->quoteName('a.package') . ' = ' . $db->quoteName('g.id') . ')'); // Filter by published state $published = $this->getState('filter.published'); if (is_numeric($published)) { $query->where('a.published = ' . (int) $published); } elseif ($published === '') { $query->where('(a.published = 0 OR a.published = 1)'); } // Filter by search. $search = $this->getState('filter.search'); if (!empty($search)) { if (stripos($search, 'id:') === 0) { $query->where('a.id = ' . (int) substr($search, 3)); } else { $search = $db->quote('%' . $db->escape($search) . '%'); $query->where('(a.user LIKE '.$search.' OR a.package LIKE '.$search.' OR g.name LIKE '.$search.' OR a.date_start LIKE '.$search.' OR a.date_expire LIKE '.$search.' OR a.active LIKE '.$search.' OR a.comment LIKE '.$search.')'); } } // Filter by User. $_user = $this->getState('filter.user'); if (is_numeric($_user)) { if (is_float($_user)) { $query->where('a.user = ' . (float) $_user); } else { $query->where('a.user = ' . (int) $_user); } } elseif (ExtensiondistributorHelper::checkString($_user)) { $query->where('a.user = ' . $db->quote($db->escape($_user))); } elseif (ExtensiondistributorHelper::checkArray($_user)) { // Secure the array for the query $_user = array_map( function ($val) use(&$db) { if (is_numeric($val)) { if (is_float($val)) { return (float) $val; } else { return (int) $val; } } elseif (ExtensiondistributorHelper::checkString($val)) { return $db->quote($db->escape($val)); } }, $_user); // Filter by the User Array. $query->where('a.user IN (' . implode(',', $_user) . ')'); } // Filter by Package. $_package = $this->getState('filter.package'); if (is_numeric($_package)) { if (is_float($_package)) { $query->where('a.package = ' . (float) $_package); } else { $query->where('a.package = ' . (int) $_package); } } elseif (ExtensiondistributorHelper::checkString($_package)) { $query->where('a.package = ' . $db->quote($db->escape($_package))); } elseif (ExtensiondistributorHelper::checkArray($_package)) { // Secure the array for the query $_package = array_map( function ($val) use(&$db) { if (is_numeric($val)) { if (is_float($val)) { return (float) $val; } else { return (int) $val; } } elseif (ExtensiondistributorHelper::checkString($val)) { return $db->quote($db->escape($val)); } }, $_package); // Filter by the Package Array. $query->where('a.package IN (' . implode(',', $_package) . ')'); } // Filter by Active. $_active = $this->getState('filter.active'); if (is_numeric($_active)) { if (is_float($_active)) { $query->where('a.active = ' . (float) $_active); } else { $query->where('a.active = ' . (int) $_active); } } elseif (ExtensiondistributorHelper::checkString($_active)) { $query->where('a.active = ' . $db->quote($db->escape($_active))); } // Add the list ordering clause. $orderCol = $this->state->get('list.ordering', 'a.id'); $orderDirn = $this->state->get('list.direction', 'desc'); if ($orderCol != '') { $query->order($db->escape($orderCol . ' ' . $orderDirn)); } return $query; } /** * Method to get a store id based on model configuration state. * * @return string A store id. * */ protected function getStoreId($id = '') { // Compile the store id. $id .= ':' . $this->getState('filter.id'); $id .= ':' . $this->getState('filter.search'); $id .= ':' . $this->getState('filter.published'); $id .= ':' . $this->getState('filter.ordering'); $id .= ':' . $this->getState('filter.created_by'); $id .= ':' . $this->getState('filter.modified_by'); // Check if the value is an array $_user = $this->getState('filter.user'); if (ExtensiondistributorHelper::checkArray($_user)) { $id .= ':' . implode(':', $_user); } // Check if this is only an number or string elseif (is_numeric($_user) || ExtensiondistributorHelper::checkString($_user)) { $id .= ':' . $_user; } // Check if the value is an array $_package = $this->getState('filter.package'); if (ExtensiondistributorHelper::checkArray($_package)) { $id .= ':' . implode(':', $_package); } // Check if this is only an number or string elseif (is_numeric($_package) || ExtensiondistributorHelper::checkString($_package)) { $id .= ':' . $_package; } $id .= ':' . $this->getState('filter.active'); $id .= ':' . $this->getState('filter.date_start'); $id .= ':' . $this->getState('filter.date_expire'); return parent::getStoreId($id); } /** * Build an SQL query to checkin all items left checked out longer then a set time. * * @return a bool * */ protected function checkInNow() { // Get set check in time $time = JComponentHelper::getParams('com_extensiondistributor')->get('check_in'); if ($time) { // Get a db connection. $db = JFactory::getDbo(); // Reset query. $query = $db->getQuery(true); $query->select('*'); $query->from($db->quoteName('#__extensiondistributor_subscription')); // Only select items that are checked out. $query->where($db->quoteName('checked_out') . '!=0'); $db->setQuery($query, 0, 1); $db->execute(); if ($db->getNumRows()) { // Get Yesterdays date. $date = JFactory::getDate()->modify($time)->toSql(); // Reset query. $query = $db->getQuery(true); // Fields to update. $fields = array( $db->quoteName('checked_out_time') . '=\'0000-00-00 00:00:00\'', $db->quoteName('checked_out') . '=0' ); // Conditions for which records should be updated. $conditions = array( $db->quoteName('checked_out') . '!=0', $db->quoteName('checked_out_time') . '<\''.$date.'\'' ); // Check table. $query->update($db->quoteName('#__extensiondistributor_subscription'))->set($fields)->where($conditions); $db->setQuery($query); $db->execute(); } } return false; } }