Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design_customisation_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/designs/design/design_edit_fns.inc';
19 
32 {
33 
34 
39  function __construct()
40  {
41  parent::__construct();
42  $this->static_screens['details']['lock_type'] = 'all';
43 
44  }//end constructor
45 
46 
58  {
59  $customised_areas = $asset->getCustomisedAreas();
60  if (empty($customised_areas)) {
61  echo translate('core_design_customisation_no_design_areas_customised');
62  } else {
63  $this->_paintDesignAreaList($customised_areas, translate('core_remove_customisation_question'), $prefix.'_remove_customisation', $asset->writeAccess('links'), TRUE);
64  }
65 
66  }//end paintCustomisedDesignAreas()
67 
68 
80  {
81  if (empty($_POST[$prefix.'_remove_customisation'])) {
82  return FALSE;
83  }
84 
85  $parent_design = $asset->getParentDesign();
86  if (is_null($parent_design)) return FALSE;
87 
88  $data = Array();
89 
90  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
91  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
92 
93  foreach ($_POST[$prefix.'_remove_customisation'] as $id_name) {
94  $parents_link = $parent_design->getDesignAreaLink($id_name, 'design');
95  if (empty($parents_link)) continue;
96 
97  $data[$id_name] = Array($parents_link['minorid'], $parents_link['minor_type_code']);
98  }// end foreach
99 
100  if ($data && !$this->_updateDesignAreaLink($asset, $data)) {
101  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
102  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
103  return FALSE;
104  }// end if
105 
106  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
107  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
108 
109  // regenerate the design_file.php and css_file.php here (because the processBackend function
110  // doesn't do it automatically any more
111  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
112  $vars = Array(
113  'assetid' => $asset->id,
114  'parentid' => $parent_design->id,
115  );
116  $hh->queueHipo('hipo_job_regenerate_design', $vars);
117 
118  return TRUE;
119 
120  }//end processCustomisedDesignAreas()
121 
122 
134  {
135  $uncustomised_areas = $asset->getUnCustomisedAreas();
136  if (empty($uncustomised_areas)) {
137  echo translate('core_design_customisation_all_design_areas_customised');
138  } else {
139  $this->_paintDesignAreaList($uncustomised_areas, translate('core_customise_question'), $prefix.'_customise', $asset->writeAccess('links'));
140  }
141 
142  }//end paintUnCustomisedDesignAreas()
143 
144 
156  {
157  if (empty($_POST[$prefix.'_customise'])) return FALSE;
158 
159  $data = Array();
160 
161  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
162  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
163  $map = Array();
164  foreach ($_POST[$prefix.'_customise'] as $id_name) {
165  $link = $asset->getDesignAreaLink($id_name);
166  if (is_null($link)) continue;
167  $da = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code'], FALSE);
168  if (is_null($da)) continue;
169 
170  $create_link = Array (
171  'asset' => $asset,
172  'link_type' => SQ_LINK_TYPE_3,
173  'value' => $link['value'],
174  'sort_order' => NULL,
175  'is_dependant' => 1,
176  'is_exclusive' => 0,
177  );
178  $GLOBALS['SQ_SYSTEM']->am->acquireLock($da->id, 'all');
179 
180  // First let's duplicate the DA, ignoring the directory with the files
181  $clone = $GLOBALS['SQ_SYSTEM']->am->cloneAsset($da, $create_link, $map, Array('attributes', 'permissions', 'roles'), TRUE);
182  $GLOBALS['SQ_SYSTEM']->am->releaseLock($da->id, 'all');
183  $GLOBALS['SQ_SYSTEM']->am->releaseLock($clone->id, 'all');
184  if (is_null($clone)) {
185  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
186  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
187  return FALSE;
188  }
189 
190  $data[$id_name] = Array($clone->id, $clone->type());
191 
192  }// end foreach
193 
194  if ($data && !$this->_updateDesignAreaLink($asset, $data)) {
195  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
196  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
197  return FALSE;
198  }// end if
199 
200  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
201  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
202 
203  // Acquire lock on 'links', in case of file uploading, it demands lock on 'links'
204  $GLOBALS['SQ_SYSTEM']->am->acquireLock($asset->id, 'links');
205 
206  return TRUE;
207 
208  }//end processUnCustomisedDesignAreas()
209 
210 
220  public function _processUploadedFile(Asset $asset, Array $info=Array())
221  {
222  // if we already have a file with the same name TYPE_3 linked to us,
223  // delete this link so we link up this new file
224  $existing_ids = Array();
225  $existing = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_3, 'file', FALSE);
226 
227  foreach ($existing as $link) {
228  $existing_ids[$link['minorid']] = $link['linkid'];
229  }
230  $existing_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_keys($existing_ids));
231  foreach ($existing_info as $id => $existing_info) {
232  // if the name is the same, delete the link
233  $new_file_name = make_valid_web_paths(Array($info['name']));
234  $new_file_name = array_shift($new_file_name);
235 
236  if ($existing_info['name'] == $new_file_name) {
237  $linkid = $existing_ids[$id];
238 
239  // We need to update the (ex)-TYPE_3-linked file's lookups as
240  // well as that on the design
241  $minor_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($id);
242 
243  $asset->deleteExistingLink($linkid);
244 
245  $minor_asset->updateLookups();
246  $asset->updateLookups();
247  }
248  }
249  return parent::_processUploadedFile($asset, $info);
250 
251  }//end _processUploadedFile()
252 
253 
265  public function _updateDesignAreaLink(Asset $asset, Array $data)
266  {
267  $da_links = $asset->getDesignAreaLink();
268  $old_da_ids = Array();
269  foreach ($da_links as $da_link) {
270  $id_name = $da_link['value'];
271  // skip this design area if it is not in the list of design areas to update
272  if (empty($data[$id_name])) continue;
273 
274  // okay, this design area is in the list of design areas to update, but
275  // skip it if it is already link'dup correctly
276  if ($data[$id_name][0] == $da_link['minorid']) {
277  continue;
278  }
279 
280  $old_da_ids[] = $da_link['minorid'];
281 
282  // remove the old link (the one from the original design)
283  if (!$asset->deleteLink($da_link['linkid'])) {
284  return FALSE;
285  }
286 
287  // create the new link if required (wont be required if we created during a customisation)
288  $existing_link = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($asset->id, $data[$id_name][0]);
289  if (empty($existing_link)) {
290  // now create the new link
291  $da = $GLOBALS['SQ_SYSTEM']->am->getAsset($data[$id_name][0], $data[$id_name][1]);
292  if (is_null($da)) return FALSE;
293  if (!$asset->createLink($da, SQ_LINK_TYPE_3, $id_name, NULL, '1')) {
294  return FALSE;
295  }
296  }
297 
298  // remove any cached versions
299  if (isset($asset->_tmp['design_areas'])) {
300  unset($asset->_tmp['design_areas'][$id_name]);
301  }
302 
303  }//end foreach
304 
305  // Update our customisations to use our DAs where appropriate
306  $customisation_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'design_customisation', TRUE, 'major', 'customisation');
307  foreach ($customisation_links as $customisation_link) {
308  $customisation = $GLOBALS['SQ_SYSTEM']->am->getAsset($customisation_link['minorid'], $customisation_link['minor_type_code']);
309  if (is_null($customisation)) continue;
310 
311  // We don't want to overwrite already-customised design areas in the
312  // customisation, so we filter the update array to remove them.
313  // We do this by applying the rule that we can only unlink DAs
314  // from the customisation if we just unlinked them from ourselves
315  $cust_update_data = $data; // copy
316  foreach ($customisation->getCustomisedAreas() as $link) {
317  if (isset($cust_update_data[$link['value']])) {
318  if (!in_array($link['minorid'], $old_da_ids)) {
319  // this customisation has its own version of this DA already
320  // so don't cascade our DA to it
321  unset($cust_update_data[$link['value']]);
322  }
323  }
324  }
325  if (!$this->_updateDesignAreaLink($customisation, $cust_update_data)) {
326  return FALSE;
327  }
328  }
329 
330  return TRUE;
331 
332  }//end _updateDesignAreaLink()
333 
334 
347  protected function _paintDesignAreaList(Array $links, $check_box_col, $check_box_name, $write_access, $link_name=FALSE)
348  {
349  $am = $GLOBALS['SQ_SYSTEM']->am;
350  ?>
351  <table class="sq-backend-table">
352  <tr>
353  <th class="sq-backend-table-header" width="33%"><?php echo translate('type'); ?></th>
354  <th class="sq-backend-table-header" width="33%"><?php echo translate('core_design_area'); ?></th>
355  <?php
356  if ($write_access) {
357  ?>
358  <th class="sq-backend-table-header" width="34%" style="text-align: center;"><?php echo $check_box_col; ?></th>
359  <?php
360  }// end if
361  ?>
362  </tr>
363  <?php
364  uasort($links, Array(get_class($this), 'linkCompare'));
365  $current_type = '';
366  foreach ($links as $link) {
367  $da = $am->getAsset($link['minorid'], $link['minor_type_code']);
368 
369  if ($da->attr('id_name') == '') {
370  trigger_localised_error('CORE0154', E_USER_NOTICE, $da->id);
371  foreach ($links as $link) {
372  if ($link['minorid'] == $da->id) {
373  if (!empty($link['value'])) {
374  $da->setAttrValue('id_name', $link['value']);
375  $da->saveAttributes();
376  } else {
377  trigger_localised_error('CORE0149', $da->id, E_USER_WARNING);
378  }
379  break;
380  }
381  }
382  }
383 
384  if (is_null($da)) continue;
385  ?>
386  <tr>
387  <td class="sq-backend-table-cell">
388  <?php
389  if ($current_type != $da->type()) {
390  $current_type = $da->type();
391  echo '<i>', substr($current_type, 12), '</i>'; // take of the 'design_area_'
392  } else {
393  echo '&nbsp;';
394  }
395  ?>
396  </td>
397  <td class="sq-backend-table-cell" style="white-space: nowrap;">
398  <?php
399  if ($link_name) {
400  echo get_asset_tag_line($da->id, 'details');
401  } else {
402  echo $da->name.' (Id: #'.$da->id.')';
403  }
404  ?>
405  </td>
406  <?php
407  if ($write_access) {
408  ?>
409  <td class="sq-backend-table-cell" style="text-align: center;">
410  <input type="checkbox" name="<?php echo $check_box_name; ?>[]" value="<?php echo $da->attr('id_name');; ?>">
411  </td>
412  <?php
413  }// end if
414  ?>
415  </tr>
416  <?php
417  }//end foreach
418  ?>
419  </table>
420  <?php
421 
422  }//end _paintDesignAreaList()
423 
424 
435  public static function linkCompare($a, $b)
436  {
437  // if they don't match, return the value
438  if ($c = strcmp($a['minor_type_code'], $b['minor_type_code'])) {
439  return $c;
440  }
441  // otherwise the types are equal, so compare by id_name
442  return strcmp(strtolower($a['value']), strtolower($b['value']));
443 
444  }//end linkCompare()
445 
446 
447 }//end class
448 
449 ?>