Open Monograph Press  3.3.0
CodelistItemDAO.inc.php
1 <?php
2 
18 import('classes.codelist.CodelistItem');
19 
20 class CodelistItemDAO extends DAO {
21 
25  function __construct() {
26  parent::__construct();
27  }
28 
34  function _getCache($locale = null) {
35  if ($locale == null) {
36  $locale = AppLocale::getLocale();
37  }
38  $cacheName = $this->getCacheName();
39 
40  $cache =& Registry::get($cacheName, true, null);
41  if ($cache === null) {
42  $cacheManager = CacheManager::getManager();
43  $cache = $cacheManager->getFileCache(
44  $this->getName() . '_codelistItems', $locale,
45  array($this, '_cacheMiss')
46  );
47  $cacheTime = $cache->getCacheTime();
48  if ($cacheTime !== null && $cacheTime < filemtime($this->getFilename($locale))) {
49  $cache->flush();
50  }
51  }
52 
53  return $cache;
54  }
55 
62  function _cacheMiss($cache, $id) {
63  $allCodelistItems =& Registry::get('all' . $this->getName() . 'CodelistItems', true, null);
64  if ($allCodelistItems === null) {
65  // Add a locale load to the debug notes.
66  $notes =& Registry::get('system.debug.notes');
67  $locale = $cache->cacheId;
68  if ($locale == null) {
69  $locale = AppLocale::getLocale();
70  }
71  $filename = $this->getFilename($locale);
72  $notes[] = array('debug.notes.codelistItemListLoad', array('filename' => $filename));
73 
74  // Reload locale registry file
75  $xmlDao = new XMLDAO();
76  $nodeName = $this->getName(); // i.e., subject
77  $data = $xmlDao->parseStruct($filename, array($nodeName));
78 
79  // Build array with ($charKey => array(stuff))
80  if (isset($data[$nodeName])) {
81  foreach ($data[$nodeName] as $codelistData) {
82  $allCodelistItems[$codelistData['attributes']['code']] = array(
83  $codelistData['attributes']['text'],
84  );
85  }
86  }
87  if (is_array($allCodelistItems)) {
88  asort($allCodelistItems);
89  }
90  $cache->setEntireCache($allCodelistItems);
91  }
92  return null;
93  }
94 
99  function getCacheName() {
100  return $this->getName() . 'Cache';
101  }
102 
108  function getFilename($locale) {
109  assert(false);
110  }
111 
116  function getName() {
117  assert(false);
118  }
119 
124  function newDataObject() {
125  assert(false);
126  }
127 
133  function getByCode($code) {
134  $cache = $this->_getCache();
135  return $this->_fromRow($code, $cache->get($code));
136  }
137 
143  function getCodelistItems($locale = null) {
144  $cache = $this->_getCache($locale);
145  $returner = array();
146  foreach ($cache->getContents() as $code => $entry) {
147  $returner[] = $this->_fromRow($code, $entry);
148  }
149  return $returner;
150  }
151 
157  function getNames($locale = null) {
158  $cache = $this->_getCache($locale);
159  $returner = array();
160  $cacheContents = $cache->getContents();
161  if (is_array($cacheContents)) {
162  foreach ($cache->getContents() as $code => $entry) {
163  $returner[] = $entry[0];
164  }
165  }
166  return $returner;
167  }
168 
175  function _fromRow($code, $entry) {
176  $codelistItem = $this->newDataObject();
177  $codelistItem->setCode($code);
178  $codelistItem->setText($entry[0]);
179 
180  HookRegistry::call('CodelistItemDAO::_fromRow', array(&$codelistItem, &$code, &$entry));
181 
182  return $codelistItem;
183  }
184 }
185 
XMLDAO
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:19
CodelistItemDAO\getByCode
getByCode($code)
Definition: CodelistItemDAO.inc.php:133
CodelistItemDAO
Parent class for operations involving Codelist objects.
Definition: CodelistItemDAO.inc.php:20
CodelistItemDAO\getNames
getNames($locale=null)
Definition: CodelistItemDAO.inc.php:157
CodelistItemDAO\_fromRow
_fromRow($code, $entry)
Definition: CodelistItemDAO.inc.php:175
CacheManager\getManager
static getManager()
Definition: CacheManager.inc.php:27
Registry\get
static & get($key, $createIfEmpty=false, $createWithDefault=null)
Definition: Registry.inc.php:35
CodelistItemDAO\newDataObject
newDataObject()
Definition: CodelistItemDAO.inc.php:124
CodelistItemDAO\_getCache
_getCache($locale=null)
Definition: CodelistItemDAO.inc.php:34
CodelistItemDAO\getFilename
getFilename($locale)
Definition: CodelistItemDAO.inc.php:108
CodelistItemDAO\getName
getName()
Definition: CodelistItemDAO.inc.php:116
CodelistItemDAO\getCodelistItems
getCodelistItems($locale=null)
Definition: CodelistItemDAO.inc.php:143
CodelistItemDAO\_cacheMiss
_cacheMiss($cache, $id)
Definition: CodelistItemDAO.inc.php:62
CodelistItemDAO\getCacheName
getCacheName()
Definition: CodelistItemDAO.inc.php:99
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
AppLocale\getLocale
static getLocale()
Definition: env1/MockAppLocale.inc.php:40
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31
CodelistItemDAO\__construct
__construct()
Definition: CodelistItemDAO.inc.php:25