Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
CategoryForm.inc.php
1 <?php
2 
16 import('lib.pkp.classes.form.Form');
17 
18 define('THUMBNAIL_MAX_WIDTH', 106);
19 define('THUMBNAIL_MAX_HEIGHT', 100);
20 
21 class CategoryForm extends Form {
23  var $_categoryId;
24 
26  var $_pressId;
27 
29  var $_userId;
30 
33 
35  var $_sizeArray;
36 
37 
43  function CategoryForm($pressId, $categoryId = null) {
44  parent::Form('controllers/grid/settings/category/form/categoryForm.tpl');
45  $this->_pressId = $pressId;
46  $this->_categoryId = $categoryId;
47 
48  $request = Application::getRequest();
49  $user = $request->getUser();
50  $this->_userId = $user->getId();
51 
52  // Validation checks for this form
53  $this->addCheck(new FormValidatorLocale($this, 'name', 'required', 'grid.category.nameRequired'));
54  $this->addCheck(new FormValidatorAlphaNum($this, 'path', 'required', 'grid.category.pathAlphaNumeric'));
55  $this->addCheck(new FormValidatorCustom(
56  $this, 'path', 'required', 'grid.category.pathExists',
57  create_function(
58  '$path,$form,$categoryDao,$pressId',
59  'return !$categoryDao->categoryExistsByPath($path,$pressId) || ($form->getData(\'oldPath\') != null && $form->getData(\'oldPath\') == $path);'
60  ),
61  array(&$this, DAORegistry::getDAO('CategoryDAO'), $pressId)
62  ));
63  $this->addCheck(new FormValidatorPost($this));
64  }
65 
66  //
67  // Getters and Setters
68  //
73  function getCategoryId() {
74  return $this->_categoryId;
75  }
76 
81  function getPressId() {
82  return $this->_pressId;
83  }
84 
85  //
86  // Implement template methods from Form.
87  //
91  function getLocaleFieldNames() {
92  $categoryDao = DAORegistry::getDAO('CategoryDAO');
93  return $categoryDao->getLocaleFieldNames();
94  }
95 
99  function initData() {
100  $categoryDao = DAORegistry::getDAO('CategoryDAO');
101  $category = $categoryDao->getById($this->getCategoryId(), $this->getPressId());
102 
103  if ($category) {
104  $this->setData('name', $category->getTitle(null)); // Localized
105  $this->setData('description', $category->getDescription(null)); // Localized
106  $this->setData('parentId', $category->getParentId());
107  $this->setData('path', $category->getPath());
108  $this->setData('image', $category->getImage());
109  }
110  }
111 
115  function validate() {
116  if ($temporaryFileId = $this->getData('temporaryFileId')) {
117  import('lib.pkp.classes.file.TemporaryFileManager');
118  $temporaryFileManager = new TemporaryFileManager();
119  $temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO');
120  $temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $this->_userId);
121  if ( !$temporaryFile ||
122  !($this->_imageExtension = $temporaryFileManager->getImageExtension($temporaryFile->getFileType())) ||
123  !($this->_sizeArray = getimagesize($temporaryFile->getFilePath())) ||
124  $this->_sizeArray[0] <= 0 || $this->_sizeArray[1] <= 0
125  ) {
126  $this->addError('temporaryFileId', __('form.invalidImage'));
127  return false;
128  }
129  }
130  return parent::validate();
131  }
132 
136  function readInputData() {
137  $this->readUserVars(array('name', 'parentId', 'path', 'description', 'temporaryFileId'));
138 
139  // For path duplicate checking; excuse the current path.
140  if ($categoryId = $this->getCategoryId()) {
141  $categoryDao = DAORegistry::getDAO('CategoryDAO');
142  $category = $categoryDao->getById($categoryId, $this->getPressId());
143  $this->setData('oldPath', $category->getPath());
144  }
145  }
146 
150  function fetch($request) {
151  $categoryDao = DAORegistry::getDAO('CategoryDAO');
152  $press = $request->getPress();
153  $templateMgr = TemplateManager::getManager($request);
154  $templateMgr->assign('categoryId', $this->getCategoryId());
155 
156  // Provide a list of root categories to the template
157  $rootCategoriesIterator = $categoryDao->getByParentId(0, $press->getId());
158  $rootCategories = array(0 => __('common.none'));
159  while ($category = $rootCategoriesIterator->next()) {
160  $categoryId = $category->getId();
161  if ($categoryId != $this->getCategoryId()) {
162  // Don't permit time travel paradox
163  $rootCategories[$categoryId] = $category->getLocalizedTitle();
164  }
165  }
166  $templateMgr->assign('rootCategories', $rootCategories);
167 
168  // Determine if this category has children of its own;
169  // if so, prevent the user from giving it a parent.
170  // (Forced two-level maximum tree depth.)
171  if ($this->getCategoryId()) {
172  $children = $categoryDao->getByParentId($this->getCategoryId(), $press->getId());
173  if ($children->next()) {
174  $templateMgr->assign('cannotSelectChild', true);
175  }
176  }
177 
178  return parent::fetch($request);
179  }
180 
184  function execute($request) {
185  $categoryId = $this->getCategoryId();
186  $categoryDao = DAORegistry::getDAO('CategoryDAO');
187 
188  // Get a category object to edit or create
189  if ($categoryId == null) {
190  $category = $categoryDao->newDataObject();
191  $category->setPressId($this->getPressId());
192  } else {
193  $category = $categoryDao->getById($categoryId, $this->getPressId());
194  }
195 
196  // Set the editable properties of the category object
197  $category->setTitle($this->getData('name'), null); // Localized
198  $category->setDescription($this->getData('description'), null); // Localized
199  $category->setParentId($this->getData('parentId'));
200  $category->setPath($this->getData('path'));
201 
202  // Handle the image upload if there was one.
203  if ($temporaryFileId = $this->getData('temporaryFileId')) {
204  // Fetch the temporary file storing the uploaded library file
205  $temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO');
206 
207  $temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $this->_userId);
208  $temporaryFilePath = $temporaryFile->getFilePath();
209  import('lib.pkp.classes.file.ContextFileManager');
210  $pressFileManager = new ContextFileManager($this->getPressId());
211  $basePath = $pressFileManager->getBasePath() . '/categories/';
212 
213  // Delete the old file if it exists
214  $oldSetting = $category->getImage();
215  if ($oldSetting) {
216  $pressFileManager->deleteFile($basePath . $oldSetting['thumbnailName']);
217  $pressFileManager->deleteFile($basePath . $oldSetting['name']);
218  }
219 
220  // The following variables were fetched in validation
221  assert($this->_sizeArray && $this->_imageExtension);
222 
223  // Generate the surrogate images.
224  switch ($this->_imageExtension) {
225  case '.jpg': $image = imagecreatefromjpeg($temporaryFilePath); break;
226  case '.png': $image = imagecreatefrompng($temporaryFilePath); break;
227  case '.gif': $image = imagecreatefromgif($temporaryFilePath); break;
228  }
229  assert($image);
230 
231  $thumbnailFilename = $category->getId() . '-category-thumbnail' . $this->_imageExtension;
232  $xRatio = min(1, THUMBNAIL_MAX_WIDTH / $this->_sizeArray[0]);
233  $yRatio = min(1, THUMBNAIL_MAX_HEIGHT / $this->_sizeArray[1]);
234 
235  $ratio = min($xRatio, $yRatio);
236 
237  $thumbnailWidth = round($ratio * $this->_sizeArray[0]);
238  $thumbnailHeight = round($ratio * $this->_sizeArray[1]);
239  $thumbnail = imagecreatetruecolor(THUMBNAIL_MAX_WIDTH, THUMBNAIL_MAX_HEIGHT);
240  $whiteColor = imagecolorallocate($thumbnail, 255, 255, 255);
241  imagefill($thumbnail, 0, 0, $whiteColor);
242  imagecopyresampled($thumbnail, $image, (THUMBNAIL_MAX_WIDTH - $thumbnailWidth)/2, (THUMBNAIL_MAX_HEIGHT - $thumbnailHeight)/2, 0, 0, $thumbnailWidth, $thumbnailHeight, $this->_sizeArray[0], $this->_sizeArray[1]);
243 
244  // Copy the new file over
245  $filename = $category->getId() . '-category' . $this->_imageExtension;
246  $pressFileManager->copyFile($temporaryFile->getFilePath(), $basePath . $filename);
247 
248  switch ($this->_imageExtension) {
249  case '.jpg': imagejpeg($thumbnail, $basePath . $thumbnailFilename); break;
250  case '.png': imagepng($thumbnail, $basePath . $thumbnailFilename); break;
251  case '.gif': imagegif($thumbnail, $basePath . $thumbnailFilename); break;
252  }
253  imagedestroy($thumbnail);
254  imagedestroy($image);
255 
256  $category->setImage(array(
257  'name' => $filename,
258  'width' => $this->_sizeArray[0],
259  'height' => $this->_sizeArray[1],
260  'thumbnailName' => $thumbnailFilename,
261  'thumbnailWidth' => THUMBNAIL_MAX_WIDTH,
262  'thumbnailHeight' => THUMBNAIL_MAX_HEIGHT,
263  'uploadName' => $temporaryFile->getOriginalFileName(),
264  'dateUploaded' => Core::getCurrentDate(),
265  ));
266 
267  // Clean up the temporary file
268  import('lib.pkp.classes.file.TemporaryFileManager');
269  $temporaryFileManager = new TemporaryFileManager();
270  $temporaryFileManager->deleteFile($temporaryFileId, $this->_userId);
271  }
272 
273  // Update or insert the category object
274  if ($categoryId == null) {
275  $categoryId = $categoryDao->insertObject($category);
276  } else {
277  $categoryDao->updateObject($category);
278  }
279 
280  return $category;
281  }
282 }
283 
284 ?>
static & getDAO($name, $dbconn=null)
addCheck($formValidator)
Definition: Form.inc.php:396
setData($key, $value)
Definition: Form.inc.php:219
Form validation check with a custom user function performing the validation check.
Form validation check to make sure the form is POSTed.
Class defining operations for private context file management.
readUserVars($vars)
Definition: Form.inc.php:364
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
addError($field, $message)
Definition: Form.inc.php:405
getData($key)
Definition: Form.inc.php:210
CategoryForm($pressId, $categoryId=null)
Class to represent a form validation check for localized fields.
Form validation check for alphanumeric (plus interior dash/underscore) characters only...
Form to add/edit category.
static getCurrentDate($ts=null)
Definition: Core.inc.php:95