00001 <?php
00002
00015 import('lib.pkp.classes.form.Form');
00016
00017 class PublicationDateForm extends Form {
00019 var $_monograph;
00020
00022 var $_publicationDate;
00023
00027 function PublicationDateForm($monograph, $publicationDate) {
00028 parent::Form('controllers/grid/catalogEntry/form/pubDateForm.tpl');
00029 $this->setMonograph($monograph);
00030 $this->setPublicationDate($publicationDate);
00031
00032
00033 $this->addCheck(new FormValidator($this, 'role', 'required', 'grid.catalogEntry.roleRequired'));
00034 $this->addCheck(new FormValidator($this, 'dateFormat', 'required', 'grid.catalogEntry.dateFormatRequired'));
00035
00036 $this->addCheck(new FormValidatorCustom(
00037 $this, 'date', 'required', 'grid.catalogEntry.dateRequired',
00038 create_function(
00039 '$date, $form, $onixCodelistItemDao',
00040 '$dateFormat = $form->getData(\'dateFormat\');
00041 if (!$dateFormat) return false;
00042 $dateFormats =& $onixCodelistItemDao->getCodes(\'List55\');
00043 $format = $dateFormats[$dateFormat];
00044 if (stristr($format, \'string\') && $date != \'\') return true;
00045 $format = preg_replace(\'/\s*\(H\)/i\', \'\', $format);
00046 if (count(str_split($date)) == count(str_split($format))) return true;
00047 return false;'), array(&$this, DAORegistry::getDAO('ONIXCodelistItemDAO')
00048 )
00049 ));
00050
00051 $this->addCheck(new FormValidator($this, 'publicationFormatId', 'required', 'grid.catalogEntry.publicationFormatRequired'));
00052 $this->addCheck(new FormValidatorPost($this));
00053 }
00054
00055
00056
00057
00062 function &getPublicationDate() {
00063 return $this->_publicationDate;
00064 }
00065
00070 function setPublicationDate($publicationDate) {
00071 $this->_publicationDate =& $publicationDate;
00072 }
00073
00078 function &getMonograph() {
00079 return $this->_monograph;
00080 }
00081
00086 function setMonograph($monograph) {
00087 $this->_monograph =& $monograph;
00088 }
00089
00090
00091
00092
00093
00097 function initData() {
00098 $date =& $this->getPublicationDate();
00099
00100 if ($date) {
00101 $this->_data = array(
00102 'publicationDateId' => $date->getId(),
00103 'role' => $date->getRole(),
00104 'dateFormat' => $date->getDateFormat(),
00105 'date' => $date->getDate(),
00106 );
00107 }
00108 }
00109
00114 function fetch(&$request) {
00115
00116 $templateMgr =& TemplateManager::getManager();
00117 $publicationFormatId = null;
00118
00119 $monograph =& $this->getMonograph();
00120 $templateMgr->assign('monographId', $monograph->getId());
00121 $publicationDate =& $this->getPublicationDate();
00122
00123 if ($publicationDate) {
00124 $publicationFormatId = $publicationDate->getPublicationFormatId();
00125 $templateMgr->assign('publicationDateId', $publicationDate->getId());
00126 $templateMgr->assign('role', $publicationDate->getRole());
00127 $templateMgr->assign('dateFormat', $publicationDate->getDateFormat());
00128 $templateMgr->assign('date', $publicationDate->getDate());
00129 $publicationFormatId = $publicationDate->getPublicationFormatId();
00130 } else {
00131 $publicationFormatId = (int) $request->getUserVar('publicationFormatId');
00132 $templateMgr->assign('dateFormat', '20');
00133 }
00134
00135 $publicationFormatDao =& DAORegistry::getDAO('PublicationFormatDAO');
00136 $publicationFormat =& $publicationFormatDao->getById($publicationFormatId, $monograph->getId());
00137
00138 if ($publicationFormat) {
00139 $templateMgr->assign('publicationFormatId', $publicationFormatId);
00140 $publicationDates = $publicationFormat->getPublicationDates();
00141 $assignedRoles = array_keys($publicationDates->toAssociativeArray('role'));
00142 if ($publicationDate) $assignedRoles = array_diff($assignedRoles, array($publicationDate->getRole()));
00143 $onixCodelistItemDao =& DAORegistry::getDAO('ONIXCodelistItemDAO');
00144 $roles =& $onixCodelistItemDao->getCodes('List163', $assignedRoles);
00145 $templateMgr->assign_by_ref('publicationDateRoles', $roles);
00146
00147
00148 $dateFormats =& $onixCodelistItemDao->getCodes('List55');
00149 $templateMgr->assign_by_ref('publicationDateFormats', $dateFormats);
00150 } else {
00151 fatalError('Format not in authorized monograph');
00152 }
00153
00154 return parent::fetch($request);
00155 }
00156
00161 function readInputData() {
00162 $this->readUserVars(array(
00163 'publicationDateId',
00164 'publicationFormatId',
00165 'role',
00166 'dateFormat',
00167 'date',
00168 ));
00169 }
00170
00175 function execute() {
00176 $publicationDateDao =& DAORegistry::getDAO('PublicationDateDAO');
00177 $publicationFormatDao =& DAORegistry::getDAO('PublicationFormatDAO');
00178
00179 $monograph = $this->getMonograph();
00180 $publicationDate =& $this->getPublicationDate();
00181 $publicationFormat =& $publicationFormatDao->getById($this->getData('publicationFormatId'), $monograph->getId());
00182
00183 if (!$publicationDate) {
00184
00185 $publicationDate = $publicationDateDao->newDataObject();
00186 if ($publicationFormat != null) {
00187 $publicationDate->setPublicationFormatId($publicationFormat->getId());
00188 $existingFormat = false;
00189 } else {
00190 fatalError('This assigned format not in authorized monograph context!');
00191 }
00192 } else {
00193 $existingFormat = true;
00194 if ($publicationFormat->getId() !== $publicationDate->getPublicationFormatId()) fatalError('Invalid format!');
00195 }
00196
00197 $publicationDate->setRole($this->getData('role'));
00198 $publicationDate->setDateFormat($this->getData('dateFormat'));
00199 $publicationDate->setDate($this->getData('date'));
00200
00201 if ($existingFormat) {
00202 $publicationDateDao->updateObject($publicationDate);
00203 $publicationDateId = $publicationDate->getId();
00204 } else {
00205 $publicationDateId = $publicationDateDao->insertObject($publicationDate);
00206 }
00207
00208 return $publicationDateId;
00209 }
00210 }
00211
00212 ?>