Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hash.inc
1 <?php
28 class Hash
29 {
30 
34  var $hash;
35 
40  var $tree_propagation;
41 
61  function Hash()
62  {
63  $this->hash = Array();
64  $this->tree_propagation = Array();
65 
66  }//end constructor
67 
68 
75  function isEmpty()
76  {
77  return empty($this->hash);
78 
79  }//end isEmpty()
80 
81 
91  function _set($name, $value)
92  {
93  if (!isset($this->hash[$name])) {
94  $this->hash[$name] = Array();
95  }
96 
97  if (!is_array($value)) $value = Array($value);
98 
99  foreach ($value as $one_value) {
100  if (!in_array($one_value, $this->hash[$name])) {
101  $this->hash[$name][] = $one_value;
102  }
103  }
104 
105  }//end _set()
106 
107 
116  function setEvent($event)
117  {
118  $this->_set('event',$event);
119 
120  }//end setEvent()
121 
122 
131  function setAssetId($assetid)
132  {
133  $this->_set('assetid',$assetid);
134 
135  }//end setAssetId()
136 
137 
147  function setAssetType($asset_type, $descendants=FALSE)
148  {
149  if (!empty($asset_type) && $GLOBALS['SQ_SYSTEM']->am->installed($asset_type)) {
150 
151  if ($descendants) {
152 
153  // Grab the list of types, put into the 'asset_type' field
154  $type_descendants = $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants($asset_type, TRUE);
155  if (!empty($type_descendants)) {
156  $this->_set('asset_type', $type_descendants);
157  }
158 
159  } else {
160  $this->_set('asset_type', $asset_type);
161  }
162 
163  }
164 
165  }//end setAssetType()
166 
167 
177  function setLinkTree($tree_id, $propagation)
178  {
179  $this->_set('treeid',$tree_id);
180 
181  if (!is_array($tree_id)) $tree_id = Array($tree_id);
182  foreach ($tree_id as $id){
183  $this->tree_propagation[$id] = $propagation;
184  }
185 
186  }//end setLinkTree()
187 
188 
197  function setTriggerId($trigger_id)
198  {
199  $this->_set('triggerid',$trigger_id);
200 
201  }//end setTriggerId()
202 
203 
213  function setLinkedId($linked_id, $is_major)
214  {
215  if (!$is_major) {
216  $this->_set('parentid',$linked_id);
217  } else {
218  $this->_set('childid',$linked_id);
219  }
220 
221  }//end setLinkedId()
222 
223 
233  function setLinkedType($linked_type, $is_major)
234  {
235  if (!$is_major) {
236  $this->_set('parent_type',$linked_type);
237  } else {
238  $this->_set('child_type',$linked_type);
239  }
240 
241  }//end setLinkedType()
242 
243 
252  {
253  $result = $this->hash;
254 
255  // make sure every field has data, and that no fields are null
256  foreach ($result as $id => $data) {
257  if (empty($result[$id])) $result[$id] = '-1';
258  }
259 
260 
261  $indicies = Array(
262  'event',
263  'assetid',
264  'asset_type',
265  'treeid',
266  'tree_propagate',
267  'triggerid',
268  'parentid',
269  'parent_type',
270  'childid',
271  'child_type',
272  );
273  foreach ($indicies as $index) {
274  if (!isset($result[$index])) $result[$index] = '-1';
275  }
276 
277  $combinations = $this->_createCombinations($result);
278 
279  // Bug #5612, we should not create combinations of the 'tree_propagate' index, so manually populate.
280  if ($result['treeid'] != '-1'){
281  foreach ($combinations as &$data){
282  $data['tree_propagate'] = $this->tree_propagation[$data['treeid']];
283  }
284  }
285 
286  return $combinations;
287 
288  }//end getHashCombinations()
289 
290 
309  function _createCombinations($source)
310  {
311  $result = Array();
312 
313  // step through each item in the source array
314  // and make combinations of it with the other items,
315  // preserving the position (index)
316  // so the item with index 'a' will always be at index 'a'
317  // if given an array of type Array(1=>'a',2=>'b',3=>'c'), the result will be the same array
318  // if given Array(1=>Array('a','b'), 2=>Array('c')), the result is Array(Array(1=>'a',2=>'c'),Array(1=>'b',2=>'c'))
319  // in short, this function creates a flat representation of the deep array, which can
320  // easily be converted back to the deep version if needed, i.e. no information is lost
321 
322  // let's roll this one
323  foreach ($source as $source_key => $source_item) {
324  reset($result);
325  $new_result = Array();
326 
327  if (!is_array($source_item)) {
328  $source_item = Array($source_item);
329  }
330 
331  if (empty($result)) {
332  foreach ($source_item as $one_item) {
333  $new_result[] = Array($source_key => $one_item);
334  }
335  } else {
336  foreach ($result as $res) {
337  foreach ($source_item as $one_item) {
338  $new_res = $res;
339  $new_res[$source_key] = $one_item;
340  $new_result[] = $new_res;
341  }
342  }
343  }
344 
345  $result = $new_result;
346  }
347 
348  return $result;
349 
350  }//end _createCombinations()
351 
352 
353 }//end class
354 
355 
356 ?>