com_extensiondistributor/admin/models/releases.php

579 lines
16 KiB
PHP

<?php
/*----------------------------------------------------------------------------------| www.vdm.io |----/
Agence Agerix
/-------------------------------------------------------------------------------------------------------/
@version 1.x.x
@build 2nd June, 2022
@created 12th December, 2020
@package Extension Distributor
@subpackage releases.php
@author Emmanuel Danan <https://agerix.fr>
@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;
/**
* Releases List Model
*/
class ExtensiondistributorModelReleases 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',
'g.name','extension',
'h.name','package',
'a.reltype','reltype',
'a.stability','stability',
'a.version_number','version_number',
'a.release_date','release_date'
);
}
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);
$extension = $this->getUserStateFromRequest($this->context . '.filter.extension', 'filter_extension');
if ($formSubmited)
{
$extension = $app->input->post->get('extension');
$this->setState('filter.extension', $extension);
}
$package = $this->getUserStateFromRequest($this->context . '.filter.package', 'filter_package');
if ($formSubmited)
{
$package = $app->input->post->get('package');
$this->setState('filter.package', $package);
}
$reltype = $this->getUserStateFromRequest($this->context . '.filter.reltype', 'filter_reltype');
if ($formSubmited)
{
$reltype = $app->input->post->get('reltype');
$this->setState('filter.reltype', $reltype);
}
$stability = $this->getUserStateFromRequest($this->context . '.filter.stability', 'filter_stability');
if ($formSubmited)
{
$stability = $app->input->post->get('stability');
$this->setState('filter.stability', $stability);
}
$version_number = $this->getUserStateFromRequest($this->context . '.filter.version_number', 'filter_version_number');
if ($formSubmited)
{
$version_number = $app->input->post->get('version_number');
$this->setState('filter.version_number', $version_number);
}
$release_date = $this->getUserStateFromRequest($this->context . '.filter.release_date', 'filter_release_date');
if ($formSubmited)
{
$release_date = $app->input->post->get('release_date');
$this->setState('filter.release_date', $release_date);
}
// 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
}
}
// set selection value to a translatable value
if (ExtensiondistributorHelper::checkArray($items))
{
foreach ($items as $nr => &$item)
{
// convert reltype
$item->reltype = $this->selectionTranslation($item->reltype, 'reltype');
// convert stability
$item->stability = $this->selectionTranslation($item->stability, 'stability');
}
}
// return items
return $items;
}
/**
* Method to convert selection values to translatable string.
*
* @return translatable string
*/
public function selectionTranslation($value,$name)
{
// Array of reltype language strings
if ($name === 'reltype')
{
$reltypeArray = array(
0 => 'COM_EXTENSIONDISTRIBUTOR_RELEASE_PLEASE_SELECT',
'security' => 'COM_EXTENSIONDISTRIBUTOR_RELEASE_SECURITY',
'maintenance' => 'COM_EXTENSIONDISTRIBUTOR_RELEASE_MAINTENANCE',
'newversion' => 'COM_EXTENSIONDISTRIBUTOR_RELEASE_NEW_VERSION'
);
// Now check if value is found in this array
if (isset($reltypeArray[$value]) && ExtensiondistributorHelper::checkString($reltypeArray[$value]))
{
return $reltypeArray[$value];
}
}
// Array of stability language strings
if ($name === 'stability')
{
$stabilityArray = array(
0 => 'COM_EXTENSIONDISTRIBUTOR_RELEASE_DEVELOPMENT',
1 => 'COM_EXTENSIONDISTRIBUTOR_RELEASE_ALPHA',
2 => 'COM_EXTENSIONDISTRIBUTOR_RELEASE_BETA',
3 => 'COM_EXTENSIONDISTRIBUTOR_RELEASE_RC',
4 => 'COM_EXTENSIONDISTRIBUTOR_RELEASE_STABLE'
);
// Now check if value is found in this array
if (isset($stabilityArray[$value]) && ExtensiondistributorHelper::checkString($stabilityArray[$value]))
{
return $stabilityArray[$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_release', 'a'));
// From the extensiondistributor_extension table.
$query->select($db->quoteName('g.name','extension_name'));
$query->join('LEFT', $db->quoteName('#__extensiondistributor_extension', 'g') . ' ON (' . $db->quoteName('a.extension') . ' = ' . $db->quoteName('g.id') . ')');
// From the extensiondistributor_package table.
$query->select($db->quoteName('h.name','package_name'));
$query->join('LEFT', $db->quoteName('#__extensiondistributor_package', 'h') . ' ON (' . $db->quoteName('a.package') . ' = ' . $db->quoteName('h.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.extension LIKE '.$search.' OR a.package LIKE '.$search.' OR a.version_number LIKE '.$search.' OR a.reltype LIKE '.$search.' OR a.description LIKE '.$search.' OR a.changelog LIKE '.$search.' OR a.stability LIKE '.$search.')');
}
}
// Filter by Extension.
$_extension = $this->getState('filter.extension');
if (is_numeric($_extension))
{
if (is_float($_extension))
{
$query->where('a.extension = ' . (float) $_extension);
}
else
{
$query->where('a.extension = ' . (int) $_extension);
}
}
elseif (ExtensiondistributorHelper::checkString($_extension))
{
$query->where('a.extension = ' . $db->quote($db->escape($_extension)));
}
elseif (ExtensiondistributorHelper::checkArray($_extension))
{
// Secure the array for the query
$_extension = 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));
}
}, $_extension);
// Filter by the Extension Array.
$query->where('a.extension IN (' . implode(',', $_extension) . ')');
}
// 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 Reltype.
$_reltype = $this->getState('filter.reltype');
if (is_numeric($_reltype))
{
if (is_float($_reltype))
{
$query->where('a.reltype = ' . (float) $_reltype);
}
else
{
$query->where('a.reltype = ' . (int) $_reltype);
}
}
elseif (ExtensiondistributorHelper::checkString($_reltype))
{
$query->where('a.reltype = ' . $db->quote($db->escape($_reltype)));
}
elseif (ExtensiondistributorHelper::checkArray($_reltype))
{
// Secure the array for the query
$_reltype = 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));
}
}, $_reltype);
// Filter by the Reltype Array.
$query->where('a.reltype IN (' . implode(',', $_reltype) . ')');
}
// Filter by Stability.
$_stability = $this->getState('filter.stability');
if (is_numeric($_stability))
{
if (is_float($_stability))
{
$query->where('a.stability = ' . (float) $_stability);
}
else
{
$query->where('a.stability = ' . (int) $_stability);
}
}
elseif (ExtensiondistributorHelper::checkString($_stability))
{
$query->where('a.stability = ' . $db->quote($db->escape($_stability)));
}
elseif (ExtensiondistributorHelper::checkArray($_stability))
{
// Secure the array for the query
$_stability = 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));
}
}, $_stability);
// Filter by the Stability Array.
$query->where('a.stability IN (' . implode(',', $_stability) . ')');
}
// 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
$_extension = $this->getState('filter.extension');
if (ExtensiondistributorHelper::checkArray($_extension))
{
$id .= ':' . implode(':', $_extension);
}
// Check if this is only an number or string
elseif (is_numeric($_extension)
|| ExtensiondistributorHelper::checkString($_extension))
{
$id .= ':' . $_extension;
}
// 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;
}
// Check if the value is an array
$_reltype = $this->getState('filter.reltype');
if (ExtensiondistributorHelper::checkArray($_reltype))
{
$id .= ':' . implode(':', $_reltype);
}
// Check if this is only an number or string
elseif (is_numeric($_reltype)
|| ExtensiondistributorHelper::checkString($_reltype))
{
$id .= ':' . $_reltype;
}
// Check if the value is an array
$_stability = $this->getState('filter.stability');
if (ExtensiondistributorHelper::checkArray($_stability))
{
$id .= ':' . implode(':', $_stability);
}
// Check if this is only an number or string
elseif (is_numeric($_stability)
|| ExtensiondistributorHelper::checkString($_stability))
{
$id .= ':' . $_stability;
}
$id .= ':' . $this->getState('filter.version_number');
$id .= ':' . $this->getState('filter.release_date');
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_release'));
// 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_release'))->set($fields)->where($conditions);
$db->setQuery($query);
$db->execute();
}
}
return false;
}
}