Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_design.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
18 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
19 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
20 require_once SQ_SYSTEM_ROOT.'/core/attributes/parameter_map/parameter_map.inc';
21 
35 {
36 
37 
57  public static function execute($settings, &$state)
58  {
59 
60  // see if the id is set in the parameter map
61  $value_map = array_get_index($settings, 'value_map', serialize(Array()));
62  $param_map_attr = new Asset_Attribute_Parameter_Map(0, $value_map);
63  $param_map_value = $param_map_attr->getParameterValue('design_id');
64 
65  // check settings, state
66  if (empty($settings['assetid']) && empty($param_map_value)) {
67  // if no settings, fail
68  return FALSE;
69  }
70 
71  // whether we use the parameter map value or the settings value for the target asset
72  $assetid = is_null($param_map_value) ? $settings['assetid'] : $param_map_value;
73 
74  if (!$GLOBALS['SQ_SYSTEM']->am->assetExists($assetid)) {
75  return FALSE;
76  }
77 
78  $design_type = array_get_index($settings, 'design_type', 'design::system');
79  $design_name = array_get_index($settings, 'design_name', '::user_define');
80 
81  // state
82  if (empty($state['asset'])) {
83  // grab the asset if assetid is given, but not the asset.
84  if (empty($state['assetid'])) {
85  return FALSE;
86  } else {
87  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
88  }
89  }
90 
91 
92  $am = $GLOBALS['SQ_SYSTEM']->am;
93 
94  // setting the default frontend design
95  if ($design_type == 'design::system') {
96  $design_final_name = 'design::system::frontend';
97  } else if ($design_type == 'design::user') {
98  $design_final_name = 'design::user::'.$design_name;
99  } else {
100  $design_final_name = $design_type;
101  }
102 
103  // get all system_design::frontend links attached to the asset
104  $design_links = $am->getLinks($state['assetid'], SQ_LINK_NOTICE, 'design', FALSE, 'major', $design_final_name);
105  $link_already_created = FALSE;
106 
107  if (!empty($design_links)) {
108 
109  // Wipe them out. All of them.
110  foreach ($design_links as $link) {
111 
112  // if the new design is already linked, leave it alone. delete duplicates if found
113  if ($link['minorid'] == $assetid && !$link_already_created) {
114  // so that we don't try and recreate the link later if it already exists
115  $link_already_created = TRUE;
116  } else {
117  if (!$am->deleteAssetLink($link['linkid'])) {
118  // if we try to create another system_design::frontend while one already exists, we
119  // end up in a whole world of pain. Bail now.
120  return FALSE;
121  }
122  }
123 
124  }
125  }//end if(!empty())
126 
127  // create a link between the asset (major) and the design (minor)
128  if (!$link_already_created) {
129  $design = $am->getAsset($assetid);
130  $link_create_status = $am->createAssetLink($state['asset'], $design, SQ_LINK_NOTICE, $design_final_name);
131 
132  if (!$link_create_status) {
133  // couldn't create link, die
134  return FALSE;
135  }
136  }
137 
138  // Do it by HIPO so that the children assets are also affected.
139  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
140  $vars = Array('assetids' => Array($state['asset']->id));
141  $hh->freestyleHipo('hipo_job_update_lookups', $vars);
142  return Array(
143  'assetid' => $state['asset']->id,
144  'designid' => $assetid,
145  'code' => 'system::frontend',
146  );
147 
148  }//end execute()
149 
150 
161  public static function getInterface($settings, $prefix, $write_access=FALSE)
162  {
163 
164  if (empty($settings['assetid'])) {
165  // set to default
166  $selected_assetid = '0';
167  } else {
168  $selected_assetid = $settings['assetid'];
169  }
170 
171  $design_type = array_get_index($settings, 'design_type', '');
172  $design_name = array_get_index($settings, 'design_name', '');
173 
174  // check if the selected asset is valid
175  if ($selected_assetid && !$GLOBALS['SQ_SYSTEM']->am->assetExists($selected_assetid)) {
176  $selected_assetid = 0;
177  }
178 
179  if (!$write_access) {
180  $form_element_extras = 'disabled="disabled"';
181  } else {
182  $form_element_extras = '';
183  }
184 
185  $design_types = Array (
186  'design::system::frontend' => 'Frontend Design',
187  'design::system::login' => 'Login Design',
188  'design::user' => 'User Defined Design',
189  );
190 
191  // begin buffering basic options
192 
193  $disabled = ($design_type == 'design::user') ? '' : 'disabled="disabled"';
194  $extra = 'onChange="if(this.value == \'design::user\') { document.getElementById(\''.$prefix.'[design_name]'.'\').disabled=\'\';}
195  else { document.getElementById(\''.$prefix.'[design_name]'.'\').disabled=\'disabled\'; }"';
196  ob_start();
197  if ($write_access) {
198  asset_finder($prefix.'[assetid]', $selected_assetid, Array('design' => 'D'));
199  echo '<br /><br />';
200  echo translate('trigger_set_design_select_design_type');
201  combo_box($prefix.'[design_type]', $design_types, FALSE, $design_type, 0, $extra);
202  echo '<br /><br />';
203  echo translate('trigger_set_design_set_user_design_name');
204  text_box($prefix.'[design_name]', $design_name, '', '', FALSE, $disabled);
205  } else {
206  if ($selected_assetid) {
207  $selected_assetname = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($selected_assetid), Array(), TRUE, 'name');
208  text_box($prefix.'[assetid]', $selected_assetname[$selected_assetid].' (#'.$selected_assetid.')', '', '', FALSE, $form_element_extras);
209  } else {
210  text_box($prefix.'[assetid]', '', '', '', FALSE, $form_element_extras);
211  }
212  }
213  // prints the parameter map to accept dynamic values
214  if ($write_access) {
215  // attribute friendly prefix
216  $new_prefix = str_replace(array('[',']'), '_', $prefix);
217  hidden_field($prefix.'[new_prefix]', $new_prefix);
218  $param_map_value = array_get_index($settings, 'value_map', serialize(Array()));
219  $param_map_attr = new Asset_Attribute_Parameter_Map(0, $param_map_value);
220  $param_map_attr->setParameter('design_id', 'Design ID');
221  echo $param_map_attr->paint($new_prefix.'_parameter_map');
222  }
223 
224  $basic_part_1 = ob_get_contents();
225  ob_end_clean();
226 
227  return translate('trigger_set_design_basic', $basic_part_1);
228 
229  }//end getInterface()
230 
231 
243  public static function processInterface(&$settings, $request_data)
244  {
245 
246  // check if the status is valid
247  $settings['assetid'] = $request_data['assetid']['assetid'];
248  $settings['design_type'] = $request_data['design_type'];
249  if (!empty($request_data['design_name'])) {
250  $settings['design_name'] = $request_data['design_name'];
251  }
252 
253  // parameter map to accept dynamic value
254  $new_prefix = str_replace(array('[',']'), '_', array_get_index($request_data, 'new_prefix', ''));
255  if ($new_prefix) {
256  $param_map_attr = new Asset_Attribute_Parameter_Map();
257  $param_map_attr->process($new_prefix.'_parameter_map');
258  $settings['value_map'] = $param_map_attr->value;
259  }
260 
261  $value_map = unserialize($settings['value_map']);
262  // make sure the assetid isn't blank
263  if (empty($settings['assetid']) && empty($value_map)) {
264  return 'Design Asset id is not specified';
265  }
266  return FALSE;
267 
268  }//end processInterface()
269 
270 
280  public static function getLocks($settings, &$state)
281  {
282  return Array(
283  $state['assetid'] => Array(
284  'links',
285  'lookups',
286  ),
287  );
288 
289  }//end getLocks()
290 
291 
292 }//end class
293 
294 ?>