Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
news_item.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
18 
29 class News_Item extends Page
30 {
31 
32 
38  function __construct($assetid=0)
39  {
40  parent::__construct($assetid);
41 
42  }//end constructor
43 
44 
51  public function lockTypes()
52  {
53  $lock_types = parent::lockTypes();
54  $lock_types['link_url'] = ($lock_types['attributes'] | $lock_types['links']);
55  return $lock_types;
56 
57  }//end lockTypes()
58 
59 
67  public function _getAllowedLinks()
68  {
69  $page_links = parent::_getAllowedLinks();
70  $page_links[SQ_LINK_NOTICE]['asset'] = Array('card' => 'M', 'exclusive' => false);
71  return $page_links;
72 
73  }//end _getAllowedLinks()
74 
75 
82  public function getLinkAsset()
83  {
84  if ($this->id == 0) {
85  $null = NULL;
86  return $null;
87  }
88 
89  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, '', true, 'link_asset');
90  if (empty($link)) {
91  $null = null;
92  return $null;
93  }
94 
95  return $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
96 
97  }//end getLinkAsset()
98 
99 
108  public function setLinkAsset($asset)
109  {
110  if (!is_null($asset)) {
111  assert_is_a($asset, 'asset');
112  }
113 
114  $old_link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, '', true, 'link_asset');
115  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
116  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
117  if (empty($old_link)) {
118  // if there is not existing asset, and we aren't setting on, we are done here
119  if (is_null($asset)) {
120  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
121  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
122  return true;
123  }
124 
125  // else if there is a link, and we are about to set an asset, but it is the same as we already have
126  } else if (!is_null($asset) && $old_link['minorid'] == $asset->id) {
127  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
128  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
129  return true;
130 
131  // else something different, delete the old link
132  } else {
133  if (!$this->deleteLink($old_link['linkid'])) {
134  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
135  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
136  return false;
137  }
138  }
139 
140  if (!is_null($asset)) {
141  if (!$this->createLink($asset, SQ_LINK_NOTICE, 'link_asset')) {
142  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
143  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
144  return false;
145  }
146  }
147 
148  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
149  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
150 
151  return true;
152 
153  }//end setLinkAsset()
154 
155 
166  protected function _getName($short_name=FALSE, $contextid=NULL)
167  {
168  // No context specified, using the current context
169  if ($contextid === NULL) {
170  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
171  }//end if
172 
173  // Obtain the attribute value for Name from the specified Context
174  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
175  if (empty($values) === TRUE) {
176  return parent::_getName($short_name, $contextid);
177  } else {
178  return $values[$this->id];
179  }
180 
181  }//end _getName()
182 
183 
192  public function printBody()
193  {
194  // start performance mode timer
195  $GLOBALS['SQ_SYSTEM']->pm->startTimer($this);
196 
197  // if there is no url, see if we have an asset selected
198  if (($url = $this->attr('link_url')) == '') {
199  $link_asset = $this->getLinkAsset();
200  if (!is_null($link_asset)) {
201  if (($url = $link_asset->getURL()) == '') {
202  $url = current_url().'?a='.$link_asset->id;
203  }
204  }
205  }
206 
207  if (!$url == '') {
208  $url .= trim($this->attr('url_suffix'));
209  }
210 
211  if ($this->attr('show_headline')) {
212  if (!$url == '') echo '<a href='.$url.'>';
213  ?>
214  <h1><?php echo $this->attr('name'); ?></h1>
215  <?php
216  if (!$url == '') echo '</a>';
217  }
218 
219  if ($this->attr('show_summary')) {
220  ?>
221  <div id="sq_news_summary">
222  <p><?php echo $this->attr('summary'); ?></p>
223  </div>
224  <?php
225  }
226 
227  if ($this->attr('show_body')) {
228  ?>
229  <div id="sq_news_body">
230  <p><?php echo $this->attr('body'); ?></p>
231  </div>
232  <?php
233  }
234 
235  if ($this->attr('show_contact_name') || $this->attr('show_contact_phone')) {
236  ?>
237  <div id="sq_news_contact">
238  <?php
239  if ($this->attr('show_contact_name')) {
240  ?>
241  <p><?php echo 'Contact: '.$this->attr('contact_name'); ?></p>
242  <?php
243  }
244 
245  if ($this->attr('show_contact_phone')) {
246  ?>
247  <p><?php echo 'Phone: '.$this->attr('contact_phone'); ?></p>
248  <?php
249  }
250  ?>
251  </div>
252  <?php
253  }
254 
255  // stop performance mode timer
256  $GLOBALS['SQ_SYSTEM']->pm->stopTimer($this);
257 
258  }//end printBody()
259 
260 
275  public function getAvailableKeywords()
276  {
277  $res = parent::getAvailableKeywords();
278  $res['asset_name_linked_to_link_url'] = 'Asset name linked to link URL';
279  $res['link_url'] = 'The Link URL for this item';
280  return $res;
281 
282  }//end getAvailableKeywords()
283 
284 
292  {
293  $url = $this->getLinkUrlKeywordReplacement();
294  if (empty($url)) {
295  // no URL or Asset, so link to the asset itself
296  return '<a href="'.$this->getURL().'">'.$this->attr('name').'</a>';
297  } else {
298  return '<a href="'.$url.'">'.$this->attr('name').'</a>';
299  }
300 
301  }//end getAssetNameLinkedToLinkURLKeywordReplacement()
302 
303 
311  {
312  $url = '';
313  $link_asset = &$this->getLinkAsset();
314  if (is_null($link_asset)) {
315  $link_url = trim($this->attr('link_url'));
316  if (!empty($link_url)) {
317  $url = $link_url;
318  $suffix = $this->attr('url_suffix');
319  if (!empty($suffix)) {
320  $url .= '?'.trim($suffix);
321  }
322  }
323  } else {
324  $url = $link_asset->getURL();
325  if (empty($url)) {
326  $url = current_url().'?a='.$link_asset->id;
327  }
328  }
329  return $url;
330 
331  }//end getLinkUrlKeywordReplacement()
332 
333 
340  public function backendAccess()
341  {
342  if (SQ_IN_LIMBO) {
343  return is_a($GLOBALS['SQ_SYSTEM']->user, 'user');
344  } else {
345  return parent::backendAccess();
346  }
347 
348  }//end backendAccess()
349 
350 
351 }//end class
352 ?>