Newer
Older
<?php
/**
* This File contains QUI\Cron\Manager
*/
namespace QUI\Cron;
/**
* Cron Manager
*
* @author www.pcsg.de (Henning Leutz)
*/
class Manager
{
* @param unknown $cron - Name of the Cron
* @param unknown $min - On which minute should it start
* @param unknown $hour - On which hour should it start
* @param unknown $day - On which day should it start
* @param unknown $month - On which month should it start
*/
public function add($cron, $min, $hour, $day, $month)
{
if ( !$this->_cronExists( $cron ) ) {
throw new \QUI\Exception( 'Cannot add Cron. Cron not exists', 404 );
}
$cronData = $this->getCronData( $cron );
\QUI::getDataBase()->insert($this->Table(), array(
'exec' => $cronData['exec'],
'title' => $cronData['title'],
'min' => $min,
'hour' => $hour,
'day' => $day,
'month' => $month
\QUI::getMessagesHandler()->addSuccess(
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
'Cron erfolgreich hinzugefügt'
);
}
/**
* Return the Crons which are available and from other Plugins provided
*
* @return Array
*/
public function getAvailableCrons()
{
$PackageManager = \QUI::getPackageManager();
$packageList = $PackageManager->getInstalled();
$result = array();
foreach ( $packageList as $entry )
{
$dir = OPT_DIR . $entry['name'] .'/';
$cronFile = $dir . 'cron.xml';
if ( !file_exists( $cronFile ) ) {
continue;
}
$result = array_merge(
$result,
$this->getCronsFromFile( $cronFile )
);
}
return $result;
}
/**
* Return the data of a specific cron
*/
public function getCronData($cron)
{
$availableCrons = $this->getAvailableCrons();
// check if cron is available
foreach ( $availableCrons as $entry )
{
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
return $entry;
}
}
return false;
}
/**
* Return the cron list
*
* @return Array
*/
public function getList()
{
return \QUI::getDataBase()->fetch(array(
'from' => self::Table()
));
}
/**
* Exist the cron?
*
* @return Bool
*/
protected function _cronExists($cron)
{
return $this->getCronData( $cron ) === false ? false : true;
}
/**
* static
*/
/**
* Return the cron tabe
*
* @return String
*/
static function Table()
{
return QUI_DB_PRFX .'cron';
}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Return the Crons from a XML File
*
* @param String $file
* @return Array
*/
static function getCronsFromFile($file)
{
if ( !file_exists( $file ) ) {
return array();
}
$Dom = \QUI\Utils\XML::getDomFromXml( $file );
$crons = $Dom->getElementsByTagName( 'crons' );
if ( !$crons || !$crons->length ) {
return array();
}
$Crons = $crons->item( 0 );
$list = $Crons->getElementsByTagName( 'cron' );
if ( !$list || !$list->length ) {
return array();
}
$result = array();
for ( $i = 0; $i < $list->length; $i++ )
{
$Cron = $list->item( $i );
$title = '';
$desc = '';
$Title = $Cron->getElementsByTagName( 'title' );
$Desc = $Cron->getElementsByTagName( 'description' );
if ( $Title->length ) {
$title = trim( $Title->item( 0 )->nodeValue );
}
if ( $Desc->length ) {
$desc = trim( $Desc->item( 0 )->nodeValue );
}
$result[] = array(
'title' => $title,
'description' => $desc,
'exec' => $Cron->getAttribute( 'exec' )
);
}
return $result;
}
/**
* Print a message to the log cron.log
*
* @param String $message - Message
*/
static function log($message)
{
$User = \QUI::getUsers()->getUserBySession();
$dir = VAR_DIR . 'log/';
$file = $dir . 'cron_'. date('Y-m-d') .'.log';
$str = '['. date('Y-m-d H:i:s') .' :: '. $User->getName() .'] '. $message;
QUI\System\Log::write( $str, 'cron' );
}