Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design_customisation.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/designs/design/design.inc';
19 
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
55  public function _getAllowedLinks()
56  {
57  $links = parent::_getAllowedLinks();
58  $links[SQ_LINK_TYPE_1]['file'] = Array('card' => 'M', 'exclusive' => FALSE);
59  $links[SQ_LINK_TYPE_2]['file'] = Array('card' => 'M', 'exclusive' => FALSE);
60  $links[SQ_LINK_TYPE_3]['file'] = Array('card' => 'M', 'exclusive' => FALSE);
61  return $links;
62 
63  }//end _getAllowedLinks()
64 
65 
86  public function prepareLink(Asset $asset, $side_of_link, &$link_type, &$value, &$sort_order, &$dependant, &$exclusive)
87  {
88  if ($side_of_link == 'minor' && $asset instanceof Design) {
89  $value = 'customisation';
90  return TRUE;
91  }
92 
93  return FALSE;
94 
95  }//end prepareLink()
96 
97 
106  function deleteLink($linkid)
107  {
108  // first, we should try and find the link
109  $delete_link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid, $this->id);
110  if (empty($delete_link)) {
111  trigger_localised_error('CORE0155', E_USER_WARNING, $linkid);
112  return FALSE;
113  }
114 
115  $am =& $GLOBALS['SQ_SYSTEM']->am;
116  $minor = $am->getAsset($delete_link['minorid'], $delete_link['minor_type_code']);
117 
118  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
119  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
120 
121  if (!$this->deleteExistingLink($linkid)) {
122  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
123  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
124  return FALSE;
125  }
126 
127  // special processing if we are deleting a TYPE_2 linked file (a file overridding a design file)
128  if ($delete_link['link_type'] == SQ_LINK_TYPE_2 && ($minor instanceof File)) {
129 
130  $design = $this->getParentDesign();
131 
132  // if we have not been orphaned from a design...
133  if ($design) {
134  // go through all the links of the design we are customising and find a file
135  // linked to it with the same name as the one we just deleted so it can take its place
136  $file_link_ids = Array();
137  $file_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($design->id, SQ_LINK_TYPE_2, 'file', FALSE);
138  foreach ($file_links as $link) {
139  $file_link_ids[$link['minorid']] = $link['linkid'];
140  }
141  $file_link_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_keys($file_link_ids));
142 
143  foreach ($file_link_info as $assetid => $info) {
144  if ($info['name'] == $minor->name) {
145  // found the link to create
146  $new_minor = $am->getAsset($assetid);
147  if (!$this->createLink($new_minor, SQ_LINK_TYPE_3, '', NULL, '1')) {
148  trigger_localised_error('CORE0162', E_USER_WARNING);
149  $am->forgetAsset($design);
150  $am->forgetAsset($new_minor);
151  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
152  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
153  return FALSE;
154  }
155  $am->forgetAsset($new_minor);
156  }
157  }//end foreach $file_links
158 
159  $am->forgetAsset($design);
160  }
161 
162  }//end if is_a file
163 
164  // special processing if we are deleting a TYPE_2 linked CSS file (a CSS file overridding a design file)
165  if ($delete_link['link_type'] == SQ_LINK_TYPE_2 && ($minor instanceof Design_Css)) {
166 
167  $design = $this->getParentDesign();
168 
169  // go through all the links of the design we are customisaing and find a css file
170  // linked to it with the same name as the one we just deleted so it can take its place
171  $css_link_ids = Array();
172  $css_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($design->id, SQ_LINK_TYPE_2, 'design_css', FALSE);
173  foreach ($css_links as $link) {
174  $css_link_ids[$link['minorid']] = $link['linkid'];
175  }
176  $css_link_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_keys($css_link_ids));
177 
178  foreach ($css_link_info as $info) {
179  if ($info['name'] == $minor->name) {
180  // found the link to create
181  $new_minor = $am->getAsset($info['assetid']);
182  if (!$this->createLink($new_minor, SQ_LINK_TYPE_3, '', NULL, '1')) {
183  trigger_localised_error('CORE0161', E_USER_WARNING);
184  $am->forgetAsset($design);
185  $am->forgetAsset($new_minor);
186  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
187  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
188  return FALSE;
189  }
190  $am->forgetAsset($new_minor);
191  }
192  }//end foreach $file_links
193 
194  $am->forgetAsset($design);
195  }
196 
197  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
198  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
199  return TRUE;
200 
201  }//end deleteLink()
202 
203 
216  public function deleteExistingLink($linkid)
217  {
218  // first, we should try and find the link
219  $delete_link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid, $this->id);
220  if (empty($delete_link)) {
221  trigger_localised_error('CORE0155', E_USER_WARNING, $linkid);
222  return FALSE;
223  }
224 
225  $am = $GLOBALS['SQ_SYSTEM']->am;
226  $minor = $am->getAsset($delete_link['minorid'], $delete_link['minor_type_code']);
227 
228  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
229  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
230 
231  if (!parent::deleteLink($linkid)) {
232  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
233  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
234  return FALSE;
235  }
236 
237  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
238  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
239 
240  return TRUE;
241 
242  }//end deleteExistingLink()
243 
244 
253  public function linksUpdated()
254  {
255  parent::linksUpdated();
256  unset($this->_tmp['parent_design']);
257  unset($this->_tmp['customised_areas']);
258  unset($this->_tmp['uncustomised_areas']);
259 
260  }//end linksUpdated()
261 
262 
269  public function getParentDesign()
270  {
271  if (empty($this->_tmp['parent_design'])) {
272  $parent_link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_TYPE_2, 'design', FALSE, 'customisation', 'minor');
273  if (!$parent_link) {
274  $null_asset = NULL;
275  return $null_asset;
276  } else {
277  $this->_tmp['parent_design'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($parent_link['majorid'], $parent_link['major_type_code']);
278  }
279  }
280  return $this->_tmp['parent_design'];
281 
282  }//end getParentDesign()
283 
284 
291  public function getCustomisedAreas()
292  {
293  if (!isset($this->_tmp['customised_areas'])) {
294  $parent = $this->getParentDesign();
295  if (!is_null($parent)) {
296  // OK, get our design area links and our parents
297  $our_links = $this->getDesignAreaLink();
298  $parents_links = $parent->getDesignAreaLink();
299  $this->_tmp['customised_areas'] = Array();
300 
301  // Basically we do a search through our links and see if the design areas (ie the id_names)
302  // are pointing to the same assetid or not to ascertain whether we have customised this area or not
303  foreach ($our_links as $our_link) {
304  foreach ($parents_links as $parents_link) {
305  // if the id_names are the same
306  if ($our_link['value'] == $parents_link['value']) {
307  // if the id's are different, we have customised this one
308  if ($parents_link['minorid'] == $our_link['minorid']) {
309  $this->_tmp['uncustomised_areas'][] = $our_link;
310  } else {
311  $this->_tmp['customised_areas'][] = $our_link;
312  }
313  continue;
314  }//end if
315  }//end foreach
316  }//end foreach
317  }//end if
318  }//end if
319 
320  if (!isset($this->_tmp['customised_areas'])) {
321  $this->_tmp['customised_areas'] = Array();
322  }
323  if (!isset($this->_tmp['uncustomised_areas'])) {
324  $this->_tmp['uncustomised_areas'] = Array();
325  }
326 
327  return $this->_tmp['customised_areas'];
328 
329  }//end getCustomisedAreas()
330 
331 
338  public function getUnCustomisedAreas()
339  {
340  if (!isset($this->_tmp['uncustomised_areas'])) {
341  $this->getCustomisedAreas();
342  }//end if
343 
344  return $this->_tmp['uncustomised_areas'];
345 
346  }//end getUnCustomisedAreas()
347 
348 
360  public function updateFromParent(Design $parent, $update_children = TRUE)
361  {
362  $am = $GLOBALS['SQ_SYSTEM']->am;
363 
365  if (!$this->setAttrValue('contents', $parent->attr('contents')) || !$this->saveAttributes()) {
366  return FALSE;
367  }
368 
370 
371  $tmp = $parent->getDesignAreaLink();
372  $parent_da_links = Array();
373  foreach ($tmp as $link) {
374  $parent_da_links[$link['value']] = $link;
375  }
376 
377  $tmp = $this->getDesignAreaLink();
378  $this_da_links = Array();
379  foreach ($tmp as $link) {
380  $this_da_links[$link['value']] = $link;
381  }
382 
383  $remove_id_names = array_diff(array_keys($this_da_links), array_keys($parent_da_links));
384  $added_id_names = array_diff(array_keys($parent_da_links), array_keys($this_da_links));
385 
386  // now remove any unused design area customisations
387  foreach ($remove_id_names as $id_name) {
388  if (!$this->deleteLink($this_da_links[$id_name]['linkid'])) {
389  return FALSE;
390  }
391  }//end foreach
392 
393  foreach ($added_id_names as $id_name) {
394  $da = $am->getAsset($parent_da_links[$id_name]['minorid'], $parent_da_links[$id_name]['minor_type_code'], TRUE);
395  if (is_null($da)) continue;
396  $this->createLink($da, SQ_LINK_TYPE_3, $id_name, NULL, '1');
397  }//end foreach
398 
400 
401  $links = $this->getCustomisedAreas();
402  foreach ($links as $link) {
403  $da = $am->getAsset($link['minorid'], $link['minor_type_code']);
404  if (is_null($da)) continue;
405 
406  $parent_da = $parent->getDesignArea($link['value']);
407  if (is_null($parent_da)) continue;
408  foreach ($parent_da->vars as $name => $details) {
409  if (FALSE !== strpos($name, 'contents')) {
410  $da->setAttrValue($name, $parent_da->attr($name));
411  }
412  }
413 
414  // For updating design areas that don't set contents, such as Declared vars areas,
415  // we need to check for declared_vars as well
416  if (isset($da->vars['declared_vars'])) {
417  $da->mergeParentDeclaredVars($parent_da->attr('declared_vars'));
418  }
419 
420  $da->saveAttributes();
421 
422  }//end foreach
423 
425  $this->generateDesignFile(FALSE); // not recursive because update customisations takes care of that
426  if($update_children)
427  $this->updateCustomisations();
428 
429  return TRUE;
430 
431  }//end updateFromParent()
432 
433 
446  public function paint(Asset $ASSET, Array $FILE_URLS=Array(), Array $CSS_URLS=Array())
447  {
448  // generate an array of all the URLs we are going to need for the files in this customisation
449  // that are inherited from the design we are customising
450  $file_link_ids = Array();
451  $file_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3, 'file', FALSE);
452  foreach ($file_links as $link) {
453  $file_link_ids[] = $link['minorid'];
454  }
455  $file_link_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_unique($file_link_ids));
456  $file_link_urls = $GLOBALS['SQ_SYSTEM']->am->getAssetURL(array_unique($file_link_ids));
457 
458  foreach ($file_link_info as $minorid => $file_info) {
459  $file_url = $file_link_urls[$minorid];
460  if ($file_url == '') {
461  $file_url = "Javascript: alert(js_translate('no_url_found', '".addslashes($file_info['name'])."', '".addslashes($this->attr('id_name'))."'));";
462  } else if ($this->attr('static_url_versioning')){
463  $served_by_apache = strpos($file_url, '/__data/') !== FALSE || (SQ_CONF_STATIC_ROOT_URL && strpos($file_url, SQ_CONF_STATIC_ROOT_URL.'/') !== FALSE);
464  if ($served_by_apache) $file_url .= '?v='.$file_info['version'];
465  }
466  $FILE_URLS[$file_info['name']] = $file_url;
467  }
468 
469  // generate an array of all the URLs we are going to need for the attached CSS in this design
470  $css_link_ids = Array();
471  $css_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3, 'design_css', FALSE);
472  foreach ($css_links as $link) {
473  $css_link_ids[] = $link['minorid'];
474  }
475 
476  $css_link_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_unique($css_link_ids));
477  $css_link_urls = $GLOBALS['SQ_SYSTEM']->am->getAssetURL(array_unique($css_link_ids));
478 
479  foreach ($css_link_info as $minorid => $css_info) {
480  $css_url = $css_link_urls[$minorid];
481  if ($css_url == '') {
482  $css_url = "Javascript: alert(js_translate('no_url_found', '".addslashes($file_info['name'])."', '".addslashes($this->attr('id_name'))."'));";
483  } else if ($this->attr('static_url_versioning')){
484  $served_by_apache = strpos($css_url, '/__data/') !== FALSE || (SQ_CONF_STATIC_ROOT_URL && strpos($css_url, SQ_CONF_STATIC_ROOT_URL.'/') !== FALSE);
485  if ($served_by_apache) $css_url .= '?v='.$css_info['version'];
486  }
487  $CSS_URLS[$css_info['name']] = $css_url;
488  }
489 
490  return parent::paint($ASSET, $FILE_URLS, $CSS_URLS);
491 
492  }//end paint()
493 
494 
495 }//end class
496 ?>