[ Index ] |
|
Code source de dotProject 2.1 RC1 |
1 <?php 2 // $Id: event_queue.class.php,v 1.7.2.2 2007/02/01 07:26:31 ajdonnison Exp $ 3 4 /** 5 * Event handling queue class. 6 * 7 * The event queue uses the table event_queue to manage 8 * event notifications and other timed events, as well as 9 * outgoing emails. 10 * 11 * Copyright 2005, the dotProject team. 12 */ 13 14 if (!defined('DP_BASE_DIR')){ 15 die('You should not access this file directly'); 16 } 17 18 class EventQueue { 19 20 var $table = 'event_queue'; 21 var $update_list = array(); 22 var $delete_list = array(); 23 var $event_count = 0; 24 25 function EventQueue() 26 { 27 } 28 29 /** 30 * Add an event to the queue. 31 * 32 * The callback can either be the name of a global function or the 33 * name of a class 34 * @param mixed $callback function to call when this event is due. 35 * @param mixed $args Arguments to pass to the callback 36 * @param string $module module, or originator of the event 37 * @param string $type type of event (to allow searching) 38 * @param integer $id id of originating event. 39 * @param integer $date Seconds since 1970 to trigger event. 40 * @param integer $repeat_interval seconds to repeat 41 * @param integer $repeat_count number of times to repeat 42 * @return integer queue id 43 */ 44 function add($callback, &$args, $module, $sysmodule = false, $id = 0, $type = '', $date = 0, $repeat_interval = 0, $repeat_count = 1) 45 { 46 global $AppUI; 47 48 if (! isset($AppUI)) 49 $user_id = 0; 50 else 51 $user_id = $AppUI->user_id; 52 53 if (is_array($callback)) { 54 list($class, $method) = $callback; 55 if (is_object($class)) 56 $class = get_class($class); 57 $caller = $class . '::' . $method; 58 } else { 59 $caller = $callback; 60 } 61 62 $q = new DBQuery; 63 $q->addTable($this->table); 64 $q->addInsert('queue_owner', $user_id); 65 $q->addInsert('queue_start', $date); 66 $q->addInsert('queue_callback', $caller); 67 $q->addInsert('queue_data', serialize($args)); 68 $q->addInsert('queue_repeat_interval', $repeat_interval); 69 $q->addInsert('queue_repeat_count', $repeat_count); 70 $q->addInsert('queue_module', $module); 71 $q->addInsert('queue_type', $type); 72 $q->addInsert('queue_origin_id', $id); 73 if ($sysmodule) 74 $q->addInsert('queue_module_type', 'system'); 75 else 76 $q->addInsert('queue_module_type', 'module'); 77 if ($q->exec()) 78 $return = db_insert_id(); 79 else 80 $return = false; 81 $q->clear(); 82 return $return; 83 } 84 85 /** 86 * Remove the event from the queue. 87 * 88 */ 89 function remove($id) 90 { 91 $q = new DBQuery; 92 $q->setDelete($this->table); 93 $q->addWhere("queue_id = '$id'"); 94 $q->exec(); 95 $q->clear(); 96 } 97 98 /** 99 * Find a queue record (or records) based upon the 100 * 101 */ 102 function find($module, $type, $id = null) 103 { 104 $q = new DBQuery; 105 $q->addTable($this->table); 106 $q->addWhere("queue_module = '$module'"); 107 $q->addWhere("queue_type = '$type'"); 108 if (isset($id)) 109 $q->addWhere("queue_origin_id = '$id'"); 110 return $q->loadHashList('queue_id'); 111 } 112 113 /** 114 * Execute a queue entry. This involves resolving the 115 * method to execute and passing the arguments to it. 116 */ 117 function execute(&$fields) 118 { 119 global $AppUI; 120 121 if (isset($fields['queue_module_type']) 122 && $fields['queue_module_type'] == 'system') 123 include_once $AppUI->getSystemClass($fields['queue_module']); 124 else 125 include_once $AppUI->getModuleClass($fields['queue_module']); 126 127 $args = unserialize($fields['queue_data']); 128 if (strpos($fields['queue_callback'], '::') !== false) { 129 list($class, $method) = explode('::', $fields['queue_callback']); 130 if (!class_exists($class)) { 131 dprint(__FILE__, __LINE__, 2, "Cannot process event: Class $class does not exist"); 132 return false; 133 } 134 $object = new $class; 135 if (!method_exists($object, $method)) { 136 dprint(__FILE__, __LINE__, 2, "Cannot process event: Method $class::$method does not exist"); 137 return false; 138 } 139 return $object->$method($fields['queue_module'], $fields['queue_type'], $fields['queue_origin_id'], $fields['queue_owner'], $args); 140 } else { 141 $method = $fields['queue_callback']; 142 if (!function_exists($method)) { 143 dprint(__FILE__, __LINE__, 2, "Cannot process event: Function $method does not exist"); 144 return false; 145 } 146 return $method($fields['queue_module'], $fields['queue_type'], $fields['queue_origin_id'], $fields['queue_owner'], $args); 147 } 148 } 149 150 /** 151 * Scans the queue for entries that are older than current date. 152 * If it finds one it tries to execute the attached function. 153 * If successful, the entry is removed from the queue, or if 154 * it is a repeatable event the repeat time is added to the 155 * start time and the repeat count (if set) is decremented. 156 */ 157 function scan() 158 { 159 $q = new DBQuery; 160 $q->addTable($this->table); 161 $now = time(); 162 $q->addWhere('queue_start < ' . $now); 163 $rid = $q->exec(); 164 165 $this->event_count = 0; 166 for ($rid; ! $rid->EOF; $rid->moveNext()) { 167 if ($this->execute($rid->fields)) { 168 $this->update_event($rid->fields); 169 $this->event_count++; 170 } 171 } 172 $q->clear(); 173 174 $this->commit_updates(); 175 } 176 177 function update_event(&$fields) 178 { 179 if ($fields['queue_repeat_interval'] > 0 && $fields['queue_repeat_count'] > 0) { 180 $fields['queue_start'] += $fields['queue_repeat_interval']; 181 $fields['queue_repeat_count']--; 182 $this->update_list[] = $fields; 183 } else { 184 $this->delete_list[] = $fields['queue_id']; 185 } 186 } 187 188 function commit_updates() 189 { 190 $q = new DBQuery; 191 if (count($this->delete_list)) { 192 $q->setDelete($this->table); 193 $q->addWhere("queue_id in (" . implode(',', $this->delete_list) . ")"); 194 $q->exec(); 195 $q->clear(); 196 } 197 $this->delete_list = array(); 198 199 foreach ($this->update_list as $fields) { 200 $q->addTable($this->table); 201 $q->addUpdate('queue_repeat_count', $fields['queue_repeat_count']); 202 $q->addUpdate('queue_start', $fields['queue_start']); 203 $q->addWhere('queue_id = ' . $fields['queue_id']); 204 $q->exec(); 205 $q->clear(); 206 } 207 $this->update_list = array(); 208 } 209 210 } 211 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 18 19:46:52 2007 | par Balluche grâce à PHPXref 0.7 |