Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
SeriesForm.inc.php
1 <?php
2 
17 import('lib.pkp.classes.form.Form');
18 
19 define('THUMBNAIL_MAX_WIDTH', 106);
20 define('THUMBNAIL_MAX_HEIGHT', 100);
21 
22 class SeriesForm extends Form {
25 
27  var $_userId;
28 
31 
33  var $_sizeArray;
34 
38  function SeriesForm($seriesId = null) {
39  $this->setSeriesId($seriesId);
40 
41  $request = Application::getRequest();
42  $user = $request->getUser();
43  $this->_userId = $user->getId();
44 
45  parent::Form('controllers/grid/settings/series/form/seriesForm.tpl');
46 
47  // Validation checks for this form
48  $this->addCheck(new FormValidatorLocale($this, 'title', 'required', 'manager.setup.form.series.nameRequired'));
49  $this->addCheck(new FormValidatorPost($this));
50  }
51 
57  function initData($args, $request) {
58  $press = $request->getPress();
59 
60  $seriesDao = DAORegistry::getDAO('SeriesDAO');
61  $seriesId = $this->getSeriesId();
62  if ($seriesId) {
63  $series = $seriesDao->getById($seriesId, $press->getId());
64  }
65 
66  if (isset($series) ) {
67  $this->_data = array(
68  'seriesId' => $seriesId,
69  'title' => $series->getTitle(null),
70  'featured' => $series->getFeatured(),
71  'path' => $series->getPath(),
72  'description' => $series->getDescription(null),
73  'prefix' => $series->getPrefix(null),
74  'subtitle' => $series->getSubtitle(null),
75  'image' => $series->getImage(),
76  'restricted' => $series->getEditorRestricted(),
77  );
78  }
79  }
80 
84  function validate() {
85  if ($temporaryFileId = $this->getData('temporaryFileId')) {
86  import('lib.pkp.classes.file.TemporaryFileManager');
87  $temporaryFileManager = new TemporaryFileManager();
88  $temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO');
89  $temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $this->_userId);
90  if ( !$temporaryFile ||
91  !($this->_imageExtension = $temporaryFileManager->getImageExtension($temporaryFile->getFileType())) ||
92  !($this->_sizeArray = getimagesize($temporaryFile->getFilePath())) ||
93  $this->_sizeArray[0] <= 0 || $this->_sizeArray[1] <= 0
94  ) {
95  $this->addError('temporaryFileId', __('form.invalidImage'));
96  return false;
97  }
98  }
99  return parent::validate();
100  }
101 
107  function fetch($request) {
108  $templateMgr = TemplateManager::getManager($request);
109  $templateMgr->assign('seriesId', $this->getSeriesId());
110 
111  $press = $request->getPress();
112  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
113  $seriesEditorCount = $userGroupDao->getContextUsersCount($press->getId(), null, ROLE_ID_SUB_EDITOR);
114  $templateMgr->assign('seriesEditorCount', $seriesEditorCount);
115 
116  $categoryDao = DAORegistry::getDAO('CategoryDAO');
117  $categoryCount = $categoryDao->getCountByPressId($press->getId());
118  $templateMgr->assign('categoryCount', $categoryCount);
119  return parent::fetch($request);
120  }
121 
126  function readInputData() {
127  $this->readUserVars(array('seriesId', 'path', 'featured', 'restricted', 'title', 'description', 'seriesEditors', 'categories', 'prefix', 'subtitle', 'temporaryFileId'));
128  }
129 
135  function execute($args, $request) {
136  $seriesDao = DAORegistry::getDAO('SeriesDAO');
137  $press = $request->getPress();
138 
139  // Get or create the series object
140  if ($this->getSeriesId()) {
141  $series = $seriesDao->getById($this->getSeriesId(), $press->getId());
142  } else {
143  import('classes.press.Series');
144  $series = new Series();
145  $series->setPressId($press->getId());
146  }
147 
148  // Populate/update the series object from the form
149  $series->setPath($this->getData('path'));
150  $series->setFeatured($this->getData('featured'));
151  $series->setTitle($this->getData('title'), null); // Localized
152  $series->setDescription($this->getData('description'), null); // Localized
153  $series->setPrefix($this->getData('prefix'), null); // Localized
154  $series->setSubtitle($this->getData('subtitle'), null); // Localized
155  $series->setEditorRestricted($this->getData('restricted'));
156 
157  // Handle the image upload if there was one.
158  if ($temporaryFileId = $this->getData('temporaryFileId')) {
159  // Fetch the temporary file storing the uploaded library file
160  $temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO');
161 
162  $temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $this->_userId);
163  $temporaryFilePath = $temporaryFile->getFilePath();
164  import('lib.pkp.classes.file.ContextFileManager');
165  $pressFileManager = new ContextFileManager($press->getId());
166  $basePath = $pressFileManager->getBasePath() . '/series/';
167 
168  // Delete the old file if it exists
169  $oldSetting = $series->getImage();
170  if ($oldSetting) {
171  $pressFileManager->deleteFile($basePath . $oldSetting['thumbnailName']);
172  $pressFileManager->deleteFile($basePath . $oldSetting['name']);
173  }
174 
175  // The following variables were fetched in validation
176  assert($this->_sizeArray && $this->_imageExtension);
177 
178  // Generate the surrogate image.
179  switch ($this->_imageExtension) {
180  case '.jpg': $image = imagecreatefromjpeg($temporaryFilePath); break;
181  case '.png': $image = imagecreatefrompng($temporaryFilePath); break;
182  case '.gif': $image = imagecreatefromgif($temporaryFilePath); break;
183  }
184  assert($image);
185 
186  $thumbnailFilename = $series->getId() . '-series-thumbnail' . $this->_imageExtension;
187  $xRatio = min(1, THUMBNAIL_MAX_WIDTH / $this->_sizeArray[0]);
188  $yRatio = min(1, THUMBNAIL_MAX_HEIGHT / $this->_sizeArray[1]);
189 
190  $ratio = min($xRatio, $yRatio);
191 
192  $thumbnailWidth = round($ratio * $this->_sizeArray[0]);
193  $thumbnailHeight = round($ratio * $this->_sizeArray[1]);
194  $thumbnail = imagecreatetruecolor(THUMBNAIL_MAX_WIDTH, THUMBNAIL_MAX_HEIGHT);
195  $whiteColor = imagecolorallocate($thumbnail, 255, 255, 255);
196  imagefill($thumbnail, 0, 0, $whiteColor);
197  imagecopyresampled($thumbnail, $image, (THUMBNAIL_MAX_WIDTH - $thumbnailWidth)/2, (THUMBNAIL_MAX_HEIGHT - $thumbnailHeight)/2, 0, 0, $thumbnailWidth, $thumbnailHeight, $this->_sizeArray[0], $this->_sizeArray[1]);
198 
199  // Copy the new file over
200  $filename = $series->getId() . '-series' . $this->_imageExtension;
201  $pressFileManager->copyFile($temporaryFile->getFilePath(), $basePath . $filename);
202 
203  switch ($this->_imageExtension) {
204  case '.jpg': imagejpeg($thumbnail, $basePath . $thumbnailFilename); break;
205  case '.png': imagepng($thumbnail, $basePath . $thumbnailFilename); break;
206  case '.gif': imagegif($thumbnail, $basePath . $thumbnailFilename); break;
207  }
208  imagedestroy($thumbnail);
209  imagedestroy($image);
210 
211  $series->setImage(array(
212  'name' => $filename,
213  'width' => $this->_sizeArray[0],
214  'height' => $this->_sizeArray[1],
215  'thumbnailName' => $thumbnailFilename,
216  'thumbnailWidth' => THUMBNAIL_MAX_WIDTH,
217  'thumbnailHeight' => THUMBNAIL_MAX_HEIGHT,
218  'uploadName' => $temporaryFile->getOriginalFileName(),
219  'dateUploaded' => Core::getCurrentDate(),
220  ));
221 
222  // Clean up the temporary file
223  import('lib.pkp.classes.file.TemporaryFileManager');
224  $temporaryFileManager = new TemporaryFileManager();
225  $temporaryFileManager->deleteFile($temporaryFileId, $this->_userId);
226  }
227 
228  // Insert or update the series in the DB
229  if ($this->getSeriesId()) {
230  $seriesDao->updateObject($series);
231  } else {
232  $this->setSeriesId($seriesDao->insertObject($series));
233  }
234 
235  import('lib.pkp.classes.controllers.listbuilder.ListbuilderHandler');
236  // Save the series editor associations.
238  $request,
239  $this->getData('seriesEditors'),
240  array(&$this, 'deleteSeriesEditorEntry'),
241  array(&$this, 'insertSeriesEditorEntry'),
242  array(&$this, 'updateSeriesEditorEntry')
243  );
244 
245  // Save the category associations.
247  $request,
248  $this->getData('categories'),
249  array(&$this, 'deleteCategoryEntry'),
250  array(&$this, 'insertCategoryEntry'),
251  array(&$this, 'updateCategoryEntry')
252  );
253 
254  return true;
255  }
256 
261  function getSeriesId() {
262  return $this->_seriesId;
263  }
264 
269  function setSeriesId($seriesId) {
270  $this->_seriesId = $seriesId;
271  }
272 
277  function insertSeriesEditorEntry($request, $newRowId) {
278  $press = $request->getPress();
279  $seriesId = $this->getSeriesId();
280  $userId = array_shift($newRowId);
281 
282  $seriesEditorsDao = DAORegistry::getDAO('SeriesEditorsDAO');
283 
284  // Make sure the membership doesn't already exist
285  if ($seriesEditorsDao->editorExists($press->getId(), $this->getSeriesId(), $userId)) {
286  return false;
287  }
288 
289  // Otherwise, insert the row.
290  $seriesEditorsDao->insertEditor($press->getId(), $this->getSeriesId(), $userId, true, true);
291  return true;
292  }
293 
300  function deleteSeriesEditorEntry($request, $rowId) {
301  $seriesEditorsDao = DAORegistry::getDAO('SeriesEditorsDAO');
302  $press = $request->getPress();
303 
304  $seriesEditorsDao->deleteEditor($press->getId(), $this->getSeriesId(), $rowId);
305  return true;
306  }
307 
315  function updateSeriesEditorEntry($request, $rowId, $newRowId) {
316  $this->deleteSeriesEditorEntry($request, $rowId);
317  $this->insertSeriesEditorEntry($request, $newRowId);
318  return true;
319  }
320 
325  function insertCategoryEntry($request, $newRowId) {
326  $press = $request->getPress();
327  $seriesId = $this->getSeriesId();
328  $categoryId = array_shift($newRowId);
329 
330  $seriesDao = DAORegistry::getDAO('SeriesDAO');
331 
332  // Make sure the membership doesn't already exist
333  if ($seriesDao->categoryAssociationExists($this->getSeriesId(), $categoryId)) {
334  return false;
335  }
336 
337  // Otherwise, insert the row.
338  $seriesDao->addCategory($this->getSeriesId(), $categoryId);
339  return true;
340  }
341 
348  function deleteCategoryEntry($request, $rowId) {
349  $seriesDao = DAORegistry::getDAO('SeriesDAO');
350  $press = $request->getPress();
351 
352  $seriesDao->removeCategory($this->getSeriesId(), $rowId);
353  return true;
354  }
355 
363  function updateCategoryEntry($request, $rowId, $newRowId) {
364  $this->deleteCategoryEntry($request, $rowId);
365  $this->insertCategoryEntry($request, $newRowId);
366  return true;
367  }
368 }
369 
370 ?>
static & getDAO($name, $dbconn=null)
addCheck($formValidator)
Definition: Form.inc.php:396
execute($args, $request)
Form validation check to make sure the form is POSTed.
Class defining operations for private context file management.
insertSeriesEditorEntry($request, $newRowId)
readUserVars($vars)
Definition: Form.inc.php:364
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
Describes basic series properties.
Definition: Series.inc.php:19
initData()
Definition: Form.inc.php:227
addError($field, $message)
Definition: Form.inc.php:405
fetch($request)
getData($key)
Definition: Form.inc.php:210
updateSeriesEditorEntry($request, $rowId, $newRowId)
Form for adding/edditing a series stores/retrieves from an associative array.
Class to represent a form validation check for localized fields.
deleteCategoryEntry($request, $rowId)
setSeriesId($seriesId)
insertCategoryEntry($request, $newRowId)
SeriesForm($seriesId=null)
updateCategoryEntry($request, $rowId, $newRowId)
deleteSeriesEditorEntry($request, $rowId)
unpack($request, $data, $deletionCallback=null, $insertionCallback=null, $updateCallback=null)
static getCurrentDate($ts=null)
Definition: Core.inc.php:95