Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
link.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 
31 class Link extends Asset
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
48 
56  public function _getAllowedLinks()
57  {
58  $allowed = parent::_getAllowedLinks();
59  $allowed[SQ_LINK_TYPE_1]['page'] = Array('card' => 'M', 'exclusive' => FALSE);
60  $allowed[SQ_LINK_TYPE_1]['file'] = Array('card' => 'M', 'exclusive' => FALSE);
61  $allowed[SQ_LINK_TYPE_1]['folder'] = Array('card' => 'M', 'exclusive' => FALSE);
62  $allowed[SQ_LINK_TYPE_1]['data_source'] = Array('card' => 'M', 'exclusive' => FALSE);
63  $allowed[SQ_LINK_TYPE_1]['link'] = Array('card' => 'M', 'exclusive' => FALSE);
64 
65  $allowed[SQ_LINK_TYPE_2]['page'] = Array('card' => 'M', 'exclusive' => FALSE);
66  $allowed[SQ_LINK_TYPE_2]['file'] = Array('card' => 'M', 'exclusive' => FALSE);
67  $allowed[SQ_LINK_TYPE_2]['folder'] = Array('card' => 'M', 'exclusive' => FALSE);
68  $allowed[SQ_LINK_TYPE_2]['data_source'] = Array('card' => 'M', 'exclusive' => FALSE);
69  $allowed[SQ_LINK_TYPE_2]['link'] = Array('card' => 'M', 'exclusive' => FALSE);
70 
71  $allowed[SQ_LINK_TYPE_3]['page'] = Array('card' => 'M', 'exclusive' => FALSE);
72 
73  $allowed[SQ_LINK_NOTICE]['page'] = Array('card' => 'M', 'exclusive' => FALSE);
74  $allowed[SQ_LINK_NOTICE]['link'] = Array('card' => 'M', 'exclusive' => FALSE);
75 
76  return $allowed;
77 
78  }//end _getAllowedLinks()
79 
80 
91  protected function _preCreateCheck(Array &$link)
92  {
93  if (!parent::_preCreateCheck($link)) return FALSE;
94 
95  $name = trim($this->attr('name'));
96  if ($name == '') {
97  trigger_localised_error('CORE0083', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
98  return FALSE;
99  }
100 
101  return TRUE;
102 
103  }//end _preCreateCheck()
104 
105 
116  protected function _createAdditional(Array &$link)
117  {
118  if (!parent::_createAdditional($link)) return FALSE;
119 
120  return $this->makeAndSaveInitialWebPath(strtolower($this->attr('name')), $link);
121 
122  }//end _createAdditional()
123 
124 
135  protected function _getName($short_name=FALSE, $contextid=NULL)
136  {
137  // No context specified, using the current context
138  if ($contextid === NULL) {
139  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
140  }//end if
141 
142  // Obtain the attribute value for Name from the Default Context
143  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
144  if (empty($values) === TRUE) {
145  return parent::_getName($short_name, $contextid);
146  } else {
147  return $values[$this->id];
148  }
149 
150  }//end _getName()
151 
152 
161  public function printBody()
162  {
163  $link_str = '<a href="'.$this->_getLinkURL().'"';
164 
165  $relation = $this->attr('link_relation');
166  if (!empty($relation)) {
167  $link_str .= ' rel="'.$relation.'"';
168  }
169 
170  $link_str .= '>'.$this->_getName().'</a>';
171 
172  echo $link_str;
173 
174  }//end printBody()
175 
176 
192  {
193  $keywords = parent::getAvailableKeywords();
194 
195  $asset = new Asset();
196 
197  foreach ($asset->getAvailableKeywords() as $keyword => $desc) {
198  $keywords['link_target_'.$keyword] = 'Referring Asset: '.$desc;
199  }
200  $keywords['link_url'] = 'The Link URL for this asset';
201  $keywords['link_relation'] = 'Link relation for this asset';
202 
203  return $keywords;
204 
205  }//end getAvailableKeywords()
206 
207 
222  function getKeywordReplacement($keyword)
223  {
224  $replacement = NULL;
225 
226  // Remove any modifiers from keyword
227  $full_keyword = $keyword;
228  $keyword = parse_keyword($keyword, $modifiers);
229  $contextid = extract_context_modifier($modifiers);
230 
231  if ($contextid !== NULL) {
232  // If we were able to extract a context ID to change to, and it's
233  // different to our current one, then change and then reload a copy
234  // of the asset in that context (as we would need to do anyway)
235 
236  if ((int)$contextid !== $GLOBALS['SQ_SYSTEM']->getContextId()) {
237  $GLOBALS['SQ_SYSTEM']->changeContext($contextid);
238  $contexted_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->id);
239 
240  // Get the keyword without any modifiers
241  $replacement = $contexted_asset->getKeywordReplacement($keyword);
242 
243  // Then apply the ones we had remaining, then return it
244  apply_keyword_modifiers($replacement, $modifiers, Array('assetid' => $contexted_asset->id));
245 
246  unset($contexted_asset);
247  $GLOBALS['SQ_SYSTEM']->restoreContext();
248  return $replacement;
249 
250  }//end if contextid is not the currently active context
251 
252  }//end if contextid is not NULL
253 
254  if (preg_match('|^link\_target\_(.*)|', $keyword, $matches)) {
255  $linked_asset = $this->getLinkedAsset();
256 
257  if ($linked_asset) {
258  $replacement = $linked_asset->getKeywordReplacement($matches[1]);
259  } else {
260  $replacement = '';
261  }
262  } else if ($keyword === 'link_url') {
263  $replacement = $this->_getLinkURL();
264  } else if ($keyword === 'link_relation') {
265  $replacement = $this->_getLinkRelation();
266  } else {
267  return parent::getKeywordReplacement($full_keyword);
268  }
269 
270  if (count($modifiers) > 0) {
271  apply_keyword_modifiers($replacement, $modifiers, Array('assetid' => $this->id));
272  }
273 
274  return $replacement;
275 
276  }//end getKeywordReplacement()
277 
278 
285  function &getLinkedAsset()
286  {
287  $linked_assetid = $this->attr('link_asset_id');
288  $linked_url = $this->attr('link_url');
289 
290  $linked_asset = NULL;
291 
292  // If we have assetid of linked asset then just get the asset, else try getting it from url
293  if (!empty($linked_assetid)) {
294  $linked_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($linked_assetid);
295 
296  } else if (!empty($linked_url)) {
297  // Strip protocal info and trailing query string from the url
298  $linked_url = preg_replace('|^.*://|', '', $linked_url);
299  $linked_url = preg_replace('|\?.*$|', '', $linked_url);
300 
301  $linked_asset = $GLOBALS['SQ_SYSTEM']->am->getAssetFromURL(NULL, $linked_url, FALSE, TRUE);
302  }
303 
304  return $linked_asset;
305 
306  }//end getLinkedAsset()
307 
308 
316  {
317  return $this->_getLinkURL();
318 
319  }//end getLinkUrlKeywordReplacement()
320 
321 
328  private function _getLinkURL()
329  {
330  $url = $this->attr('link_url');
331  if (empty($url) || (trim($url) == '')) {
332  $asset = NULL;
333  $assetid = $this->attr('link_asset_id');
334  $url = '';
335  if (!empty($assetid)) {
336  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, '', TRUE);
337  if (!is_null($asset)) $url = $asset->getURL();
338  }
339  }
340 
341  return $url;
342 
343  }//end _getLinkURL()
344 
345 
352  private function _getLinkRelation()
353  {
354  return $this->attr('link_relation') ? $this->attr('link_relation') : '';
355 
356  }//end _getLinkRelation()
357 
358 }//end class
359 ?>