Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design_css_customisation.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/designs/design_css/design_css.inc';
19 
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
55  function _getAllowedLinks()
56  {
57  $links = parent::_getAllowedLinks();
58  $links[SQ_LINK_TYPE_3]['file'] = Array('card' => 'M', 'exclusive' => FALSE);
59  return $links;
60 
61  }//end _getAllowedLinks()
62 
63 
72  public function deleteLink($linkid)
73  {
74  // first, we should try and find the link
75  $delete_link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid, $this->id);
76  if (empty($delete_link)) {
77  trigger_localised_error('CORE0155', E_USER_WARNING, $linkid);
78  return FALSE;
79  }
80 
81  $am = $GLOBALS['SQ_SYSTEM']->am;
82  $minor = $am->getAsset($delete_link['minorid'], $delete_link['minor_type_code']);
83 
84  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
85  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
86 
87  if (!$this->deleteExistingLink($linkid)) {
88  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
89  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
90  return FALSE;
91  }
92 
93  // special processing if we are deleting a TYPE_2 linked file (a file overridding a design file)
94  if ($delete_link['link_type'] == SQ_LINK_TYPE_2 && ($minor instanceof File)) {
95 
96  $design = $this->getParentDesign();
97 
98  // go through all the links of the design we are customisaing and find a file
99  // linked to it with the same name as the one we just deleted so it can take its place
100  $file_link_ids = Array();
101  $file_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($design->id, SQ_LINK_TYPE_2, 'file', FALSE);
102  foreach ($file_links as $link) {
103  $file_link_ids[$link['minorid']] = $link['linkid'];
104  }
105  $file_link_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_keys($file_link_ids));
106 
107  foreach ($file_link_info as $info) {
108  if ($info['name'] == $minor->name) {
109  // found the link to create
110  $new_minor = $am->getAsset($info['assetid']);
111  if (!$this->createLink($new_minor, SQ_LINK_TYPE_3, '', NULL, '1')) {
112  trigger_localised_error('CORE0162', E_USER_WARNING);
113  $am->forgetAsset($design);
114  $am->forgetAsset($new_minor);
115  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
116  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
117  return FALSE;
118  }
119  $am->forgetAsset($new_minor);
120  }
121  }//end foreach $file_links
122 
123  $am->forgetAsset($design);
124  }
125 
126  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
127  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
128  return TRUE;
129 
130  }//end deleteLink()
131 
132 
145  public function deleteExistingLink($linkid)
146  {
147  // first, we should try and find the link
148  $delete_link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid, $this->id);
149  if (empty($delete_link)) {
150  trigger_localised_error('CORE0155', E_USER_WARNING, $linkid);
151  return FALSE;
152  }
153 
154  $am = $GLOBALS['SQ_SYSTEM']->am;
155  $minor = $am->getAsset($delete_link['minorid'], $delete_link['minor_type_code']);
156 
157  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
158  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
159 
160  if (!parent::deleteLink($linkid)) {
161  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
162  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
163  return FALSE;
164  }
165 
166  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
167  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
168 
169  return TRUE;
170 
171  }//end deleteExistingLink()
172 
173 
182  public function linksUpdated()
183  {
184  parent::linksUpdated();
185  unset($this->_tmp['parent_design']);
186  unset($this->_tmp['customised_areas']);
187  unset($this->_tmp['uncustomised_areas']);
188 
189  }//end linksUpdated()
190 
191 
198  public function getParentDesign()
199  {
200  if (empty($this->_tmp['parent_design'])) {
201  $parent_link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_TYPE_2, 'design_css', FALSE, 'customisation', 'minor');
202  $this->_tmp['parent_design'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($parent_link['majorid'], $parent_link['major_type_code']);
203  }
204  return $this->_tmp['parent_design'];
205 
206  }//end getParentDesign()
207 
208 
215  public function getCustomisedAreas()
216  {
217  if (!isset($this->_tmp['customised_areas'])) {
218  $parent = $this->getParentDesign();
219  if (!is_null($parent)) {
220  // OK, get our design area links and our parents
221  $our_links = $this->getDesignAreaLink();
222  $parents_links = $parent->getDesignAreaLink();
223  $this->_tmp['customised_areas'] = Array();
224 
225  // Basically we do a search through our links and see if the design areas (ie the id_names)
226  // are pointing to the same assetid or not to ascertain whether we have customised this area or not
227  foreach ($our_links as $our_link) {
228  foreach ($parents_links as $parents_link) {
229  // if the id_names are the same
230  if ($our_link['value'] == $parents_link['value']) {
231  // if the id's are different, we have customised this one
232  if ($parents_link['minorid'] == $our_link['minorid']) {
233  $this->_tmp['uncustomised_areas'][] = $our_link;
234  } else {
235  $this->_tmp['customised_areas'][] = $our_link;
236  }
237  continue;
238  }//end if
239  }//end foreach
240  }//end foreach
241  }//end if
242  }//end if
243 
244  if (!isset($this->_tmp['customised_areas'])) {
245  $this->_tmp['customised_areas'] = Array();
246  }
247  if (!isset($this->_tmp['uncustomised_areas'])) {
248  $this->_tmp['uncustomised_areas'] = Array();
249  }
250 
251  return $this->_tmp['customised_areas'];
252 
253  }//end getCustomisedAreas()
254 
255 
262  public function getUnCustomisedAreas()
263  {
264  if (!isset($this->_tmp['uncustomised_areas'])) {
265  $this->getCustomisedAreas();
266  }//end if
267 
268  return $this->_tmp['uncustomised_areas'];
269 
270  }//end getUnCustomisedAreas()
271 
272 
283  public function updateFromParent(Design_CSS $parent, $update_children = TRUE)
284  {
285  $am = $GLOBALS['SQ_SYSTEM']->am;
286 
288  if (!$this->setAttrValue('contents', $parent->attr('contents')) || !$this->saveAttributes()) {
289  return FALSE;
290  }
291 
293 
294  $tmp = $parent->getDesignAreaLink();
295  $parent_da_links = Array();
296  foreach ($tmp as $link) {
297  $parent_da_links[$link['value']] = $link;
298  }
299 
300  $tmp = $this->getDesignAreaLink();
301  $this_da_links = Array();
302  foreach ($tmp as $link) {
303  $this_da_links[$link['value']] = $link;
304  }
305 
306  $remove_id_names = array_diff(array_keys($this_da_links), array_keys($parent_da_links));
307  $added_id_names = array_diff(array_keys($parent_da_links), array_keys($this_da_links));
308 
309  // now remove any unused design area customisations
310  foreach ($remove_id_names as $id_name) {
311  if (!$this->deleteLink($this_da_links[$id_name]['linkid'])) {
312  return FALSE;
313  }
314  }//end foreach
315 
316  foreach ($added_id_names as $id_name) {
317  $da = $am->getAsset($parent_da_links[$id_name]['minorid'], $parent_da_links[$id_name]['minor_type_code'], TRUE);
318  if (is_null($da)) continue;
319  $this->createLink($da, SQ_LINK_TYPE_3, $id_name, NULL, '1');
320  }//end foreach
321 
323 
324  $links = $this->getCustomisedAreas();
325  foreach ($links as $link) {
326  $da = $am->getAsset($link['minorid'], $link['minor_type_code']);
327  if (is_null($da)) continue;
328 
329  $parent_da = $parent->getDesignArea($link['value']);
330  if (is_null($parent_da)) continue;
331 
332  $da->setAttrValue('contents', $parent_da->attr('contents'));
333  $da->saveAttributes();
334 
335  }//end foreach
336 
338  $this->generateDesignFile(FALSE); // not recursive because update customisations takes care of that
339  if($update_children)
340  $this->updateCustomisations();
341 
342  return TRUE;
343 
344  }//end updateFromParent()
345 
346 
358  public function paint(Asset $ASSET, Array $FILE_URLS=Array())
359  {
360  // generate an array of all the URLs we are going to need for the files in this customisation
361  // that are inherited from the design we are customising
362  $file_link_ids = Array();
363  $file_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3, 'file', FALSE);
364  foreach ($file_links as $link) {
365  $file_link_ids[] = $link['minorid'];
366  }
367 
368  $file_link_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_unique($file_link_ids));
369  $file_link_urls = $GLOBALS['SQ_SYSTEM']->am->getAssetURL(array_unique($file_link_ids));
370 
371  foreach ($file_link_info as $minorid => $file_info) {
372  $file_url = $file_link_urls[$minorid];
373  if ($file_url == '') {
374  $file_url = "Javascript: alert(js_translate('no_url_found', '".addslashes($file_info['name'])."', '".addslashes($this->attr('id_name'))."'));";
375  } else if ($this->attr('static_url_versioning')){
376  $served_by_apache = strpos($file_url, '/__data/') !== FALSE || (SQ_CONF_STATIC_ROOT_URL && strpos($file_url, SQ_CONF_STATIC_ROOT_URL.'/') !== FALSE);
377  if ($served_by_apache) $file_url .= '?v='.$file_info['version'];
378  }
379  $FILE_URLS[$file_info['name']] = $file_url;
380  }
381 
382  return parent::paint($ASSET, $FILE_URLS);
383 
384  }//end paint()
385 
386 
387 }//end class
388 ?>