Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_create_image_variety.inc
1 <?php
17 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
18 
32 {
33 
34 
55  public static function execute($settings, &$state)
56  {
57  $am =& $GLOBALS['SQ_SYSTEM']->am;
58 
59  if (empty($state['asset'])) {
60  // grab the asset if assetid is given, but not the asset.
61  if (empty($state['assetid'])) {
62  return FALSE;
63  } else {
64  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
65  }
66  }
67 
68  // asset must be image
69  if (!($state['asset'] instanceof Image)) {
70  trigger_error('Unable to create image variety for a '.$state['asset']->type().' asset', E_USER_NOTICE);
71  return FALSE;
72  }
73 
74  $GLOBALS['SQ_SYSTEM']->am->includeAsset('image_variety');
75 
76  $variety = new Image_Variety();
77  $variety->setAttrValue('variety_type', 'resize');
78 
79  // set & check settings, state
80 
81  // name
82  if (!@$variety->setAttrValue('name', $settings['name'])) {
83  return FALSE;
84  }
85 
86  // constrain
87  if (!@$variety->setAttrValue('constrain', $settings['constrain'])) {
88  return FALSE;
89  }
90 
91  // width
92  if (!@$variety->setAttrValue('width', $settings['width'])) {
93  return FALSE;
94  }
95 
96  // height
97  if (!@$variety->setAttrValue('height', $settings['height'])) {
98  return FALSE;
99  }
100 
101  // rely on image_variety to check whether the variety already exists
102  $vid = $state['asset']->saveVariety($variety);
103 
104  return Array(
105  'imageid' => $state['asset']->id,
106  'varietyid' => $state['asset']->id.':'.$vid,
107  'name' => $variety->name,
108  'width' => $variety->attr('width'),
109  'height' => $variety->attr('height'),
110  );
111 
112  }//end execute()
113 
114 
125  public static function getInterface($settings, $prefix, $write_access=FALSE)
126  {
127  // set defaults
128  $settings['name'] = array_get_index($settings, 'name', '');
129  $settings['constrain'] = (string) array_get_index($settings, 'constrain', 'width');
130  $settings['width'] = array_get_index($settings, 'width', 0);
131  $settings['height'] = array_get_index($settings, 'height', 0);
132  $width_display_style = 'none';
133  $width_section_display_style = 'none';
134  $height_display_style = 'none';
135  $height_section_display_style = 'none';
136  $dimension_display_style = 'none';
137 
138  switch ($settings['constrain']) {
139  case 'width':
140  $width_display_style = 'inline';
141  $width_section_display_style = 'block';
142  $height_display_style = 'none';
143  $height_section_display_style = 'none';
144  $dimension_display_style = 'none';
145  break;
146 
147  case 'height':
148  $width_display_style = 'none';
149  $width_section_display_style = 'none';
150  $height_display_style = 'inline';
151  $height_section_display_style = 'block';
152  $dimension_display_style = 'none';
153  break;
154 
155  case 'dimension':
156  $width_display_style = 'none';
157  $width_section_display_style = 'none';
158  $height_display_style = 'none';
159  $height_section_display_style = 'block';
160  $dimension_display_style = 'inline';
161  break;
162 
163  case 'none':
164  $width_display_style = 'inline';
165  $width_section_display_style = 'block';
166  $height_display_style = 'inline';
167  $height_section_display_style = 'block';
168  $dimension_display_style = 'none';
169  break;
170  }//end switch
171 
172  // Name box
173  ob_start();
174  if ($write_access) {
175  text_box($prefix.'[name]', $settings['name']);
176  } else {
177  echo $settings['name'];
178  }//end if
179  $basic_part_1 = ob_get_contents();
180  ob_end_clean();
181 
182  // Painting the interface
183  ob_start();
184  echo translate('trigger_create_image_variety_basic', $basic_part_1);
185  echo '<br />';
186 
187  $js_prefix = preg_replace('/[^a-z0-9_]/', '_', $prefix);
188  ?>
189  <script type="text/javascript">
190  <!-- //
191 
192  function <?php echo $js_prefix; ?>_update_visible_fields() {
193 
194  var constrain = get_form_element_value("<?php echo $prefix; ?>[constrain]");
195  if (constrain == 'width') {
196  document.getElementById("<?php echo $prefix; ?>_width_section").style.display = "block";
197  document.getElementById("<?php echo $prefix; ?>_width_label").style.display = "inline";
198  document.getElementById("<?php echo $prefix; ?>_height_section").style.display = "none";
199  document.getElementById("<?php echo $prefix; ?>_height_label").style.display = "none";
200  document.getElementById("<?php echo $prefix; ?>_dimension_label").style.display = "none";
201  } else if (constrain == 'height') {
202  document.getElementById("<?php echo $prefix; ?>_width_section").style.display = "none";
203  document.getElementById("<?php echo $prefix; ?>_width_label").style.display = "none";
204  document.getElementById("<?php echo $prefix; ?>_height_section").style.display = "block";
205  document.getElementById("<?php echo $prefix; ?>_height_label").style.display = "inline";
206  document.getElementById("<?php echo $prefix; ?>_dimension_label").style.display = "none";
207  } else if (constrain == 'dimension') {
208  document.getElementById("<?php echo $prefix; ?>_width_section").style.display = "none";
209  document.getElementById("<?php echo $prefix; ?>_width_label").style.display = "none";
210  document.getElementById("<?php echo $prefix; ?>_height_section").style.display = "block";
211  document.getElementById("<?php echo $prefix; ?>_height_label").style.display = "none";
212  document.getElementById("<?php echo $prefix; ?>_dimension_label").style.display = "inline";
213  } else if (constrain == 'none') {
214  document.getElementById("<?php echo $prefix; ?>_width_section").style.display = "block";
215  document.getElementById("<?php echo $prefix; ?>_width_label").style.display = "inline";
216  document.getElementById("<?php echo $prefix; ?>_height_section").style.display = "block";
217  document.getElementById("<?php echo $prefix; ?>_height_label").style.display = "inline";
218  document.getElementById("<?php echo $prefix; ?>_dimension_label").style.display = "none";
219  }//end if
220  return true;
221 
222  }// end <?php echo $prefix; ?>_update_visible_fields()
223 
224  // -->
225  </script>
226  <?php
227  $javascript = 'onchange="return '.$js_prefix.'_update_visible_fields();"';
228  $options = Array(
229  'width' => translate('width'),
230  'height' => translate('height'),
231  'dimension' => translate('both'),
232  'none' => translate('neither'),
233  );
234  echo translate('constrain_by').' ';
235  if ($write_access) {
236  combo_box($prefix.'[constrain]', $options, FALSE, $settings['constrain'], 0, $javascript);
237  } else {
238  echo $options[$settings['constrain']];
239  }//end if
240  echo '<br />';
241 
242  echo '<div id="'.$prefix.'_width_section" style="display:'.$width_section_display_style.'">';
243  echo '<span id="'.$prefix.'_width_label" style="display:'.$width_display_style.'">';
244  echo translate('width');
245  echo '&nbsp; </span>';
246  if ($write_access) {
247  int_text_box($prefix.'[width]', $settings['width'], FALSE);
248  } else {
249  echo $settings['width'];
250  }//end if
251  echo '</div>';
252 
253  echo '<div id="'.$prefix.'_height_section" style="display:'.$height_section_display_style.'">';
254  echo '<span id="'.$prefix.'_height_label" style="display:'.$height_display_style.'">';
255  echo translate('height');
256  echo ' </span>';
257  echo '<span id="'.$prefix.'_dimension_label" style="display:'.$dimension_display_style.'">';
258  echo translate('dimension');
259  echo ' </span>';
260  if ($write_access) {
261  int_text_box($prefix.'[height]', $settings['height'], FALSE);
262  } else {
263  echo $settings['height'];
264  }//end if
265  echo '</div>';
266 
267  $html = ob_get_contents();
268  ob_end_clean();
269 
270  return $html;
271 
272  }//end getInterface()
273 
274 
286  public static function processInterface(&$settings, $request_data)
287  {
288  if (empty($request_data)) {
289  return translate('trigger_input_data_missing');
290  }
291 
292  // check if the name, constrain, width and height are set
293  if (empty($request_data['name'])) {
294  return translate('trigger_variety_name_cannot_be_empty');
295  }
296 
297  if (!isset($request_data['constrain']) || !isset($request_data['width']) || !isset($request_data['height'])) {
298  return translate('trigger_require_constrain_width_height');
299  }
300 
301  $GLOBALS['SQ_SYSTEM']->am->includeAsset('image_variety');
302 
303  $variety = new Image_Variety();
304  $variety->setAttrValue('variety_type', 'resize');
305 
306  // name
307  if (!$variety->setAttrValue('name', $request_data['name'])) {
308  return translate('trigger_cannot_set_variety_value', translate('name'));
309  }
310 
311  // constrain
312  if (!$variety->setAttrValue('constrain', $request_data['constrain'])) {
313  return translate('trigger_cannot_set_variety_value', translate('constrain'));
314  }
315 
316  // width
317  if (!$variety->setAttrValue('width', $request_data['width'])) {
318  return translate('trigger_cannot_set_variety_value', translate('width'));
319  }
320 
321  // height
322  if (!$variety->setAttrValue('height', $request_data['height'])) {
323  return translate('trigger_cannot_set_variety_value', translate('height'));
324  }
325 
326  unset($variety);
327 
328  // save settings
329  $settings['name'] = $request_data['name'];
330  $settings['constrain'] = $request_data['constrain'];
331  $settings['width'] = strval($request_data['width']);
332  $settings['height'] = strval($request_data['height']);
333 
334  return FALSE;
335 
336  }//end processInterface()
337 
338 
348  public static function getLocks($settings, &$state)
349  {
350  return Array($state['assetid'] => Array('all'));
351 
352  }//end getLocks()
353 
354 
355 }//end class
356 
357 ?>