forked from Kicktemp/plg_task_kickmanagearticle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkickmanagearticle.php
127 lines (109 loc) · 3.46 KB
/
kickmanagearticle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* @package Kick Manage Article
*
* @author Kicktemp GmbH <[email protected]>
* @copyright Copyright © 2022 Kicktemp GmbH. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* @link https://kicktemp.com
*/
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Content\Site\Helper\QueryHelper;
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\DatabaseDriver;
use Joomla\Database\ParameterType;
use Joomla\Event\SubscriberInterface;
use Joomla\CMS\Log\Log;
class plgTaskKickManageArticle extends CMSPlugin implements SubscriberInterface
{
use TaskPluginTrait;
/**
* @var string[]
*
* @since 1.0.0
*/
protected const TASKS_MAP = [
'kickmanagearticle.move' => [
'langConstPrefix' => 'PLG_TASK_KICKMANAGEARTICLE_MOVE',
'form' => 'move',
'method' => 'moveArticle',
],
];
/**
* Load the language file on instantiation.
*
* @var boolean
*
* @since 4.0.0
*/
protected $autoloadLanguage = true;
/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since 4.0.0
*/
public static function getSubscribedEvents(): array
{
return [
'onTaskOptionsList' => 'advertiseRoutines',
'onExecuteTask' => 'standardRoutineHandler',
'onContentPrepareForm' => 'enhanceTaskItemForm',
];
}
/**
* @param ExecuteTaskEvent $event The onExecuteTask event
*
* @return integer The exit code
*
* @throws RuntimeException
* @throws LogicException
* @since 4.1.0
*/
protected function moveArticle(ExecuteTaskEvent $event): int
{
$params = $event->getArgument('params');
$toCatid = (int) $params->toCatid;
$cf = (int) $params->customField;
$format = $params->dateFormat;
/** @var DatabaseDriver $db */
$db = Factory::getContainer()->get('DatabaseDriver');
$query = $db->getQuery(true);
$now = Factory::getDate('now', 'GMT');
$query->select($db->quoteName('a.id'))
->from($db->quoteName('#__content', 'a'))
->join('INNER', $db->quoteName('#__fields_values', 'fv') . ' ON (' . $db->quoteName('a.id') . ' = ' . $db->quoteName('fv.item_id') . ')')
->where($db->quoteName('a.catid') . ' = :categoryId')
->where($db->quoteName('fv.field_id') . ' = ' . $cf ) // the condition custom field
->where('STR_TO_DATE(' . $db->quoteName('fv.value') . ', \'' . $format . '\') < :now') // Convert the date format
->bind(':categoryId', $params->fromCatid)
->bind(':now', $now->toSql());
$db->setQuery($query);
$pks = $db->loadColumn();
//$toLog = json_encode($pks);
//Log::add($toLog, Log::INFO, 'task');
if (count($pks) && $toCatid)
{
// Remove zero values resulting from input filter
$pks = array_filter($pks);
// SQL UPDATE query to move the articles
$updateQuery = $db->getQuery(true);
$updateQuery->update($db->quoteName('#__content'))
->set($db->quoteName('catid') . ' = ' . $toCatid)
->where($db->quoteName('id') . ' IN (' . implode(',', $pks) . ')');
try {
$db->setQuery($updateQuery);
$db->execute();
} catch (\RuntimeException $e) {
$this->logTask($e->getMessage(), 'error');
return TaskStatus::NO_RUN;
}
}
return TaskStatus::OK;
}
}