Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_delete_image_variety.inc
1 <?php
17 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
18 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
19 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
20 require_once SQ_SYSTEM_ROOT.'/core/attributes/parameter_map/parameter_map.inc';
21 
36 {
37 
38 
50  public static function execute($settings, &$state)
51  {
52  // check if we have a image asset to look under
53  if ($settings['image_assetid'] == '0' && !$settings['use_current']) return FALSE;
54 
55  if (empty($state['asset'])) {
56  // grab the asset if assetid is given, but not the asset.
57  if (empty($state['assetid'])) {
58  return FALSE;
59  } else {
60  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
61  }
62  }
63 
64  if (is_null($state['asset'])) return FALSE;
65 
66  $old_current_asset = $GLOBALS['SQ_SYSTEM']->getGlobalDefine('CURRENT_ASSET', NULL);
67  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $state['asset']);
68 
69  $parameter_map_value = array_get_index($settings, 'value_map', serialize(Array()));
70  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
71  $param_map_value = $atr_parameter_map->getParameterValue('image_var');
72 
73  if (is_null($old_current_asset)) {
74  $GLOBALS['SQ_SYSTEM']->unsetGlobalDefine('CURRENT_ASSET');
75  } else {
76  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $old_current_asset);
77  }
78 
79  // check if we have one of the variable set to point us tot he variety to delete
80  if (empty($param_map_value) && empty($settings['variety_name']) && !$settings['delete_all']) return FALSE;
81 
82  // if we are asked to use current asset then over write the asset selected via asset picker
83  if ($settings['use_current']) {
84  $settings['image_assetid'] = $state['asset']->id;
85  $image_to_use = $state['asset'];
86  } else {
87  //get the asset
88  $image_to_use = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['image_assetid']);
89  }
90 
91  // make sure the asset exists
92  if (!$GLOBALS['SQ_SYSTEM']->am->assetExists($image_to_use->id)) return FALSE;
93 
94  // get all the varieties from the image
95  $varieties = $image_to_use->attr('varieties');
96 
97  // dont have any variety to delete?
98  if (empty($varieties)) return FALSE;
99 
100  // if deleting all thats it, over writes everythng else
101  $variety_deleted = Array();
102  if ($settings['delete_all']) {
103  foreach ($varieties['data'] as $varid => $data) {
104  if (!$image_to_use->deleteVariety($image_to_use->id.':'.$varid)) return FALSE;
105  $variety_deleted[] = $image_to_use->id.':'.$varid;
106  }// end if
107  } else {
108  // if we have a name configured to delete but also have a value from parameter
109  // map, we go with parameter map value
110  if (!empty($param_map_value)) $settings['variety_name'] = $param_map_value;
111 
112  $match_found = FALSE;
113  foreach ($varieties['data'] as $varid => $data) {
114  if ($data['name'] == $settings['variety_name']) {
115  $match_found = TRUE;
116  if (!$image_to_use->deleteVariety($image_to_use->id.':'.$varid)) return FALSE;
117  $variety_deleted[] = $image_to_use->id.':'.$varid;
118  }// end if
119  }// end foreach
120  }// end if/else
121 
122  $return_array = Array('majorid' => $image_to_use->id);
123  foreach ($variety_deleted as $var) {
124  $return_array['variety_deleted'][] = $var;
125  }
126 
127  return $return_array;
128 
129 
130 
131  }//end execute()
132 
133 
144  public static function getInterface($settings, $prefix, $write_access=FALSE)
145  {
146  // check settings, set defaults if necessary
147  $selected_image = array_get_index($settings, 'image_assetid', 0);
148  $variety_name = array_get_index($settings, 'variety_name', '');
149  $delete_all = array_get_index($settings, 'delete_all', FALSE);
150  $use_current = array_get_index($settings, 'use_current', FALSE);
151 
152  ob_start();
153  ?>
154  <table border="0">
155  <tr>
156  <td valign="top"><?php echo translate('trigger_action_delete_image_variety_image_under') ?></td>
157  <td valign="top">
158  <?php
159  if ($write_access) {
160  asset_finder($prefix.'[image_assetid]', $selected_image, Array('image' => 'I'));
161  } else {
162  if ($selected_image != 0) {
163  echo get_asset_tag_line($selected_image);
164  } else {
165  echo translate('trigger_action_delete_image_variety_no_image');
166  }
167  }
168  ?>
169  </td>
170  </tr>
171  <tr>
172  <td valign="top"><?php echo"<b><i>OR</i></b>"; ?></td>
173  <td></td>
174  </tr>
175  <tr>
176  <td valign="top"><?php echo translate('trigger_action_delete_image_variety_use_self') ?></td>
177  <td valign="top">
178  <?php
179  if ($write_access) {
180  check_box($prefix.'[use_current]', '1', $use_current, '', ($write_access ? '' : 'disabled="disabled"'));
181  } else {
182  echo '<img src="'.sq_web_path('lib').'/web/images/'.($use_current ? 'tick' : 'cross').'.gif" alt="'.($use_current ? translate('yes') : translate('no')).'" /> ';
183  }
184  echo translate('trigger_action_delete_image_variety_note_use_self');
185  ?>
186  </tr>
187  <tr>
188  <td align="Left" valign="top" colspan="2"><br /><b><?php echo translate('trigger_action_delete_image_variety_details'); ?></b><br /></td>
189  </tr>
190 
191  <tr>
192  <td valign="top"><?php echo translate('trigger_action_delete_image_variety_var_name'); ?></td>
193  <td valign="top">
194  <?php
195  if ($write_access) {
196  text_box($prefix.'[variety_name]', $variety_name, 25);
197  } else {
198  echo $variety_name;
199  }
200  ?>
201  </td>
202  </tr>
203  <tr>
204  <td valign="top"><?php echo translate('trigger_action_delete_image_variety_del_all'); ?></td>
205  <td valign="top">
206  <?php
207  if ($write_access) {
208  check_box($prefix.'[delete_all]', '1', $delete_all, '', ($write_access ? '' : 'disabled="disabled"'));
209  } else {
210  echo '<img src="'.sq_web_path('lib').'/web/images/'.($delete_all ? 'tick' : 'cross').'.gif" alt="'.($delete_all ? translate('yes') : translate('no')).'" /> ';
211  }
212  echo translate('trigger_action_delete_image_variety_del_all_note');
213  ?>
214  </td>
215  </tr>
216  </table>
217 
218  <?php
219  $new_prefix = str_replace(array('[',']'), '_', $prefix);
220  hidden_field($prefix.'[new_prefix]', $new_prefix);
221 
222  // print the parameter map
223  $parameter_map_value = array_get_index($settings, 'value_map', serialize(Array()));
224 
225  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
226 
227  $atr_parameter_map->setParameter('image_var', 'Image Variety');
228  echo $atr_parameter_map->paint($new_prefix.'_parameter_map', !$write_access);
229 
230 
231  $contents = ob_get_clean();
232  return $contents;
233 
234  }//end getInterface()
235 
236 
248  public static function processInterface(&$settings, $request_data)
249  {
250  $settings['image_assetid'] = $request_data['image_assetid']['assetid'];
251  $settings['variety_name'] = array_get_index($request_data, 'variety_name', '');
252  $settings['delete_all'] = array_get_index($request_data, 'delete_all', FALSE);
253  $settings['use_current'] = array_get_index($request_data, 'use_current', FALSE);
254 
255  // process parameter map
256  $new_prefix = str_replace(array('[',']'), '_', array_get_index($request_data, 'new_prefix', ''));
257  $atr_parameter_map = new Asset_Attribute_Parameter_Map();
258  $atr_parameter_map->process($new_prefix.'_parameter_map');
259 
260  $settings['value_map'] = $atr_parameter_map->value;
261 
262 
263  return FALSE;
264 
265  }//end processInterface()
266 
267 
268 }//end class
269 
270 ?>