Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset.inc';
18 
34 class Trigger extends Asset
35 {
36 
37 
44  var $tmid = 0;
45 
46 
56  function Trigger($tmid=0, $id=0)
57  {
58  $this->_ser_attrs = TRUE;
59  $this->_loadVars();
60 
61  // attempt to load trigger from database
62  if (!empty($tmid) && !empty($id)) {
63  $this->load($tmid, $id);
64  }
65 
66  }//end constructor
67 
68 
77  function setTriggerManager($tmid)
78  {
79  if (empty($tmid)) return FALSE;
80 
81  $tm = $GLOBALS['SQ_SYSTEM']->am->getAsset($tmid, 'trigger_manager');
82  if (!is_null($tm)) {
83  // this is our new trigger manager
84  $this->tmid = $tmid;
85  return TRUE;
86  }
87  return FALSE;
88 
89  }//end setTriggerManager()
90 
91 
98  function &getTriggerManager()
99  {
100  $asset = NULL;
101  if (isset($this->tmid) && !empty($this->tmid)) {
102  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->tmid, 'trigger_manager');
103  }
104  return $asset;
105 
106  }//end getTriggerManager()
107 
108 
118  function load($tmid, $id)
119  {
120  // is our trigger manager the real thing?
121  if (!$this->setTriggerManager($tmid)) return FALSE;
122  $tm = $this->getTriggerManager();
123 
124  // we use the trigger manager function to load the trigger
125  $trigger_info = $tm->_loadTrigger($id);
126  if (empty($trigger_info)) return FALSE;
127 
128  // trigger exists, so the id is valid
129  $this->id = implode(':', Array($tmid, $id));
130 
131  $this->name = $trigger_info['name'];
132  $this->short_name = $trigger_info['name'];
133 
134  // mimic the bridge status
135  $this->status = $tm->status;
136 
137  // load attributes
138  $this->vars['name']['value'] = $trigger_info['name'];
139  $this->vars['description']['value'] = $trigger_info['description'];
140  $this->vars['events']['value'] = $trigger_info['data']['events'];
141  $this->vars['conditions']['value'] = $trigger_info['data']['conditions'];
142  $this->vars['actions']['value'] = $trigger_info['data']['actions'];
143  $this->vars['active']['value'] = $trigger_info['active'];
144  $this->vars['category']['value'] = $trigger_info['category'];
145  $this->vars['blocking']['value'] = empty($trigger_info['data']['settings']['blocking']) ? 0 : 1;
146 
147  return TRUE;
148 
149  }//end load()
150 
151 
175  function create(&$link)
176  {
177  $save_result = $this->saveAttributes();
178  if (!$save_result) return $save_result;
179 
180  // possibly not the best way to do this, but we need
181  // to set our status / name and the trigger manager id
182  $this_id = explode(':', $this->id);
183  $id = $this_id[1];
184  $load_result = $this->load($this->tmid, $id);
185 
186  $em = $GLOBALS['SQ_SYSTEM']->getEventManager();
187  $tm = $this->getTriggerManager();
188 
189  // hopefully the asset map is listening
190  $em->broadcastEvent($tm, 'CreateLink', Array('linkid' => $this->id));
191 
192  return $save_result && $load_result;
193 
194  }//end create()
195 
196 
207  function saveAttributes($dont_run_updated=FALSE)
208  {
209  $trigger_info = Array();
210 
211  if (empty($this->tmid)) return FALSE;
212  $id_parts = explode(':', $this->id);
213 
214  // if we supply an id to the trigger manger, existing
215  // trigger will be updated
216  $trigger_id = array_get_index($id_parts, 1);
217  if (!is_null($trigger_id)) {
218  $trigger_info['id'] = $trigger_id;
219  }
220 
221  // build trigger info from attributes
222  $trigger_info['name'] = $this->attr('name');
223  $trigger_info['description'] = $this->attr('description');
224  $trigger_info['data']['events'] = $this->attr('events');
225  $trigger_info['data']['conditions'] = $this->attr('conditions');
226  $trigger_info['data']['actions'] = $this->attr('actions');
227  $trigger_info['active'] = $this->attr('active');
228  $trigger_info['category'] = $this->attr('category');
229 
230  // Name cannot be empty
231  if (empty($trigger_info['name'])) {
232  trigger_localised_error('TRIG0001', E_USER_NOTICE);
233  return FALSE;
234  }
235 
236  // special condition for blocking
237  if ($this->attr('blocking')) {
238  $trigger_info['data']['settings']['blocking'] = TRUE;
239  }
240 
241  // do not perform unnecessary hashing
242  if (empty($trigger_info['data']['events']) || empty($trigger_info['data']['actions']) || empty($trigger_info['active'])) {
243  $trigger_info['save_hash'] = FALSE;
244  }
245 
246  $tm = $this->getTriggerManager();
247  if (is_null($tm)) return FALSE;
248 
249  // use trigger manager to save
250  if ($id = $tm->_saveTrigger($trigger_info)) {
251  $this->id = implode(':', Array($this->tmid, $id));
252 
253  $em = $GLOBALS['SQ_SYSTEM']->getEventManager();
254  $em->broadcastEvent($this, 'AssetUpdate', Array('short_name'));
255 
256  return TRUE;
257  }
258  return FALSE;
259 
260  }//end saveAttributes()
261 
262 
263 }//end class
264 
265 ?>