/core/model/modx/processors/workspace/lexicon/getlist.php

https://github.com/esche/revolution · PHP · 108 lines · 73 code · 13 blank · 22 comment · 10 complexity · 2a1031b48765b0a9b88bcedb22021503 MD5 · raw file

  1. <?php
  2. /**
  3. * Gets a list of lexicon entries
  4. *
  5. * @param string $namespace (optional) If set, will filter by namespace.
  6. * Defaults to core.
  7. * @param integer $topic (optional) If set, will filter by this topic
  8. * @param string $language (optional) If set, will filter by language. Defaults
  9. * to en.
  10. * @param integer $start (optional) The record to start at. Defaults to 0.
  11. * @param integer $limit (optional) The number of records to limit to. Defaults
  12. * to 10.
  13. *
  14. * @package modx
  15. * @subpackage processors.workspace.lexicon
  16. */
  17. if (!$modx->hasPermission('lexicons')) return $modx->error->failure($modx->lexicon('permission_denied'));
  18. $modx->lexicon->load('lexicon');
  19. /* setup default properties */
  20. $isLimit = !empty($scriptProperties['limit']);
  21. $start = $modx->getOption('start',$scriptProperties,0);
  22. $limit = $modx->getOption('limit',$scriptProperties,10);
  23. $sort = $modx->getOption('sort',$scriptProperties,'name');
  24. $dir = $modx->getOption('dir',$scriptProperties,'ASC');
  25. $language = !empty($scriptProperties['language']) ? $scriptProperties['language'] : 'en';
  26. $namespace = !empty($scriptProperties['namespace']) ? $scriptProperties['namespace'] : 'core';
  27. $topic = !empty($scriptProperties['topic']) ? $scriptProperties['topic'] : 'default';
  28. $where = array(
  29. 'namespace' => $namespace,
  30. 'topic' => $topic,
  31. 'language' => $language,
  32. );
  33. /* setup query for db based lexicons */
  34. $c = $modx->newQuery('modLexiconEntry');
  35. $c->where($where);
  36. $c->sortby('name','ASC');
  37. $results = $modx->getCollection('modLexiconEntry',$c);
  38. $dbEntries = array();
  39. foreach ($results as $r) {
  40. $dbEntries[$r->get('name')] = $r->toArray();
  41. }
  42. /* first get file-based lexicon */
  43. $entries = $modx->lexicon->getFileTopic($language,$namespace,$topic);
  44. /* if searching */
  45. if (!empty($scriptProperties['search'])) {
  46. function parseArray($needle,array $haystack = array()) {
  47. if (!is_array($haystack)) return false;
  48. $results = array();
  49. foreach($haystack as $key=>$value) {
  50. if (strpos($key, $needle)!==false || strpos($value,$needle) !== false) {
  51. $results[$key] = $value;
  52. }
  53. }
  54. return $results;
  55. }
  56. $entries = parseArray($scriptProperties['search'],$entries);
  57. $where[] = array(
  58. 'name:LIKE' => '%'.$scriptProperties['search'].'%',
  59. 'OR:value:LIKE' => '%'.$scriptProperties['search'].'%',
  60. );
  61. }
  62. $count = count($entries);
  63. /* add in unique entries */
  64. $es = array_diff_assoc($dbEntries,$entries);
  65. foreach ($es as $n => $entryArray) {
  66. $entries[$n] = $entryArray['value'];
  67. }
  68. ksort($entries);
  69. $entries = array_slice($entries,$start,$limit,true);
  70. /* loop through */
  71. $list = array();
  72. foreach ($entries as $name => $value) {
  73. $entryArray = array(
  74. 'name' => $name,
  75. 'value' => $value,
  76. 'namespace' => $namespace,
  77. 'topic' => $topic,
  78. 'language' => $language,
  79. 'createdon' => null,
  80. 'editedon' => null,
  81. 'overridden' => 0,
  82. );
  83. /* if override in db, load */
  84. if (array_key_exists($name,$dbEntries)) {
  85. $entryArray = array_merge($entryArray,$dbEntries[$name]);
  86. $entryArray['editedon'] = $entryArray['editedon'] == '0000-00-00 00:00:00'
  87. || $entryArray['editedon'] == '-001-11-30 00:00:00'
  88. || empty($entryArray['editedon'])
  89. ? ''
  90. : strftime('%b %d, %Y %I:%M %p',strtotime($entryArray['editedon']));
  91. $entryArray['overridden'] = 1;
  92. }
  93. $list[] = $entryArray;
  94. }
  95. return $this->outputArray($list,$count);