CoreFiles by mySites.guru
Globalcheckin.php122 lines, 3,594 bytes, from Joomla_6.2.0-Stable-Full_Package.zip
Raw History Compare with 6.1.4
md5
cf4d12710b0b63347522557e3a2ddda9
sha1
ad68bd9b90f3ba2afd7f3a999ae53d18bb00db08
sha256
ba2e7b619abeeb0e2032494f1274d85e7f66b1e7e22ed46910db0e6a874dcc6f
<?php

/**
 * @package     Joomla.Plugins
 * @subpackage  Task.Globalcheckin
 *
 * @copyright   (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\Plugin\Task\Globalcheckin\Extension;

use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
use Joomla\Component\Scheduler\Administrator\Task\Status as TaskStatus;
use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\Exception\ExecutionFailureException;
use Joomla\Event\SubscriberInterface;

/**
 * Task plugin with routines to check in a checked out item.
 *
 * @since  5.0.0
 */
class Globalcheckin extends CMSPlugin implements SubscriberInterface
{
    use DatabaseAwareTrait;
    use TaskPluginTrait;

    /**
     * @var string[]
     * @since 5.0.0
     */
    protected const TASKS_MAP = [
        'plg_task_globalcheckin_task_get' => [
            'langConstPrefix' => 'PLG_TASK_GLOBALCHECKIN',
            'form'            => 'globalcheckin_params',
            'method'          => 'makeCheckin',
        ],
    ];

    /**
     * @var boolean
     * @since 5.0.0
     */
    protected $autoloadLanguage = true;

    /**
     * @inheritDoc
     *
     * @return string[]
     *
     * @since 5.0.0
     */
    public static function getSubscribedEvents(): array
    {
        return [
            'onTaskOptionsList'    => 'advertiseRoutines',
            'onExecuteTask'        => 'standardRoutineHandler',
            'onContentPrepareForm' => 'enhanceTaskItemForm',
        ];
    }

    /**
     * Standard method for the checkin routine.
     *
     * @param   ExecuteTaskEvent  $event  The onExecuteTask event
     *
     * @return  integer  The exit code
     *
     * @since   5.0.0
     */
    protected function makeCheckin(ExecuteTaskEvent $event): int
    {
        $db     = $this->getDatabase();
        $tables = $db->getTableList();
        $prefix = $db->getPrefix();
        $delay  = (int) $event->getArgument('params')->delay ?? 1;
        $failed = false;

        foreach ($tables as $tn) {
            // Make sure we get the right tables based on prefix.
            if (stripos($tn, $prefix) !== 0) {
                continue;
            }

            $fields = $db->getTableColumns($tn, false);

            if (!(isset($fields['checked_out'], $fields['checked_out_time']))) {
                continue;
            }

            $query = $db->createQuery()
                ->update($db->quoteName($tn))
                ->set($db->quoteName('checked_out') . ' = DEFAULT')
                ->set($db->quoteName('checked_out_time') . ' = NULL');

            // The following expression evaluates to false for values <= 0 as well as for NULL values
            $query->where($db->quoteName('checked_out') . ' > 0');

            if ($delay > 0) {
                $delayTime = Factory::getDate('now', 'UTC')->sub(new \DateInterval('PT' . $delay . 'H'))->toSql();
                $query->where(
                    $db->quoteName('checked_out_time') . ' < ' . $db->quote($delayTime)
                );
            }

            $db->setQuery($query);

            try {
                $db->execute();
            } catch (ExecutionFailureException) {
                // This failure isn't critical, don't care too much
                $failed = true;
            }
        }

        return $failed ? TaskStatus::INVALID_EXIT : TaskStatus::OK;
    }
}

The same content is in 13 releases, Joomla 6.1.3-rc1 to 6.2.1-rc1. See them all, or every version of this file.