PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/packages/archivist-1.2.4-pl/modCategory/b61769bf0ea8c4d02dfe15698e4397ad/0/archivist/elements/snippets/snippet.archivist.php

https://gitlab.com/haque.mdmanzurul/nga-loyaltymatters
PHP | 243 lines | 174 code | 21 blank | 48 comment | 38 complexity | 6c7fc36bb056758b40f863271509adf0 MD5 | raw file
  1. <?php
  2. /**
  3. * Archivist
  4. *
  5. * Copyright 2010-2011 by Shaun McCormick <shaun@modx.com>
  6. *
  7. * This file is part of Archivist, a simple archive navigation system for MODx
  8. * Revolution.
  9. *
  10. * Archivist is free software; you can redistribute it and/or modify it under
  11. * the terms of the GNU General Public License as published by the Free Software
  12. * Foundation; either version 2 of the License, or (at your option) any later
  13. * version.
  14. *
  15. * Archivist is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with
  21. * Archivist; if not, write to the Free Software Foundation, Inc., 59 Temple
  22. * Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * @package archivist
  25. */
  26. /**
  27. * Display an archived result filter list
  28. *
  29. * @var modX $modx
  30. * @var array $scriptProperties
  31. * @var Archivist $archivist
  32. *
  33. * @package archivist
  34. */
  35. $archivist = $modx->getService('archivist','Archivist',$modx->getOption('archivist.core_path',null,$modx->getOption('core_path').'components/archivist/').'model/archivist/',$scriptProperties);
  36. if (!($archivist instanceof Archivist)) return '';
  37. /* setup default properties */
  38. $tpl = $modx->getOption('tpl',$scriptProperties,'row');
  39. $parents = !empty($scriptProperties['parents']) ? $scriptProperties['parents'] : $modx->resource->get('id');
  40. $parents = explode(',',$parents);
  41. $target = !empty($scriptProperties['target']) ? $scriptProperties['target'] : $modx->resource->get('id');
  42. $sortBy = $modx->getOption('sortBy',$scriptProperties,'publishedon');
  43. $sortDir = $modx->getOption('sortDir',$scriptProperties,'DESC');
  44. $groupByYear = $modx->getOption('groupByYear',$scriptProperties,false);
  45. $sortYear = $modx->getOption('sortYear',$scriptProperties,'DESC');
  46. $depth = $modx->getOption('depth',$scriptProperties,10);
  47. $where = $modx->getOption('where',$scriptProperties,'');
  48. $cls = $modx->getOption('cls',$scriptProperties,'arc-row');
  49. $altCls = $modx->getOption('altCls',$scriptProperties,'arc-row-alt');
  50. $lastCls = $modx->getOption('lastCls',$scriptProperties,'');
  51. $firstCls = $modx->getOption('firstCls',$scriptProperties,'');
  52. $filterPrefix = $modx->getOption('filterPrefix',$scriptProperties,'arc_');
  53. $useMonth = $modx->getOption('useMonth',$scriptProperties,true);
  54. $useDay = $modx->getOption('useDay',$scriptProperties,false);
  55. $dateFormat = !empty($scriptProperties['dateFormat']) ? $scriptProperties['dateFormat'] : '';
  56. $limit = $modx->getOption('limit',$scriptProperties,12);
  57. $start = $modx->getOption('start',$scriptProperties,0);
  58. $hideContainers = $modx->getOption('hideContainers',$scriptProperties,true);
  59. $useFurls = $modx->getOption('useFurls',$scriptProperties,true);
  60. $persistGetParams = $modx->getOption('persistGetParams',$scriptProperties,false);
  61. /* handle existing GET params */
  62. $extraParams = $modx->getOption('extraParams',$scriptProperties,array());
  63. $extraParams = $archivist->mergeGetParams($extraParams,$persistGetParams,$filterPrefix);
  64. /* set locale for date processing */
  65. if ($modx->getOption('setLocale',$scriptProperties,true)) {
  66. $cultureKey = $modx->getOption('cultureKey',null,'en');
  67. $locale = !empty($scriptProperties['locale']) ? $scriptProperties['locale'] : $cultureKey;
  68. if (!empty($locale)) {
  69. setlocale(LC_ALL,$locale);
  70. }
  71. }
  72. /* find children of parents */
  73. $children = array();
  74. foreach ($parents as $parent) {
  75. $pchildren = $modx->getChildIds($parent, $depth);
  76. if (!empty($pchildren)) $children = array_merge($children, $pchildren);
  77. }
  78. if (!empty($children)) $parents = array_merge($parents, $children);
  79. /* get filter format */
  80. $dateEmpty = empty($dateFormat);
  81. $sqlDateFormat = '%Y';
  82. if ($dateEmpty) $dateFormat = '%Y';
  83. if ($useMonth) {
  84. if ($dateEmpty) $dateFormat = '%B '.$dateFormat;
  85. $sqlDateFormat = '%b '.$sqlDateFormat;
  86. }
  87. if ($useDay) {
  88. if ($dateEmpty) $dateFormat = '%d '.$dateFormat;
  89. $sqlDateFormat = '%d '.$sqlDateFormat;
  90. }
  91. /* build query */
  92. $c = $modx->newQuery('modResource');
  93. $fields = $modx->getSelectColumns('modResource','','',array('id',$sortBy));
  94. $c->select($fields);
  95. $c->select(array(
  96. 'FROM_UNIXTIME('.$sortBy.',"'.$sqlDateFormat.'") AS '.$modx->escape('date'),
  97. 'FROM_UNIXTIME('.$sortBy.',"'.$sqlDateFormat.'") AS '.$modx->escape('date'),
  98. 'FROM_UNIXTIME('.$sortBy.',"%D") AS '.$modx->escape('day_formatted'),
  99. 'COUNT('.$modx->escape('id').') AS '.$modx->escape('count'),
  100. ));
  101. $c->where(array(
  102. 'parent:IN' => $parents,
  103. 'published' => true,
  104. 'deleted' => false,
  105. ));
  106. /* don't grab unpublished resources */
  107. $c->where(array(
  108. 'published' => true,
  109. ));
  110. if ($hideContainers) {
  111. $c->where(array(
  112. 'isfolder' => false,
  113. ));
  114. }
  115. if (!empty($where)) {
  116. $where = $modx->fromJSON($where);
  117. $c->where($where);
  118. }
  119. $exclude = $modx->getOption('exclude',$scriptProperties,'');
  120. if (!empty($exclude)) {
  121. $c->where(array(
  122. 'id:NOT IN' => is_array($exclude) ? $exclude : explode(',',$exclude),
  123. ));
  124. }
  125. $c->sortby('FROM_UNIXTIME(`'.$sortBy.'`,"%Y") '.$sortDir.', FROM_UNIXTIME(`'.$sortBy.'`,"%m") '.$sortDir.', FROM_UNIXTIME(`'.$sortBy.'`,"%d") '.$sortDir,'');
  126. $c->groupby('FROM_UNIXTIME(`'.$sortBy.'`,"'.$sqlDateFormat.'")');
  127. /* if limiting to X records */
  128. if (!empty($limit)) { $c->limit($limit,$start); }
  129. $resources = $modx->getIterator('modResource',$c);
  130. /* iterate over resources */
  131. $output = array();
  132. $groupByYearOutput = array();
  133. $idx = 0;
  134. $count = count($resources);
  135. /** @var modResource $resource */
  136. foreach ($resources as $resource) {
  137. $resourceArray = $resource->toArray();
  138. $date = $resource->get($sortBy);
  139. $dateObj = strtotime($date);
  140. $resourceArray['date'] = strftime($dateFormat,$dateObj);
  141. $resourceArray['month_name_abbr'] = strftime('%b',$dateObj);
  142. $resourceArray['month_name'] = strftime('%B',$dateObj);
  143. $resourceArray['month'] = strftime('%m',$dateObj);
  144. $resourceArray['year'] = strftime('%Y',$dateObj);
  145. $resourceArray['year_two_digit'] = strftime('%y',$dateObj);
  146. $resourceArray['day'] = strftime('%d',$dateObj);
  147. $resourceArray['weekday'] = strftime('%A',$dateObj);
  148. $resourceArray['weekday_abbr'] = strftime('%a',$dateObj);
  149. $resourceArray['weekday_idx'] = strftime('%w',$dateObj);
  150. /* css classes */
  151. $resourceArray['cls'] = $cls;
  152. if ($idx % 2) { $resourceArray['cls'] .= ' '.$altCls; }
  153. if ($idx == 0 && !empty($firstCls)) { $resourceArray['cls'] .= ' '.$firstCls; }
  154. if ($idx+1 == $count && !empty($lastCls)) { $resourceArray['cls'] .= ' '.$lastCls; }
  155. /* setup GET params */
  156. $params = array();
  157. $params[$filterPrefix.'year'] = $resourceArray['year'];
  158. /* if using month filter */
  159. if ($useMonth) {
  160. $params[$filterPrefix.'month'] = $resourceArray['month'];
  161. }
  162. /* if using day filter (why you would ever is beyond me...) */
  163. if ($useDay) {
  164. $params[$filterPrefix.'day'] = $resourceArray['day'];
  165. if (empty($scriptProperties['dateFormat'])) {
  166. $resourceArray['date'] = $resourceArray['month_name'].' '.$resourceArray['day'].', '.$resourceArray['year'];
  167. }
  168. }
  169. if ($useFurls) {
  170. $params = implode('/',$params);
  171. if (!empty($extraParams)) $params .= '?'.$extraParams;
  172. $resourceArray['url'] = $modx->makeUrl($target).$params;
  173. } else {
  174. $params = http_build_query($params);
  175. if (!empty($extraParams)) $params .= '&'.$extraParams;
  176. $resourceArray['url'] = $modx->makeUrl($target,'',$params);
  177. }
  178. if ($groupByYear) {
  179. $groupByYearOutput[$resourceArray['year']][] = $resourceArray;
  180. } else {
  181. $output[] = $archivist->getChunk($tpl,$resourceArray);
  182. }
  183. $idx++;
  184. }
  185. if ($groupByYear) {
  186. $wrapperTpl = $modx->getOption('yearGroupTpl',$scriptProperties,'yeargroup');
  187. $wrapperRowSeparator = $modx->getOption('yearGroupRowSeparator',$scriptProperties,"\n");
  188. if (strtolower($sortYear) === 'asc') {
  189. ksort($groupByYearOutput);
  190. } else {
  191. krsort($groupByYearOutput);
  192. }
  193. foreach ($groupByYearOutput as $year => $row) {
  194. $wrapper['year'] = $year;
  195. $params = array();
  196. $params[$filterPrefix.'year'] = $year;
  197. if ($useFurls) {
  198. $params = implode('/',$params);
  199. if (!empty($extraParams)) $params .= '?'.$extraParams;
  200. $wrapper['url'] = $modx->makeUrl($target).$params;
  201. } else {
  202. $params = http_build_query($params);
  203. if (!empty($extraParams)) $params .= '&'.$extraParams;
  204. $wrapper['url'] = $modx->makeUrl($target,'',$params);
  205. }
  206. $wrapper['row'] = array();
  207. foreach ($row as $month) {
  208. $wrapper['row'][] = $archivist->getChunk($tpl,$month);
  209. }
  210. $wrapper['row'] = implode($wrapperRowSeparator,$wrapper['row']);
  211. $output[] = $archivist->getChunk($wrapperTpl,$wrapper);
  212. }
  213. }
  214. /* output or set to placeholder */
  215. $outputSeparator = $modx->getOption('outputSeparator',$scriptProperties,"\n");
  216. $output = implode($outputSeparator,$output);
  217. $toPlaceholder = $modx->getOption('toPlaceholder',$scriptProperties,false);
  218. if (!empty($toPlaceholder)) {
  219. $modx->setPlaceholder($toPlaceholder,$output);
  220. return '';
  221. }
  222. return $output;