Open Monograph Press  3.3.0
PublicationDate.inc.php
1 <?php
2 
17 class PublicationDate extends DataObject {
18 
20  var $dateFormats;
21 
25  function __construct() {
26 
27  $onixCodelistItemDao = DAORegistry::getDAO('ONIXCodelistItemDAO'); /* @var $onixCodelistItemDao ONIXCodelistItemDAO */
28  $this->dateFormats =& $onixCodelistItemDao->getCodes('List55');
29 
30  parent::__construct();
31  }
32 
37  function getPublicationFormatId() {
38  return $this->getData('publicationFormatId');
39  }
40 
45  function setPublicationFormatId($publicationFormatId) {
46  return $this->setData('publicationFormatId', $publicationFormatId);
47  }
48 
53  function setRole($role) {
54  $this->setData('role', $role);
55  }
56 
61  function getRole() {
62  return $this->getData('role');
63  }
64 
69  function setDateFormat($format) {
70  $this->setData('dateFormat', $format);
71  }
72 
77  function getDateFormat() {
78  return $this->getData('dateFormat');
79  }
80 
85  function getNameForONIXCode() {
86  $onixCodelistItemDao = DAORegistry::getDAO('ONIXCodelistItemDAO'); /* @var $onixCodelistItemDao ONIXCodelistItemDAO */
87  $codes =& $onixCodelistItemDao->getCodes('List163'); // List163 is for Publication date, Embargo date, Announcement date, etc
88  return $codes[$this->getRole()];
89  }
90 
95  function setDate($date) {
96  $this->setData('date', $date);
97  }
98 
103  function getDate() {
104  return $this->getData('date');
105  }
106 
111  function isHijriCalendar() {
112  $format = $this->dateFormats[$this->getDateFormat()];
113  if (stristr($format, '(H)')) {
114  return true;
115  } else {
116  return false;
117  }
118  }
119 
124  function isFreeText() {
125  $format = $this->dateFormats[$this->getDateFormat()];
126  if (stristr($format, 'string')) {
127  return true;
128  } else {
129  return false;
130  }
131  }
132 
139  function getReadableDates() {
140  $format = $this->dateFormats[$this->getDateFormat()];
141  $dateFormatShort = \Application::get()->getRequest()->getContext()->getLocalizedDateFormatShort();
142 
143  if ($this->isHijriCalendar()) {
144  $format = preg_replace('/\s*\(H\)/i', '', $format);
145  }
146 
147  // store the dates we parse.
148  $dates = array();
149 
150  if (!$this->isFreeText()) { // this is not a free-form code
151  // assume that the characters in the format match up with
152  // the characters in the entered date. Iterate until the end.
153 
154  $numbers = str_split($this->getDate());
155 
156  // these two should be the same length.
157  assert(count($numbers) == count(str_split($format)));
158 
159  // Some date codes have two dates (ie, a range).
160  // Split these up into both dates.
161  if (substr_count($format, 'Y') == 8) {
162  preg_match('/^(YYYY.*)(YYYY.*)$/', $format, $matches);
163  $dateFormats = array($matches[1], $matches[2]);
164  } else {
165  $dateFormats = array($format);
166  }
167 
168  foreach ($dateFormats as $format) {
169  $formatCharacters = str_split($format);
170  $previousFormatCharacter = '';
171  $thisDate = '';
172  $separator = '-';
173  $containsMonth = false;
174 
175  for ($i = 0 ; $i < count($formatCharacters) ; $i ++) {
176  switch ($formatCharacters[$i]) {
177  // if there is a Time included, change the separator.
178  // Do not include the number, add a space instead.
179  case 'T':
180  $separator = ':';
181  $thisDate .= ' ';
182  break;
183  case 'M': // falls through to default. This is just a marker.
184  $containsMonth = true;
185  default:
186  if ($i > 0 && $previousFormatCharacter != $formatCharacters[$i] && $previousFormatCharacter != 'T') {
187  $thisDate .= $separator;
188  }
189  $thisDate .= $numbers[$i];
190  break;
191  }
192 
193  $previousFormatCharacter = $formatCharacters[$i];
194  }
195 
196  // Perform date formatting here instead of in the template since
197  // testing is easier.
198  if ($containsMonth) {
199  $thisDate = strftime($dateFormatShort, strtotime($thisDate));
200  }
201 
202  $dates[] = $thisDate;
203  // remove the first date from the numbers and extract again.
204  $numbers = array_slice($numbers, count($formatCharacters));
205  }
206 
207  } else {
208  $dates[] = $this->getDate();
209  }
210  return $dates;
211  }
212 
218  function getUnixTime() {
219  $date = $this->getDate();
220  switch ($this->getDateFormat()) {
221  case '12': return strtotime($date);
222  case '05': return strtotime("$date-01-01");
223  case '01': return strtotime("$date-01");
224  case '13': // FIXME: improve resolution below day
225  case '14': // FIXME: improve resolution below day
226  case '06': // FIXME: improve resolution below day
227  case '00': return strtotime(substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6, 2));
228  }
229  return null;
230  }
231 }
232 
233 
DataObject\getData
& getData($key, $locale=null)
Definition: DataObject.inc.php:100
DataObject
Any class with an associated DAO should extend this class.
Definition: DataObject.inc.php:18
PublicationDate\setDate
setDate($date)
Definition: PublicationDate.inc.php:98
PublicationDate\__construct
__construct()
Definition: PublicationDate.inc.php:28
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PublicationDate\getReadableDates
getReadableDates()
Definition: PublicationDate.inc.php:142
PublicationDate\setPublicationFormatId
setPublicationFormatId($publicationFormatId)
Definition: PublicationDate.inc.php:48
PublicationDate\getPublicationFormatId
getPublicationFormatId()
Definition: PublicationDate.inc.php:40
PublicationDate\getRole
getRole()
Definition: PublicationDate.inc.php:64
PublicationDate\getDateFormat
getDateFormat()
Definition: PublicationDate.inc.php:80
PublicationDate\isFreeText
isFreeText()
Definition: PublicationDate.inc.php:127
PublicationDate\setDateFormat
setDateFormat($format)
Definition: PublicationDate.inc.php:72
PublicationDate\getNameForONIXCode
getNameForONIXCode()
Definition: PublicationDate.inc.php:88
PublicationDate\setRole
setRole($role)
Definition: PublicationDate.inc.php:56
PublicationDate\isHijriCalendar
isHijriCalendar()
Definition: PublicationDate.inc.php:114
PublicationDate\getDate
getDate()
Definition: PublicationDate.inc.php:106
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
PublicationDate\$dateFormats
$dateFormats
Definition: PublicationDate.inc.php:23
DataObject\setData
setData($key, $value, $locale=null)
Definition: DataObject.inc.php:132
PublicationDate\getUnixTime
getUnixTime()
Definition: PublicationDate.inc.php:221
PublicationDate
Basic class describing a publication date for a format (used on the ONIX templates for publication fo...
Definition: PublicationDate.inc.php:17