Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design_area_menu_stalks.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/designs/design_areas/menu/design_area_menu_recursive/design_area_menu_recursive.inc';
19 
32 {
37  public $arrow_width = 4;
38 
43  public $arrow_height = 6;
44 
45 
55  public function setAttrValue($name, $value)
56  {
57  // make sure the aligment is lower case
58  switch ($name) {
59  case 'alignment' :
60  $value = strtolower($value);
61  break;
62 
63  case 'min_height' :
64  $value = (int) $value;
65  if ($value <= 0) {
66  trigger_localised_error('CORE0177', E_USER_WARNING, $name);
67  return FALSE;
68  }
69  break;
70  }//end switch
71 
72  return parent::setAttrValue($name, $value);
73 
74  }//end setAttrValue()
75 
76 
84  public function printRecursiveFn()
85  {
86  $use_blanks = !$this->attr('show_stalks');
87  $cell_class = '';
88  if ($this->settingExists('class')) {
89  ob_start();
90  $this->printVar('settings.class', Array());
91  $cell_class = ' class="'.ob_get_contents().'"';
92  ob_end_clean();
93  }//end if
94 
95 
96  ob_start();
97  echo '
98  <td'.$cell_class.' style="vertical-align: top;"><img alt="" src="';
99  if ($use_blanks) {
100  echo '<', '?php echo $blank_src; ?', '>';
101  } else {
102  echo '<', '?php echo (($'.$this->attr('id_name').'_current_setting == \'current\') ? $stalk_prefix.\'arrow.png\' : $blank_src); ?', '>';
103 
104  }
105  echo '" width="'.$this->arrow_width.'" height="'.$this->attr('min_height').'" /></td>';
106  $arrow_contents = ob_get_contents();
107  ob_end_clean();
108 
109 
110 
111  ob_start();
112  if ($asset_contents = $this->attr('asset_contents')) {
113  $this->_tmp['outputting_asset_section'] = TRUE;
114  foreach ($asset_contents as $item) {
115  $this->_printContentItemCode($item);
116  }
117  unset($this->_tmp['outputting_asset_section']);
118  }//end if
119 
120  $asset_contents = '
121  <td'.$cell_class.' style="width: 100%; vertical-align: top; text-align: '.$this->attr('alignment').';'.(($this->attr('word_wrap')) ? '' : ' white-space: nowrap;').'">'.ob_get_contents().'</td>
122  ';
123  ob_end_clean();
124 
125  // if we are using ellipsis, then we need to include general/general.inc
126  if ($this->attr('ellipsis_length') > 0) {
127  echo 'require_once SQ_FUDGE_PATH.\'/general/general.inc\';';
128  }
129 
130  $unwanted_types = $this->attr('unwanted_asset_types');
131  $all_type_codes = Array();
132  foreach ($unwanted_types as $type) {
133  if (!empty($type)) {
134  $all_type_codes = array_merge($all_type_codes, $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants($type, TRUE));
135  }
136  }
137  $unwanted_types = $all_type_codes;
138 
139  echo '
140 
141  function '.$this->attr('id_name').'_recursive_fn($level, $parentid, $'.$this->attr('id_name').'_base_url, $depth=Array())
142  {
143  global $RECURSIVE_MENU_ASSET_LINEAGE;
144  static $'.$this->attr('id_name').'_settings = '.var_export($this->_getSettingsArray(), TRUE).';
145 
146  $blank_src = sq_web_path(\'lib\').\'/web/images/blank.gif\';
147  $stalk_prefix = sq_web_path(\'data\').\'/'.$this->data_path_suffix.'/\';
148 
149  // The indenting level, stalk on/off
150  $depth[] = true;
151  $depth_count = count($depth);
152 
153  $'.$this->attr('id_name').'_lineage_assetid = (empty($RECURSIVE_MENU_ASSET_LINEAGE[$level + 1])) ? 0 : $RECURSIVE_MENU_ASSET_LINEAGE[$level + 1][\'assetid\'];
154  $'.$this->attr('id_name').'_lineage_setting = (count($RECURSIVE_MENU_ASSET_LINEAGE) - 1 == $level + 1) ? \'current\' : \'hierarchy\';
155  $'.$this->attr('id_name').'_urls = $GLOBALS[\'SQ_SYSTEM\']->am->getAssetURL(array_keys($GLOBALS[\'MENU_DATA\']['.$this->_safeStringLiteral($this->attr('id_name')).'][$parentid]), $'.$this->attr('id_name').'_base_url);
156 
157 # pre_echo("Lineage Setting : $'.$this->attr('id_name').'_lineage_setting");
158 # pre_echo("Lineage Assetid : $'.$this->attr('id_name').'_lineage_assetid");
159  $num_kids = count($GLOBALS[\'MENU_DATA\'][\''.$this->attr('id_name').'\'][$parentid]);
160  $i = 0;
161  foreach ($GLOBALS[\'MENU_DATA\']['.$this->_safeStringLiteral($this->attr('id_name')).'][$parentid] as $'.$this->attr('id_name').'_row) {
162  $i++;
163  $end = ($i == $num_kids);';
164 
165  if (!empty($unwanted_types)) {
166  $unwanted_types_literal_array = 'Array("'.implode('", "', $unwanted_types).'")';
167  echo '
168  if (in_array($'.$this->attr('id_name').'_row[\'type_code\'], '.$unwanted_types_literal_array.')) continue ;';
169  }//end if
170 
171  echo '
172  // if not live, we have to get the proper object, sorry
173  if (((int) $'.$this->attr('id_name').'_row[\'status\'] & SQ_STATUS_LIVE) == 0) {
174  $'.$this->attr('id_name').'_obj = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($'.$this->attr('id_name').'_row[\'assetid\'], $'.$this->attr('id_name').'_row[\'type_code\']);
175  } else {
176  unset($'.$this->attr('id_name').'_obj);
177  }
178  $'.$this->attr('id_name').'_current_setting = ($'.$this->attr('id_name').'_lineage_assetid == $'.$this->attr('id_name').'_row[\'assetid\']) ? $'.$this->attr('id_name').'_lineage_setting : \'normal\';
179 # pre_echo("CURRENT SETTING : ".$'.$this->attr('id_name').'_current_setting);
180  ?', '>
181  <div style="text-align: '.$this->attr('alignment').';">
182  <table cellpadding="0" cellspacing="0" border="0" width="100%">
183  <tr>
184  ';
185 
186  if ($this->attr('alignment') == 'right') {
187  echo $asset_contents;
188  }
189 
190  if ($this->attr('indent')) {
191 
192  if ($this->attr('alignment') == 'right') {
193  echo $arrow_contents;
194  }
195 
196  echo '
197  <', '?php
198  ';
199 
200  // depending on the alignment differs how we loop through the array
201  if ($this->attr('alignment') == 'left') {
202  echo '
203  for ($j = 0; $j < $depth_count; $j++) {
204  ';
205  } else {
206  echo '
207  for ($j = $depth_count - 1; $j >= 0; $j--) {
208  ';
209  }
210 
211  echo '?', '>
212  <td'.$cell_class;
213 
214  if ($use_blanks) {
215  echo ' style="vertical-align: top;"><img alt="" src="<', '?php echo $blank_src; ?', '>" width="'.$this->attr('indent').'" height="'.$this->attr('min_height').'" /></td>';
216  } else {
217  // if there is the need to a stalk
218  // and if we are at the end of a sub listing and
219  // not it the indent closest to the text (which will have the end branch)
220  echo '<', '?php
221  if ($depth[$j] && !($end && ($j == $depth_count - 1)))
222  echo \' style="vertical-align:top; background-image:url(\', $stalk_prefix, \'stalk.png); background-repeat: repeat-y;"\';
223 
224  ?', '>><img alt="" src="<', '?php
225  if ($j != $depth_count - 1) {
226  echo $blank_src.\'"\';
227  } elseif ($end) {
228  echo $stalk_prefix, \'endbranch.png"\';
229  } else {
230  echo $stalk_prefix, \'branch.png"\';
231  }
232 
233  ?', '> width="'.$this->attr('indent').'" height="'.$this->attr('min_height').'" /></td>';
234 
235  }//end if $use_blanks
236  echo '
237  <', '?php
238  }//end for depth count
239 
240  ?', '>';
241 
242  if ($this->attr('alignment') == 'left') {
243  echo $arrow_contents;
244  }
245 
246  }//end if
247 
248  if ($this->attr('alignment') == 'left') {
249  echo $asset_contents;
250  }
251 
252  echo '
253 
254  </tr>
255  </table>
256  </div>
257  <', '?php
258 
259  ';
260 
261  // if we are to show a minimum number of levels, add an extra bit to the condition
262 
263  $recurse_condition = '$'.$this->attr('id_name').'_current_setting != \'normal\'';
264 
265 
266  if ($this->attr('min_num_levels') > 0) {
267  $recurse_condition = '$level < '.($this->attr('min_num_levels') + $this->attr('level') - 1).' || '.$recurse_condition;
268  }
269 
270  if ($this->attr('max_num_levels') > 0) {
271  $recurse_condition = '$level < '.($this->attr('max_num_levels') + $this->attr('level') - 1).' && ('.$recurse_condition.')';
272  }
273  echo '
274  if (('.$recurse_condition.') && !empty($GLOBALS[\'MENU_DATA\']['.$this->_safeStringLiteral($this->attr('id_name')).'][$'.$this->attr('id_name').'_row[\'assetid\']])) {
275  if ($end) $depth[$depth_count - 1] = false;
276  '.$this->attr('id_name').'_recursive_fn($level + 1, $'.$this->attr('id_name').'_row[\'assetid\'], $'.$this->attr('id_name').'_urls[$'.$this->attr('id_name').'_row[\'assetid\']], $depth);
277  } // end if
278 
279  }//end for
280 
281  }//end '.$this->attr('id_name').'_recursive_fn()
282 
283  ';
284 
285  }//end printRecursiveFn()
286 
287 
294  protected function _printInitCode()
295  {
296  if ($this->attr('show_stalks') && $this->attr('indent') && $this->attr('min_height')) {
297  if (!$this->_createStalkImages($this)) return FALSE;
298  }
299  return parent::_printInitCode();
300 
301  }//end _printInitCode()
302 
303 
310  protected function _createStalkImages()
311  {
312 
313  $bg_colour = $this->attr('stalk_bg_colour');
314  $fg_colour = $this->attr('stalk_fg_colour');
315 
316  $w = $this->attr('indent');
317  $h = $this->attr('min_height');
318 
319  if (!$w || !$h) {
320  return FALSE;
321  }
322 
323 
324  $direction = ($this->attr('alignment') == 'left') ? 'right' : 'left';
325 
326  $image_path = $this->data_path_public;
327 
328  //------------------------------------//
329 
330  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
331  require_once SQ_FUDGE_PATH.'/colour/colour.inc';
332 
333  if (!create_directory($image_path)) return FALSE;
334 
335  $transparent = FALSE;
336  if (trim($bg_colour) == '') {
337  // get a nice contrasting shade so we don't pick a colour to close to the FG
338  // to use for transperancy
339  $bg_colour = Colour::contrastingShade($fg_colour);
340  $transparent = TRUE;
341  }
342 
343  $bg_rgb = Colour::htmlToRgb($bg_colour, FALSE);
344  $fg_rgb = Colour::htmlToRgb($fg_colour, FALSE);
345 
346  foreach (Array('stalk', 'arrow', 'branch', 'endbranch') as $image_name) {
347 
348  // create the image
349  $img = ImageCreate(($image_name == 'arrow') ? $this->arrow_width : $w, $h);
350  $bg = ImageColorAllocate($img, $bg_rgb['r'], $bg_rgb['g'], $bg_rgb['b']);
351  $fg = ImageColorAllocate($img, $fg_rgb['r'], $fg_rgb['g'], $fg_rgb['b']);
352 
353  if ($transparent) ImageColorTransparent($img, $bg);
354 
355  if ($image_name == 'branch' || $image_name == 'endbranch' ) {
356  if ($direction == 'left') {
357  $x1 = 0;
358  $x2 = floor($w / 2);
359  } else {
360  $x1 = floor($w / 2);
361  $x2 = $w - 1;
362  }
363  ImageFilledRectangle($img, $x1, floor($h / 2), $x2, floor($h / 2), $fg);
364  }//end if
365 
366  if ($image_name == 'branch' || $image_name == 'stalk' ) {
367  ImageFilledRectangle($img, floor($w / 2), 0, floor($w / 2), $h - 1, $fg);
368  }//end if
369 
370  if ($image_name == 'endbranch') {
371  ImageFilledRectangle($img, floor($w / 2), 0, floor($w / 2), floor($h / 2), $fg);
372  }//end if
373 
374  if ($image_name == 'arrow') {
375  if ($direction == 'left') {
376  $x1 = $this->arrow_width - 1;
377  $multiplier = -1;
378  } else {
379  $x1 = 0;
380  $multiplier = 1;
381  }
382  $y1 = floor($h / 2) - ($this->arrow_height / 2);
383 
384  $points = Array(
385  $x1,
386  $y1,
387  $x1 + ($multiplier * ($this->arrow_width - 1)),
388  $y1 + ($this->arrow_height / 2),
389  $x1,
390  $y1 + $this->arrow_height,
391  );
392  ImageFilledPolygon($img, $points, 3, $fg);
393  }//end if
394 
395  ImagePng($img, $image_path.'/'.$image_name.'.png');
396  ImageDestroy($img);
397 
398  }//end foreach
399 
400  return TRUE;
401 
402  }//end _createStalkImages()
403 
404 
405 }//end class
406 ?>