Open Monograph Press  3.3.0
ChapterForm.inc.php
1 <?php
2 
17 import('lib.pkp.classes.form.Form');
18 
19 class ChapterForm extends Form {
22 
25 
27  var $_chapter;
28 
35  function __construct($monograph, $publication, $chapter) {
36  parent::__construct('controllers/grid/users/chapter/form/chapterForm.tpl');
37  $this->setMonograph($monograph);
38  $this->setPublication($publication);
39  $this->setDefaultFormLocale($publication->getData('locale'));
40 
41  if ($chapter) {
42  $this->setChapter($chapter);
43  }
44 
45  // Validation checks for this form
46  $this->addCheck(new FormValidatorLocale($this, 'title', 'required', 'metadata.property.validationMessage.title', $publication->getData('locale')));
47  $this->addCheck(new FormValidatorPost($this));
48  $this->addCheck(new FormValidatorCSRF($this));
49  }
50 
51 
52  //
53  // Getters/Setters
54  //
59  function getMonograph() {
60  return $this->_monograph;
61  }
62 
67  function setMonograph($monograph) {
68  $this->_monograph = $monograph;
69  }
70 
75  function getPublication() {
76  return $this->_publication;
77  }
78 
83  function setPublication($publication) {
84  $this->_publication = $publication;
85  }
86 
91  function getChapter() {
92  return $this->_chapter;
93  }
94 
99  function setChapter($chapter) {
100  $this->_chapter = $chapter;
101  }
102 
103  //
104  // Overridden template methods
105  //
110  function initData() {
111  AppLocale::requireComponents(LOCALE_COMPONENT_APP_DEFAULT, LOCALE_COMPONENT_PKP_SUBMISSION);
112 
113  $this->setData('submissionId', $this->getMonograph()->getId());
114  $this->setData('publicationId', $this->getPublication()->getId());
115  $this->setData('enableChapterPublicationDates', (bool) $this->getMonograph()->getEnableChapterPublicationDates());
116 
117  $chapter = $this->getChapter();
118  if ($chapter) {
119  $this->setData('chapterId', $chapter->getId());
120  $this->setData('title', $chapter->getTitle());
121  $this->setData('subtitle', $chapter->getSubtitle());
122  $this->setData('abstract', $chapter->getAbstract());
123  $this->setData('datePublished', $chapter->getDatePublished());
124  $this->setData('pages', $chapter->getPages());
125  } else {
126  $this->setData('title', null);
127  $this->setData('subtitle', null);
128  $this->setData('abstract', null);
129  $this->setData('datePublished', null);
130  $this->setData('pages', null);
131  }
132  }
133 
137  function fetch($request, $template = null, $display = false) {
138 
139  $chapterAuthorOptions = [];
140  $selectedChapterAuthors = [];
141  if ($this->getChapter()) {
142  $selectedChapterAuthors = DAORegistry::getDAO('ChapterAuthorDAO')->getAuthors($this->getPublication()->getId(), $this->getChapter()->getId())->toArray();
143  foreach ($selectedChapterAuthors as $selectedChapterAuthor) {
144  $chapterAuthorOptions[$selectedChapterAuthor->getId()] = $selectedChapterAuthor->getFullName();
145  }
146  }
147  $authorsIterator = Services::get('author')->getMany(['publicationIds' => $this->getPublication()->getId(), 'count' => 1000]);
148  foreach ($authorsIterator as $author) {
149  $isIncluded = false;
150  foreach ($chapterAuthorOptions as $chapterAuthorOptionId => $chapterAuthorOption) {
151  if ($chapterAuthorOptionId === $author->getId()) {
152  $isIncluded = true;
153  }
154  }
155  if (!$isIncluded) {
156  $chapterAuthorOptions[$author->getId()] = $author->getFullName();
157  }
158  }
159 
160  $templateMgr = TemplateManager::getManager($request);
161  $templateMgr->assign([
162  'chapterAuthorOptions' => $chapterAuthorOptions,
163  'selectedChapterAuthors' => array_map(function($author) { return $author->getId(); }, $selectedChapterAuthors),
164  ]);
165 
166  if ($this->getChapter()) {
167  $submissionFiles = DAORegistry::getDAO('SubmissionFileDAO')->getLatestRevisions($this->getMonograph()->getId());
168  $chapterFileOptions = [];
169  foreach ($submissionFiles as $submissionFile) {
170  if (!$submissionFile->getData('chapterId') || $submissionFile->getData('chapterId') == $this->getChapter()->getId()) {
171  $chapterFileOptions[$submissionFile->getFileId()] = $submissionFile->getLocalizedName();
172  }
173  }
174  $selectedChapterFiles = [];
175  foreach ($submissionFiles as $submissionFile) {
176  if ($submissionFile->getData('chapterId') == $this->getChapter()->getId()) {
177  $selectedChapterFiles[] = $submissionFile->getFileId();
178  }
179  }
180  $templateMgr = TemplateManager::getManager($request);
181  $templateMgr->assign([
182  'chapterFileOptions' => $chapterFileOptions,
183  'selectedChapterFiles' => $selectedChapterFiles,
184  ]);
185  }
186 
187  return parent::fetch($request, $template, $display);
188  }
189 
194  function readInputData() {
195  $this->readUserVars(array('title', 'subtitle', 'authors', 'files','abstract','datePublished','pages'));
196  }
197 
202  function execute(...$functionParams) {
203  parent::execute(...$functionParams);
204 
205  $chapterDao = DAORegistry::getDAO('ChapterDAO'); /* @var $chapterDao ChapterDAO */
206  $chapter = $this->getChapter();
207  $request = Application::get()->getRequest();
208  $isEdit = !!$chapter;
209 
210  if ($chapter) {
211  $chapter->setTitle($this->getData('title'), null); //Localized
212  $chapter->setSubtitle($this->getData('subtitle'), null); //Localized
213  $chapter->setAbstract($this->getData('abstract'), null); //Localized
214  $chapter->setDatePublished($this->getData('datePublished'));
215  $chapter->setPages($this->getData('pages'));
216  $chapterDao->updateObject($chapter);
217  } else {
218  $chapter = $chapterDao->newDataObject();
219  $chapter->setData('publicationId', $this->getPublication()->getId());
220  $chapter->setTitle($this->getData('title'), null); //Localized
221  $chapter->setSubtitle($this->getData('subtitle'), null); //Localized
222  $chapter->setAbstract($this->getData('abstract'), null); //Localized
223  $chapter->setDatePublished($this->getData('datePublished'));
224  $chapter->setPages($this->getData('pages'));
225  $chapter->setSequence(REALLY_BIG_NUMBER);
226  $chapterDao->insertChapter($chapter);
227  $chapterDao->resequenceChapters($this->getPublication()->getId());
228  }
229 
230  $this->setChapter($chapter);
231 
232  // Save the chapter author aassociations
233  DAORegistry::getDAO('ChapterAuthorDAO')->deleteChapterAuthorsByChapterId($this->getChapter()->getId());
234  foreach ((array) $this->getData('authors') as $seq => $authorId) {
235  DAORegistry::getDAO('ChapterAuthorDAO')->insertChapterAuthor($authorId, $this->getChapter()->getId(), false, $seq);
236  }
237 
238  // Save the chapter file associations
239  if ($isEdit) {
240  $selectedFiles = (array) $this->getData('files');
241  $allFiles = DAORegistry::getDAO('SubmissionFileDAO')->getLatestRevisions($this->getMonograph()->getId());
242  foreach ($allFiles as $file) {
243  $revisions = DAORegistry::getDAO('SubmissionFileDAO')->getAllRevisions($file->getId(), null, $this->getMonograph()->getId());
244  foreach ($revisions as $revision) {
245  if (in_array($revision->getFileId(), $selectedFiles)) {
246  $revision->setData('chapterId', $this->getChapter()->getId());
247  } elseif ($revision->getData('chapterId') == $chapter->getId()) {
248  $revision->setData('chapterId', null);
249  }
250  DAORegistry::getDAO('SubmissionFileDAO')->updateObject($revision);
251  }
252  }
253  }
254 
255  return true;
256  }
257 }
258 
259 
ChapterForm\execute
execute(... $functionParams)
Definition: ChapterForm.inc.php:202
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
ChapterForm
Form for adding/editing a chapter stores/retrieves from an associative array.
Definition: ChapterForm.inc.php:19
ChapterForm\initData
initData()
Definition: ChapterForm.inc.php:110
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
FormValidatorLocale
Class to represent a form validation check for localized fields.
Definition: FormValidatorLocale.inc.php:16
ChapterForm\setChapter
setChapter($chapter)
Definition: ChapterForm.inc.php:99
Form\setData
setData($key, $value=null)
Definition: Form.inc.php:229
Form\readUserVars
readUserVars($vars)
Definition: Form.inc.php:378
Form\getData
getData($key)
Definition: Form.inc.php:220
ChapterForm\setPublication
setPublication($publication)
Definition: ChapterForm.inc.php:83
FormValidatorPost
Form validation check to make sure the form is POSTed.
Definition: FormValidatorPost.inc.php:18
ChapterForm\getChapter
getChapter()
Definition: ChapterForm.inc.php:91
ChapterForm\$_monograph
$_monograph
Definition: ChapterForm.inc.php:21
Form\setDefaultFormLocale
setDefaultFormLocale($defaultLocale)
Definition: Form.inc.php:356
ChapterForm\getMonograph
getMonograph()
Definition: ChapterForm.inc.php:59
ChapterForm\fetch
fetch($request, $template=null, $display=false)
Definition: ChapterForm.inc.php:137
ChapterForm\$_publication
$_publication
Definition: ChapterForm.inc.php:24
ChapterForm\getPublication
getPublication()
Definition: ChapterForm.inc.php:75
ChapterForm\setMonograph
setMonograph($monograph)
Definition: ChapterForm.inc.php:67
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
ChapterForm\$_chapter
$_chapter
Definition: ChapterForm.inc.php:27
Form\addCheck
addCheck($formValidator)
Definition: Form.inc.php:395
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
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
ChapterForm\readInputData
readInputData()
Definition: ChapterForm.inc.php:194
ChapterForm\__construct
__construct($monograph, $publication, $chapter)
Definition: ChapterForm.inc.php:35
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49