Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design_css_customisation_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/designs/design_css/design_css_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 
57  public function paintCustomisedDesignAreas(Design_CSS_Customisation $asset, Backend_Outputter $o, $prefix)
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, TRUE);
64  }
65 
66  }//end paintCustomisedDesignAreas()
67 
68 
79  public function processCustomisedDesignAreas(Design_CSS_Customisation $asset, Backend_Outputter $o, $prefix)
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);
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 
133  public function paintUnCustomisedDesignAreas(Design_CSS_Customisation $asset, Backend_Outputter $o, $prefix)
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 
155  public function processUnCustomisedDesignAreas(Design_CSS_Customisation $asset, Backend_Outputter $o, $prefix)
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 
164  $map = Array();
165  foreach ($_POST[$prefix.'_customise'] as $id_name) {
166  $link = $asset->getDesignAreaLink($id_name);
167  if (is_null($link)) continue;
168  $da = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code'], FALSE);
169  if (is_null($da)) continue;
170 
171  $create_link = Array (
172  'asset' => &$asset,
173  'link_type' => SQ_LINK_TYPE_3,
174  'value' => $link['value'],
175  'sort_order' => NULL,
176  'is_dependant' => 1,
177  'is_exclusive' => 0,
178  );
179 
180  $GLOBALS['SQ_SYSTEM']->am->acquireLock($da->id, 'all');
181 
182  // First let's duplicate the DA, ignoring the directory with the files
183  $clone = $GLOBALS['SQ_SYSTEM']->am->cloneAsset($da, $create_link, $map, Array('attributes', 'permissions', 'roles'), TRUE);
184 
185  $GLOBALS['SQ_SYSTEM']->am->releaseLock($da->id, 'all');
186  $GLOBALS['SQ_SYSTEM']->am->releaseLock($clone->id, 'all');
187 
188  if (is_null($clone)) {
189  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
190  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
191  return FALSE;
192  }
193 
194  $data[$id_name] = Array($clone->id, $clone->type());
195 
196  }//end foreach
197 
198  if ($data && !$this->_updateDesignAreaLink($asset, $data)) {
199  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
200  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
201  return FALSE;
202  }//end if
203 
204  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
205  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
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  foreach ($existing as $link) {
227  $existing_ids[$link['minorid']] = $link['linkid'];
228  }
229  $existing_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_keys($existing_ids));
230  foreach ($existing_info as $assetid => $existing_info) {
231  // if the name is the same, delete the link
232  if ($existing_info['name'] == strtolower($info['name'])) {
233  $linkid = $existing_ids[$assetid];
234  $asset->deleteExistingLink($linkid);
235 
236  // We need to update the lookups of the old linked asset
237  $minor_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
238  $minor_asset->updateLookups();
239  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($minor_asset);
240  }
241  }
242 
243  return parent::_processUploadedFile($asset, $info);
244 
245  }//end _processUploadedFile()
246 
247 
259  public function _updateDesignAreaLink(Asset $asset, Array $data)
260  {
261  $da_links = $asset->getDesignAreaLink();
262  $old_da_ids = Array();
263  foreach ($da_links as $da_link) {
264  $id_name = $da_link['value'];
265  // skip this design area if it is not in the list of design areas to update
266  if (empty($data[$id_name])) continue;
267 
268  // okay, this design area is in the list of design areas to update, but
269  // skip it if it is already link'dup correctly
270  if ($data[$id_name][0] == $da_link['minorid']) {
271  continue;
272  }
273 
274  $old_da_ids[] = $da_link['minorid'];
275 
276  // remove the old link (the one from the original design)
277  if (!$asset->deleteLink($da_link['linkid'])) {
278  return FALSE;
279  }
280 
281  // create the new link if required (wont be required if we created during a customisation)
282  $existing_link = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($asset->id, $data[$id_name][0]);
283  if (empty($existing_link)) {
284  // now create the new link
285  $da = $GLOBALS['SQ_SYSTEM']->am->getAsset($data[$id_name][0], $data[$id_name][1]);
286  if (is_null($da)) return FALSE;
287  if (!$asset->createLink($da, SQ_LINK_TYPE_3, $id_name, NULL, '1')) {
288  return FALSE;
289  }
290  }
291 
292  // remove any cached versions
293  if (isset($asset->_tmp['design_areas'])) {
294  unset($asset->_tmp['design_areas'][$id_name]);
295  }
296 
297  }//end foreach
298 
299  // Update our customisations to use our DAs where appropriate
300  $customisation_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'design_css_customisation', TRUE, 'major', 'customisation');
301  foreach ($customisation_links as $customisation_link) {
302  $customisation = $GLOBALS['SQ_SYSTEM']->am->getAsset($customisation_link['minorid'], $customisation_link['minor_type_code']);
303  if (is_null($customisation)) continue;
304 
305  // We don't want to overwrite already-customised design areas in the
306  // customisation, so we filter the update array to remove them.
307  // We do this by applying the rule that we can only unlink DAs
308  // from the customisation if we just unlinked them from ourselves
309  $cust_update_data = $data; // copy
310  foreach ($customisation->getCustomisedAreas() as $link) {
311  if (isset($cust_update_data[$link['value']])) {
312  if (!in_array($link['minorid'], $old_da_ids)) {
313  // this customisation has its own version of this DA already
314  // so don't cascade our DA to it
315  unset($cust_update_data[$link['value']]);
316  }
317  }
318  }
319  if (!$this->_updateDesignAreaLink($customisation, $cust_update_data)) {
320  return FALSE;
321  }
322  }
323 
324  return TRUE;
325 
326  }//end _updateDesignAreaLink()
327 
328 
341  protected function _paintDesignAreaList(Array $links, $check_box_col, $check_box_name, $write_access, $link_name=FALSE)
342  {
343  $am = $GLOBALS['SQ_SYSTEM']->am;
344  ?>
345  <table class="sq-backend-table">
346  <tr>
347  <th class="sq-backend-table-header" width="33%"><?php echo translate('type'); ?></th>
348  <th class="sq-backend-table-header" width="33%"><?php echo translate('core_design_area'); ?></th>
349  <?php
350  if ($write_access) {
351  ?>
352  <th class="sq-backend-table-header" width="34%" style="text-align: center;"><?php echo $check_box_col; ?></th>
353  <?php
354  }// end if
355  ?>
356  </tr>
357  <?php
358  uasort($links, Array(get_class($this), 'linkCompare'));
359  $current_type = '';
360  foreach ($links as $link) {
361  $da = $am->getAsset($link['minorid'], $link['minor_type_code']);
362 
363  if ($da->attr('id_name') == '') {
364  trigger_localised_error('CORE0154', E_USER_NOTICE, $da->id);
365  foreach ($links as $link) {
366  if ($link['minorid'] == $da->id) {
367  if (!empty($link['value'])) {
368  $da->setAttrValue('id_name', $link['value']);
369  $da->saveAttributes();
370  } else {
371  trigger_localised_error('CORE0149', E_USER_WARNING, $da->id);
372  }
373  break;
374  }
375  }
376  }
377 
378  if (is_null($da)) continue;
379  ?>
380  <tr>
381  <td class="sq-backend-table-cell">
382  <?php
383  if ($current_type != $da->type()) {
384  $current_type = $da->type();
385  echo '<i>', substr($current_type, 12), '</i>'; // take of the 'design_area_'
386  } else {
387  echo '&nbsp;';
388  }
389  ?>
390  </td>
391  <td class="sq-backend-table-cell" style="white-space: nowrap;">
392  <?php
393  if ($link_name) {
394  echo get_asset_tag_line($da->id, 'details');
395  } else {
396  echo get_asset_tag_line($da->id, '');
397  }
398  ?>
399  </td>
400  <?php
401  if ($write_access) {
402  ?>
403  <td class="sq-backend-table-cell" style="text-align: center;">
404  <input type="checkbox" name="<?php echo $check_box_name; ?>[]" value="<?php echo $da->attr('id_name');; ?>">
405  </td>
406  <?php
407  }// end if
408  ?>
409  </tr>
410  <?php
411  }//end foreach
412  ?>
413  </table>
414  <?php
415 
416  }//end _paintDesignAreaList()
417 
418 
429  protected function linkCompare($a, $b)
430  {
431  // if they don't match, return the value
432  if ($c = strcmp($a['minor_type_code'], $b['minor_type_code'])) {
433  return $c;
434  }
435  // otherwise the types are equal, so compare by id_name
436  return strcmp(strtolower($a['value']), strtolower($b['value']));
437 
438  }//end linkCompare()
439 
440 
441 }//end class
442 
443 ?>