Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
CodelistItemDAO.inc.php
1 <?php
2 
18 import('classes.codelist.CodelistItem');
19 
20 class CodelistItemDAO extends DAO {
21 
25  function CodelistItemDAO() {
26  parent::DAO();
27  }
28 
29  function &_getCache($locale = null) {
30  if ($locale == null) {
31  $locale = AppLocale::getLocale();
32  }
33  $cacheName =& $this->getCacheName();
34 
35  $cache =& Registry::get($cacheName, true, null);
36  if ($cache === null) {
37  $cacheManager = CacheManager::getManager();
38  $cache = $cacheManager->getFileCache(
39  $this->getName() . '_codelistItems', $locale,
40  array($this, '_cacheMiss')
41  );
42  $cacheTime = $cache->getCacheTime();
43  if ($cacheTime !== null && $cacheTime < filemtime($this->getFilename($locale))) {
44  $cache->flush();
45  }
46  }
47 
48  return $cache;
49  }
50 
51  function _cacheMiss($cache, $id) {
52  $allCodelistItems =& Registry::get('all' . $this->getName() . 'CodelistItems', true, null);
53  if ($allCodelistItems === null) {
54  // Add a locale load to the debug notes.
55  $notes =& Registry::get('system.debug.notes');
56  $locale = $cache->cacheId;
57  if ($locale == null) {
58  $locale = AppLocale::getLocale();
59  }
60  $filename = $this->getFilename($locale);
61  $notes[] = array('debug.notes.codelistItemListLoad', array('filename' => $filename));
62 
63  // Reload locale registry file
64  $xmlDao = new XMLDAO();
65  $nodeName =& $this->getName(); // i.e., subject
66  $data = $xmlDao->parseStruct($filename, array($nodeName));
67 
68  // Build array with ($charKey => array(stuff))
69  if (isset($data[$nodeName])) {
70  foreach ($data[$nodeName] as $codelistData) {
71  $allCodelistItems[$codelistData['attributes']['code']] = array(
72  $codelistData['attributes']['text'],
73  );
74  }
75  }
76  if (is_array($allCodelistItems)) {
77  asort($allCodelistItems);
78  }
79  $cache->setEntireCache($allCodelistItems);
80  }
81  return null;
82  }
83 
88  function getCacheName() {
89  return $this->getName() . 'Cache';
90  }
91 
97  function getFilename($locale) {
98  assert(false);
99  }
100 
105  function getName() {
106  assert(false);
107  }
108 
113  function newDataObject() {
114  assert(false);
115  }
116 
122  function &getByCode($code) {
123  $cache =& $this->_getCache();
124  $returner =& $this->_returnFromRow($code, $cache->get($code));
125  return $returner;
126  }
127 
133  function &getCodelistItems($locale = null) {
134  $cache =& $this->_getCache($locale);
135  $returner = array();
136  foreach ($cache->getContents() as $code => $entry) {
137  $returner[] =& $this->_returnFromRow($code, $entry);
138  }
139  return $returner;
140  }
141 
147  function &getNames($locale = null) {
148  $cache =& $this->_getCache($locale);
149  $returner = array();
150  $cacheContents =& $cache->getContents();
151  if (is_array($cacheContents)) {
152  foreach ($cache->getContents() as $code => $entry) {
153  $returner[] =& $entry[0];
154  }
155  }
156  return $returner;
157  }
158 
164  function &_returnFromRow($code, &$entry) {
165  $codelistItem = $this->newDataObject();
166  $codelistItem->setCode($code);
167  $codelistItem->setText($entry[0]);
168 
169  HookRegistry::call('CodelistItemDAO::_returnFromRow', array(&$codelistItem, &$code, &$entry));
170 
171  return $codelistItem;
172  }
173 }
174 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
& getCodelistItems($locale=null)
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:19
static getLocale()
static call($hookName, $args=null)
static & get($key, $createIfEmpty=false, $createWithDefault=null)
& getNames($locale=null)
& _returnFromRow($code, &$entry)
Parent class for operations involving Codelist objects.