Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_whats_new.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/cms/listing_engine/listing_engine.inc';
19 
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
64  function getAssetList()
65  {
66  $group_by = $this->attr('group_by');
67  $bind_vars = Array();
68 
69  // this is the list of all type codes that we are going to show
70  // we pass this list to the getLinks and getChildren functions (below) to narrow
71  // the query down a bit
72  // if there are no types to list, we cant list anything
73  $wanted_types = $this->attr('types');
74  if (empty($wanted_types)) {
75  trigger_localised_error('CMS0010', E_USER_NOTICE);
76  return Array();
77  }
78 
79  // get the root asset whose sub-assets/children we are displaying
80  // if there is no root asset, we will use ourselves and print our children
81  $root_asset_ids = $this->getRootNodes();
82 
83  $sort_info = $this->getSortInfo();
84  $group_by = $this->attr('group_by');
85 
86  // if we are showing an A-Z listing but the user has not selected a proper sort field,
87  // we will use the 'name' attribute of the asset to work out the listing
88  $letter_sort_by = 'name';
89  if ($group_by == 'letter') {
90  if (!empty($sort_info) && ($sort_info['type'] == 'field') && ($sort_info['params']['field'] == 'short_name')) {
91  $letter_sort_by = 'short_name';
92  }
93  }
94 
95  $children = Array();
96  $base_bind_vars = Array();
97 
98  // this is the field in the database that we are using the specify that something is "new"
99  $field = $this->attr('date_field');
100  $ranges = $this->attr('date_ranges');
101 
102  $link_value_wanted = NULL;
103  if ($this->attr('link_value_filter_enabled')) {
104  // the data stored in the DB was encoded to protect against cross scripting so we use htmlentities
105  $link_value_wanted = htmlentities($this->attr('link_value_filter'));
106  }
107 
108  // work out the where clause that is going to find "new" assets
109  $where = '';
110  require_once SQ_ATTRIBUTES_PATH.'/datetime/datetime.inc';
111 
112  if (!empty($ranges) && isset($_REQUEST['asset_'.$this->id.'_date_range']) && isset($ranges[$_REQUEST['asset_'.$this->id.'_date_range']])) {
113  $id = $_REQUEST['asset_'.$this->id.'_date_range'];
114  $this->setAttrValue('num_per_page', 0); // If we're displaying a range, clear the pagination
115 
116  if ($ranges[$id]['range_type'] !== 'default') {
117  switch ($ranges[$id]['range_type']) {
118  case 'today':
119  $from = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime('00:00:00 today')));
120  break;
121 
122  case 'week':
123  // If it's a Monday, then doing "next Monday, 1 week ago"
124  // will make it one week out.
125  if ((int)date('w') === 1) {
126  $from = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime('00:00:00 today')));
127  } else {
128  $from = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime('00:00:00 Monday -1 week')));
129  }
130  break;
131 
132  case 'month':
133  $from = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime(date('Y-m').'-01 00:00:00')));
134  break;
135 
136  case 'year':
137  $from = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime(date('Y').'-01-01 00:00:00')));
138  break;
139  }
140 
141  $to = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime('now')));
142  } else {
143  $from = new Asset_Attribute_DateTime();
144  $from->value = $ranges[$id]['oldest_date'];
145 
146  $to = new Asset_Attribute_DateTime();
147  $to->value = $ranges[$id]['newest_date'];
148  }
149  } else if ($this->attr('range_type') !== 'default') {
150  switch ($this->attr('range_type')) {
151  case 'today':
152  $from = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime('00:00:00 today')));
153  break;
154 
155  case 'week':
156  // If it's a Monday, then doing "next Monday, 1 week ago"
157  // will make it one week out.
158  if ((int)date('w') === 1) {
159  $from = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime('00:00:00 today')));
160  } else {
161  $from = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime('00:00:00 Monday -1 week')));
162  }
163  break;
164 
165  case 'month':
166  $from = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime(date('Y-m').'-01 00:00:00')));
167  break;
168 
169  case 'year':
170  $from = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime(date('Y').'-01-01 00:00:00')));
171  break;
172  }
173 
174  $to = new Asset_Attribute_DateTime(0, ts_iso8601(strtotime('now')));
175  } else {
176  $from = $this->getAttribute('date_range_from');
177  $to = $this->getAttribute('date_range_to');
178  }
179 
180  $from = $from->getISO8601();
181  $from_date = iso8601_date_component($from);
182  if (!empty($from_date)) {
183  // invalid FROM date?
184  if (!preg_match_all('|^\d{4}-\d{2}-\d{2}$|', $from_date, $matches)) {
185  trigger_localised_error('CMS0053', E_USER_WARNING, $from_date);
186  return Array();
187  }
188 
189  $from_time = iso8601_time_component($from);
190  if (empty($from_time)) $from_time = '00:00:00';
191 
192  // invalid FROM time?
193  if (!preg_match_all('|^\d{2}:\d{2}:\d{2}$|', $from_time, $matches)) {
194  trigger_localised_error('CMS0054', E_USER_WARNING, $from_time);
195  return Array();
196  }
197  $where .= ' AND a.'.$field.' >= '.db_extras_todate(MatrixDAL::getDbType(), ':wn_from_date', FALSE);
198  $base_bind_vars['wn_from_date'] = $from_date.' '.$from_time;
199  }
200 
201  $to = $to->getISO8601();
202  $to_date = iso8601_date_component($to);
203  if (!empty($to_date)) {
204  // invalid TO date?
205  if (!preg_match_all('|^\d{4}-\d{2}-\d{2}$|', $to_date, $matches)) {
206  trigger_localised_error('CMS0055', E_USER_WARNING, $to_date);
207  return Array();
208  }
209 
210  $to_time = iso8601_time_component($to);
211  if (empty($to_time)) $to_time = '23:59:59';
212 
213  // invalid TO time?
214  if (!preg_match_all('|^\d{2}:\d{2}:\d{2}$|', $to_time, $matches)) {
215  trigger_localised_error('CMS0056', E_USER_WARNING, $to_time);
216  return Array();
217  }
218  $where .= ' AND a.'.$field.' <= '.db_extras_todate(MatrixDAL::getDbType(), ':wn_to_date', FALSE);
219  $base_bind_vars['wn_to_date'] = $to_date.' '.$to_time;
220  }
221 
222  // if there are no date conditions - we dont display anything
223  if (empty($where)) {
224  trigger_localised_error('CMS0011', E_USER_NOTICE);
225  return Array();
226  }
227 
228  if (!is_null($link_value_wanted)) {
229  $link_value_wanted = Array(
230  'link_value' => Array($link_value_wanted),
231  'equal' => $this->attr('link_value_filter_logic'),
232  );
233  }
234 
235  if ($this->attr('subs_only')) {
236  $links = Array();
237 
238  foreach ($root_asset_ids as $root_asset_id) {
239  $links_query = $GLOBALS['SQ_SYSTEM']->am->generateGetLinksQuery($root_asset_id, $this->attr('link_types'), array_keys($wanted_types), FALSE, 'major', $link_value_wanted, NULL, NULL, $letter_sort_by, SQ_PERMISSION_READ);
240  $query = $links_query['sql_array'];
241  $bind_vars = array_merge($base_bind_vars, $links_query['bind_vars']);
242 
243  // Replace references to asset table with ones to the view
244  $where = str_replace('a.'.$field, 'l.'.$field, $where);
245  $query['where'] .= $where;
246 
247  // special case for A-Z listings because they require another field selected from the database
248  if ($group_by == 'letter') {
249  $query['select'] .= ', SUBSTR(l.'.$letter_sort_by.', 1, 1) AS first_letter';
250  }
251 
252  $new_links = NULL;
253  try {
254  $_query = MatrixDAL::preparePdoQuery(implode(' ', $query));
255  foreach($bind_vars as $bind_var => $bind_value) {
256  MatrixDAL::bindValueToPdo($_query, $bind_var, $bind_value);
257  }
258  $new_links = MatrixDAL::executePdoAll($_query);
259  } catch (Exception $e) {
260  throw new Exception($e->getMessage());
261  }
262 
263  $links = array_merge($links, $new_links);
264  }
265 
266  switch ($group_by) {
267 
268  case 'letter' :
269  foreach ($links as $link_info) {
270  $children[$link_info['minorid']] = Array( 0 => Array(
271  'type_code' => $link_info['minor_type_code'],
272  'first_letter' => $link_info['first_letter'],
273  )
274  );
275  }
276  break;
277 
278  case 'number' :
279  case 'random' :
280  foreach ($links as $link_info) {
281  $children[$link_info['minorid']] = Array( 0 => Array('type_code' => $link_info['minor_type_code']));
282  }
283  break;
284  }
285 
286  } else {
287  $children = Array();
288 
289  foreach ($root_asset_ids as $root_asset_id) {
290  $root_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($root_asset_id);
291  $child_query = $GLOBALS['SQ_SYSTEM']->am->generateGetChildrenQuery($root_asset, array_keys($wanted_types), FALSE, NULL, $letter_sort_by, SQ_PERMISSION_READ, TRUE, TRUE, NULL, NULL, TRUE, $link_value_wanted);
292  $sql_array = $child_query['sql_array'];
293  $bind_vars = array_merge($base_bind_vars, $child_query['bind_vars']);
294 
295  $sql_array['where'] .= $where;
296 
297  // special case for A-Z listings because they require another field selected from the database
298  if ($group_by == 'letter') {
299  $sql_array['select'] .= ', SUBSTR(a.'.$letter_sort_by.', 1, 1) AS first_letter';
300  $sql_array['union_select'] .= ', NULL AS first_letter';
301  }
302 
303  $new_kids = NULL;
304  try {
305  $query = MatrixDAL::preparePdoQuery(implode(' ', $sql_array));
306 
307  foreach($bind_vars as $bind_var => $bind_value) {
308  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
309  }
310 
311  $new_kids = MatrixDAL::executePdoGroupedAssoc($query);
312  } catch (Exception $e) {
313  throw new Exception($e->getMessage());
314  }
315 
316  // can't use array_merge() because we need to save the key (assetid);
317  foreach ($new_kids as $kid_key => $kid_value) {
318  $children[$kid_key] = $kid_value;
319  }
320 
321  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($root_asset);
322  unset($root_asset);
323  }
324 
325  // only A-Z listings require the type_code to be in an array
326  // all other formats require a simple assetid => type_code return value
327  if ($group_by != 'letter') {
328  foreach ($children as $child_id => $child_datas) {
329  $child_data = $child_datas[0];
330  $children[$child_id] = Array( 0 => Array('type_code' => $child_data['type_code']));
331  }
332  }
333 
334  }//end else subs_only
335 
336  if ($group_by == 'number') {
337  $children = $this->sortAssetList($children, $sort_info);
338  }
339 
340  return $children;
341 
342  }//end getAssetList()
343 
344 
351  function _getCacheKey()
352  {
353  $cache_key = parent::_getCacheKey();
354 
355  $ranges = $this->attr('date_ranges');
356  if (!empty($ranges) && isset($_REQUEST['asset_'.$this->id.'_date_range']) && isset($ranges[$_REQUEST['asset_'.$this->id.'_date_range']])) {
357  // add our date range ID to the unique cache key
358  $cache_key .= ':'.$_REQUEST['asset_'.$this->id.'_date_range'];
359  }
360 
361  return $cache_key;
362 
363  }//end _getCacheKey()
364 
365 
366 //-- KEYWORD DESCRIPTION --//
367 
368 
383  {
384  $keywords = parent::_getContentsKeywords();
385  $keywords['date_range_list'] = translate('cms_whats_new_date_range_list');
386  $keywords['date_range_list_linked'] = translate('cms_whats_new_date_range_list_linked');
387 
388  return $keywords;
389 
390  }//end _getContentsKeywords()
391 
392 
393 //-- KEYWORD REPLACEMENT --//
394 
395 
403  {
404  $ranges = $this->attr('date_ranges');
405  if (empty($ranges)) return;
406 
407  $prefix = 'asset_'.$this->id;
408 
409  foreach ($ranges as $id => $info) {
410  $names[$id] = $info['name'];
411  }
412 
413  $selected = (isset($_REQUEST[$prefix.'_date_range']) ? $_REQUEST[$prefix.'_date_range'] : '');
414  $url = $_SERVER['PHP_SELF'];
415 
416  ob_start();
417  combo_box($prefix.'_date_range', $names, FALSE, $selected);
418  echo '&nbsp;';
419  $extras = 'var select = document.getElementById("'.$prefix.'_date_range"); document.location.href="'.$url.'?'.$prefix.'_date_range=" + select.selectedIndex;';
420  normal_button($prefix.'_date_range_submit', translate('go'), $extras);
421 
422  $replacement = ob_get_contents();
423  ob_end_clean();
424 
425  return $replacement;
426 
427  }//end getDateRangeListKeywordReplacement()
428 
429 
437  {
438  $ranges = $this->attr('date_ranges');
439  if (empty($ranges)) return;
440 
441  $prefix = 'asset_'.$this->id;
442 
443  foreach ($ranges as $id => $info) {
444  $names[$id] = $info['name'];
445  }
446 
447  $selected = (isset($_REQUEST[$prefix.'_date_range']) ? $_REQUEST[$prefix.'_date_range'] : '');
448  $url = $_SERVER['PHP_SELF'];
449 
450  ob_start();
451  ?>
452  <div id="date_range_list">
453  <ul>
454  <?php
455  foreach ($names as $id => $name) {
456  ?>
457  <li<?php echo ($selected == $id) ? ' id="current"' : '';?>>
458  <a href="<?php echo $url.'?'.$prefix.'_date_range='.$id; ?>"><?php echo $name; ?></a>
459  </li>
460  <?php
461  }
462  ?>
463  </ul>
464  </div>
465  <?php
466  $replacement = ob_get_contents();
467  ob_end_clean();
468 
469  return $replacement;
470 
471  }//end getDateRangeListLinkedKeywordReplacement()
472 
473 
474 }//end class
475 
476 ?>