Open Journal Systems  3.3.0
IssueGalleyForm.inc.php
1 <?php
2 
17 import('lib.pkp.classes.form.Form');
18 
19 class IssueGalleyForm extends Form {
21  var $_issue = null;
22 
24  var $_issueGalley = null;
25 
31  function __construct($request, $issue, $issueGalley = null) {
32  parent::__construct('controllers/grid/issueGalleys/form/issueGalleyForm.tpl');
33  $this->_issue = $issue;
34  $this->_issueGalley = $issueGalley;
35 
36  AppLocale::requireComponents(LOCALE_COMPONENT_APP_EDITOR, LOCALE_COMPONENT_PKP_SUBMISSION);
37 
38  $this->addCheck(new FormValidator($this, 'label', 'required', 'editor.issues.galleyLabelRequired'));
39  $this->addCheck(new FormValidatorRegExp($this, 'urlPath', 'optional', 'validator.alpha_dash', '/^[-_a-z0-9]*$/'));
40  $this->addCheck(new FormValidatorPost($this));
41  $this->addCheck(new FormValidatorCSRF($this));
42 
43  // Ensure a locale is provided and valid
44  $journal = $request->getJournal();
45  $this->addCheck(new FormValidatorCustom(
46  $this, 'galleyLocale', 'required', 'editor.issues.galleyLocaleRequired',
47  function($galleyLocale) use ($journal) {
48  return in_array($galleyLocale, $journal->getSupportedFormLocales());
49  }
50  ));
51 
52  if (!$issueGalley) {
53  // A file must be uploaded with a newly-created issue galley.
54  $this->addCheck(new FormValidator($this, 'temporaryFileId', 'required', 'form.fileRequired'));
55  }
56  }
57 
61  function fetch($request, $template = null, $display = false) {
62  $journal = $request->getJournal();
63  $templateMgr = TemplateManager::getManager($request);
64 
65  $templateMgr->assign(array(
66  'issueId' => $this->_issue->getId(),
67  'supportedLocales' => $journal->getSupportedLocaleNames(),
68  'enablePublisherId' => in_array('issueGalley', (array) $request->getContext()->getData('enablePublisherId')),
69  ));
70  if ($this->_issueGalley) $templateMgr->assign(array(
71  'issueGalleyId' => $this->_issueGalley->getId(),
72  'issueGalley' => $this->_issueGalley,
73  ));
74 
75  return parent::fetch($request, $template, $display);
76  }
77 
81  function validate($callHooks = true) {
82  // Check if public galley ID is already being used
83  $request = Application::get()->getRequest();
84  $journal = $request->getJournal();
85  $journalDao = DAORegistry::getDAO('JournalDAO'); /* @var $journalDao JournalDAO */
86 
87  $publicGalleyId = $this->getData('publicGalleyId');
88  if ($publicGalleyId) {
89  if (ctype_digit((string) $publicGalleyId)) {
90  $this->addError('publicGalleyId', __('editor.publicIdentificationNumericNotAllowed', array('publicIdentifier' => $publicGalleyId)));
91  $this->addErrorField('publicGalleyId');
92  } elseif ($journalDao->anyPubIdExists($journal->getId(), 'publisher-id', $publicGalleyId, ASSOC_TYPE_ISSUE_GALLEY, $this->_issueGalley?$this->_issueGalley->getId():null, true)) {
93  $this->addError('publicGalleyId', __('editor.publicIdentificationExistsForTheSameType', array('publicIdentifier' => $publicGalleyId)));
94  $this->addErrorField('publicGalleyId');
95  }
96  }
97 
98  if ($this->getData('urlPath')) {
99  if (ctype_digit((string) $this->getData('urlPath'))) {
100  $this->addError('urlPath', __('publication.urlPath.numberInvalid'));
101  $this->addErrorField('urlPath');
102  } else {
103  $issueGalleyDao = DAORegistry::getDAO('IssueGalleyDAO'); /* @var $issueGalleyDao IssueGalleyDAO */
104  $issueGalley = $issueGalleyDao->getByBestId($this->getData('urlPath'), $this->_issue->getId());
105  if ($issueGalley &&
106  (!$this->_issueGalley || $this->_issueGalley->getId() !== $issueGalley->getId())
107  ) {
108  $this->addError('urlPath', __('publication.urlPath.duplicate'));
109  $this->addErrorField('urlPath');
110  }
111  }
112  }
113 
114  return parent::validate($callHooks);
115  }
116 
120  function initData() {
121  if ($this->_issueGalley) {
122  $this->_data = array(
123  'label' => $this->_issueGalley->getLabel(),
124  'publicGalleyId' => $this->_issueGalley->getStoredPubId('publisher-id'),
125  'galleyLocale' => $this->_issueGalley->getLocale(),
126  'urlPath' => $this->_issueGalley->getData('urlPath'),
127  );
128  } else {
129  $this->_data = array();
130  }
131  }
132 
136  function readInputData() {
137  $this->readUserVars(
138  array(
139  'label',
140  'publicGalleyId',
141  'galleyLocale',
142  'temporaryFileId',
143  'urlPath',
144  )
145  );
146  }
147 
151  function execute(...$functionArgs) {
152  import('classes.file.IssueFileManager');
153  $issueFileManager = new IssueFileManager($this->_issue->getId());
154 
155  $request = Application::get()->getRequest();
156  $journal = $request->getJournal();
157  $user = $request->getUser();
158 
159  $issueGalley = $this->_issueGalley;
160  $issueGalleyDao = DAORegistry::getDAO('IssueGalleyDAO'); /* @var $issueGalleyDao IssueGalleyDAO */
161 
162  // If a temporary file ID was specified (i.e. an upload occurred), get the file for later.
163  $temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO'); /* @var $temporaryFileDao TemporaryFileDAO */
164  $temporaryFile = $temporaryFileDao->getTemporaryFile($this->getData('temporaryFileId'), $user->getId());
165 
166  parent::execute(...$functionArgs);
167 
168  if ($issueGalley) {
169  // Update an existing galley
170  if ($temporaryFile) {
171  // Galley has a file, delete it before uploading new one
172  if ($issueGalley->getFileId()) {
173  $issueFileManager->deleteById($issueGalley->getFileId());
174  }
175  // Upload new file
176  $issueFile = $issueFileManager->fromTemporaryFile($temporaryFile);
177  $issueGalley->setFileId($issueFile->getId());
178  }
179 
180  $issueGalley->setLabel($this->getData('label'));
181  $issueGalley->setStoredPubId('publisher-id', $this->getData('publicGalleyId'));
182  $issueGalley->setLocale($this->getData('galleyLocale'));
183  $issueGalley->setData('urlPath', $this->getData('urlPath'));
184 
185  // Update galley in the db
186  $issueGalleyDao->updateObject($issueGalley);
187  } else {
188  // Create a new galley
189  $issueGalleyFile = $issueFileManager->fromTemporaryFile($temporaryFile);
190 
191  $issueGalley = $issueGalleyDao->newDataObject();
192  $issueGalley->setIssueId($this->_issue->getId());
193  $issueGalley->setFileId($issueGalleyFile->getId());
194  $issueGalley->setData('urlPath', $this->getData('urlPath'));
195 
196  if ($this->getData('label') == null) {
197  // Generate initial label based on file type
198  if (isset($fileType)) {
199  if(strstr($fileType, 'pdf')) {
200  $issueGalley->setLabel('PDF');
201  } else if (strstr($fileType, 'postscript')) {
202  $issueGalley->setLabel('PostScript');
203  } else if (strstr($fileType, 'xml')) {
204  $issueGalley->setLabel('XML');
205  }
206  }
207 
208  if ($issueGalley->getLabel() == null) {
209  $issueGalley->setLabel(__('common.untitled'));
210  }
211 
212  } else {
213  $issueGalley->setLabel($this->getData('label'));
214  }
215  $issueGalley->setLocale($this->getData('galleyLocale'));
216 
217  $issueGalley->setStoredPubId('publisher-id', $this->getData('publicGalleyId'));
218 
219  // Insert new galley into the db
220  $issueGalleyDao->insertObject($issueGalley);
221  $this->_issueGalley = $issueGalley;
222  }
223 
224  return $this->_issueGalley->getId();
225  }
226 }
227 
IssueGalleyForm\validate
validate($callHooks=true)
Definition: IssueGalleyForm.inc.php:87
IssueGalleyForm\initData
initData()
Definition: IssueGalleyForm.inc.php:126
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
IssueFileManager
Class defining operations for issue file management.
Definition: IssueFileManager.inc.php:22
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
IssueGalleyForm\__construct
__construct($request, $issue, $issueGalley=null)
Definition: IssueGalleyForm.inc.php:37
Form\readUserVars
readUserVars($vars)
Definition: Form.inc.php:378
Form\getData
getData($key)
Definition: Form.inc.php:220
FormValidatorPost
Form validation check to make sure the form is POSTed.
Definition: FormValidatorPost.inc.php:18
Form\addError
addError($field, $message)
Definition: Form.inc.php:404
IssueGalleyForm\fetch
fetch($request, $template=null, $display=false)
Definition: IssueGalleyForm.inc.php:67
IssueGalleyForm\$_issueGalley
$_issueGalley
Definition: IssueGalleyForm.inc.php:30
IssueGalleyForm\execute
execute(... $functionArgs)
Definition: IssueGalleyForm.inc.php:157
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
FormValidator
Class to represent a form validation check.
Definition: FormValidator.inc.php:23
FormValidatorRegExp
Form validation check using a regular expression.
Definition: FormValidatorRegExp.inc.php:18
Form\addCheck
addCheck($formValidator)
Definition: Form.inc.php:395
IssueGalleyForm\$_issue
$_issue
Definition: IssueGalleyForm.inc.php:24
FormValidatorCSRF
Form validation check to make sure the CSRF token is correct.
Definition: FormValidatorCSRF.inc.php:18
Form
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
IssueGalleyForm\readInputData
readInputData()
Definition: IssueGalleyForm.inc.php:142
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
FormValidatorCustom
Form validation check with a custom user function performing the validation check.
Definition: FormValidatorCustom.inc.php:18
Form\addErrorField
addErrorField($field)
Definition: Form.inc.php:412
IssueGalleyForm
Issue galley editing form.
Definition: IssueGalleyForm.inc.php:19