16 import(
'lib.pkp.classes.form.Form');
40 function __construct($contextId, $categoryId =
null) {
41 parent::__construct(
'controllers/grid/settings/category/form/categoryForm.tpl');
42 $this->_contextId = $contextId;
43 $this->_categoryId = $categoryId;
46 $user = $request->getUser();
47 $this->_userId = $user->getId();
54 $this,
'path',
'required',
'grid.category.pathExists',
55 function($path) use ($form, $contextId) {
57 return !$categoryDao->categoryExistsByPath($path,$contextId) || ($form->getData(
'oldPath') !=
null && $form->getData(
'oldPath') == $path);
80 $this->_categoryId = $categoryId;
99 return $categoryDao->getLocaleFieldNames();
110 $this->
setData(
'name', $category->getTitle(
null));
111 $this->
setData(
'description', $category->getDescription(
null));
112 $this->
setData(
'parentId', $category->getParentId());
113 $this->
setData(
'path', $category->getPath());
114 $this->
setData(
'image', $category->getImage());
117 $sortOption = $category->getSortOption() ? $category->getSortOption() : $submissionDao->getDefaultSortOption();
118 $this->
setData(
'sortOption', $sortOption);
125 function validate($callHooks =
true) {
126 if ($temporaryFileId = $this->
getData(
'temporaryFileId')) {
127 import(
'lib.pkp.classes.file.TemporaryFileManager');
130 $temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $this->_userId);
131 if ( !$temporaryFile ||
132 !($this->_imageExtension = $temporaryFileManager->getImageExtension($temporaryFile->getFileType())) ||
133 !($this->_sizeArray = getimagesize($temporaryFile->getFilePath())) ||
134 $this->_sizeArray[0] <= 0 || $this->_sizeArray[1] <= 0
136 $this->
addError(
'temporaryFileId', __(
'form.invalidImage'));
140 return parent::validate($callHooks);
147 $this->
readUserVars(array(
'name',
'parentId',
'path',
'description',
'temporaryFileId',
'sortOption',
'subEditors'));
152 $category = $categoryDao->getById($categoryId, $this->
getContextId());
153 $this->
setData(
'oldPath', $category->getPath());
160 function fetch($request, $template =
null, $display =
false) {
162 $context = $request->getContext();
167 $rootCategoriesIterator = $categoryDao->getByParentId(0, $context->getId());
168 $rootCategories = array(0 => __(
'common.none'));
169 while ($category = $rootCategoriesIterator->next()) {
170 $categoryId = $category->getId();
173 $rootCategories[$categoryId] = $category->getLocalizedTitle();
176 $templateMgr->assign(
'rootCategories', $rootCategories);
182 $children = $categoryDao->getByParentId($this->
getCategoryId(), $context->getId());
183 if ($children->next()) {
184 $templateMgr->assign(
'cannotSelectChild',
true);
189 $templateMgr->assign(
'sortOptions', $submissionDao->getSortSelectOptions());
193 'contextId' => $context->getId(),
194 'roleIds' => ROLE_ID_SUB_EDITOR,
196 $availableSubeditors = [];
197 foreach ($usersIterator as $user) {
198 $availableSubeditors[(int) $user->getId()] = $user->getFullName();
200 $assignedToCategory = [];
203 'contextId' => $context->getId(),
204 'roleIds' => ROLE_ID_SUB_EDITOR,
205 'assignedToCategory' => (
int) $this->getCategoryId(),
208 $templateMgr->assign([
209 'availableSubeditors' => $availableSubeditors,
210 'assignedToCategory' => $assignedToCategory,
213 return parent::fetch($request, $template, $display);
219 function execute(...$functionArgs) {
224 if ($categoryId ==
null) {
225 $category = $categoryDao->newDataObject();
228 $category = $categoryDao->getById($categoryId, $this->
getContextId());
232 $category->setTitle($this->
getData(
'name'),
null);
233 $category->setDescription($this->
getData(
'description'),
null);
234 $category->setParentId($this->
getData(
'parentId'));
235 $category->setPath($this->
getData(
'path'));
236 $category->setSortOption($this->
getData(
'sortOption'));
239 if ($categoryId ==
null) {
242 $category->setSequence(REALLY_BIG_NUMBER);
243 $categoryDao->updateObject($category);
244 $categoryDao->resequenceCategories($this->
getContextId());
249 $subEditorsDao->deleteBySubmissionGroupId($category->getId(), ASSOC_TYPE_CATEGORY, $category->getContextId());
250 $subEditors = $this->
getData(
'subEditors');
251 if (!empty($subEditors)) {
253 foreach ($subEditors as $subEditor) {
254 if ($roleDao->userHasRole($category->getContextId(), $subEditor, ROLE_ID_SUB_EDITOR)) {
255 $subEditorsDao->insertEditor($category->getContextId(), $category->getId(), $subEditor, ASSOC_TYPE_CATEGORY);
261 if ($temporaryFileId = $this->
getData(
'temporaryFileId')) {
265 $temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $this->_userId);
266 $temporaryFilePath = $temporaryFile->getFilePath();
267 import(
'lib.pkp.classes.file.ContextFileManager');
269 $basePath = $contextFileManager->getBasePath() .
'/categories/';
272 $oldSetting = $category->getImage();
274 $contextFileManager->deleteByPath($basePath . $oldSetting[
'thumbnailName']);
275 $contextFileManager->deleteByPath($basePath . $oldSetting[
'name']);
279 assert($this->_sizeArray && $this->_imageExtension);
282 switch ($this->_imageExtension) {
283 case '.jpg': $image = imagecreatefromjpeg($temporaryFilePath);
break;
284 case '.png': $image = imagecreatefrompng($temporaryFilePath);
break;
285 case '.gif': $image = imagecreatefromgif($temporaryFilePath);
break;
286 default: $image =
null;
291 $coverThumbnailsMaxWidth = $context->getSetting(
'coverThumbnailsMaxWidth');
292 $coverThumbnailsMaxHeight = $context->getSetting(
'coverThumbnailsMaxHeight');
294 $xRatio = min(1, ($coverThumbnailsMaxWidth?$coverThumbnailsMaxWidth:100) / $this->_sizeArray[0]);
295 $yRatio = min(1, ($coverThumbnailsMaxHeight?$coverThumbnailsMaxHeight:100) / $this->_sizeArray[1]);
297 $ratio = min($xRatio, $yRatio);
299 $thumbnailWidth = round($ratio * $this->_sizeArray[0]);
300 $thumbnailHeight = round($ratio * $this->_sizeArray[1]);
301 $thumbnail = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
302 imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $this->_sizeArray[0], $this->_sizeArray[1]);
306 $contextFileManager->copyFile($temporaryFile->getFilePath(), $basePath . $filename);
308 switch ($this->_imageExtension) {
309 case '.jpg': imagejpeg($thumbnail, $basePath . $thumbnailFilename);
break;
310 case '.png': imagepng($thumbnail, $basePath . $thumbnailFilename);
break;
311 case '.gif': imagegif($thumbnail, $basePath . $thumbnailFilename);
break;
313 imagedestroy($thumbnail);
314 imagedestroy($image);
316 $category->setImage(array(
318 'width' => $this->_sizeArray[0],
319 'height' => $this->_sizeArray[1],
320 'thumbnailName' => $thumbnailFilename,
321 'thumbnailWidth' => $thumbnailWidth,
322 'thumbnailHeight' => $thumbnailHeight,
323 'uploadName' => $temporaryFile->getOriginalFileName(),
328 import(
'lib.pkp.classes.file.TemporaryFileManager');
330 $temporaryFileManager->deleteById($temporaryFileId, $this->_userId);
334 $categoryDao->updateObject($category);
335 parent::execute(...$functionArgs);