00001 <?php
00002
00015 import('lib.pkp.classes.form.Form');
00016
00017 define('THUMBNAIL_MAX_WIDTH', 106);
00018 define('THUMBNAIL_MAX_HEIGHT', 100);
00019
00020 class CategoryForm extends Form {
00022 var $_categoryId;
00023
00025 var $_pressId;
00026
00028 var $_userId;
00029
00031 var $_imageExtension;
00032
00034 var $_sizeArray;
00035
00036
00042 function CategoryForm($pressId, $categoryId = null) {
00043 parent::Form('controllers/grid/settings/category/form/categoryForm.tpl');
00044 $this->_pressId = $pressId;
00045 $this->_categoryId = $categoryId;
00046
00047 $request =& Application::getRequest();
00048 $user =& $request->getUser();
00049 $this->_userId = $user->getUserId();
00050
00051
00052 $this->addCheck(new FormValidatorLocale($this, 'name', 'required', 'grid.category.nameRequired'));
00053 $this->addCheck(new FormValidatorAlphaNum($this, 'path', 'required', 'grid.category.pathAlphaNumeric'));
00054 $this->addCheck(new FormValidatorCustom(
00055 $this, 'path', 'required', 'grid.category.pathExists',
00056 create_function(
00057 '$path,$form,$categoryDao,$pressId',
00058 'return !$categoryDao->categoryExistsByPath($path,$pressId) || ($form->getData(\'oldPath\') != null && $form->getData(\'oldPath\') == $path);'
00059 ),
00060 array(&$this, DAORegistry::getDAO('CategoryDAO'), $pressId)
00061 ));
00062 $this->addCheck(new FormValidatorPost($this));
00063 }
00064
00065
00066
00067
00072 function getCategoryId() {
00073 return $this->_categoryId;
00074 }
00075
00080 function getPressId() {
00081 return $this->_pressId;
00082 }
00083
00084
00085
00086
00090 function getLocaleFieldNames() {
00091 $categoryDao =& DAORegistry::getDAO('CategoryDAO');
00092 return $categoryDao->getLocaleFieldNames();
00093 }
00094
00098 function initData() {
00099 $categoryDao =& DAORegistry::getDAO('CategoryDAO');
00100 $category = $categoryDao->getById($this->getCategoryId(), $this->getPressId());
00101
00102 if ($category) {
00103 $this->setData('name', $category->getTitle(null));
00104 $this->setData('description', $category->getDescription(null));
00105 $this->setData('parentId', $category->getParentId());
00106 $this->setData('path', $category->getPath());
00107 $this->setData('image', $category->getImage());
00108 }
00109 }
00110
00114 function validate() {
00115 if ($temporaryFileId = $this->getData('temporaryFileId')) {
00116 import('classes.file.TemporaryFileManager');
00117 $temporaryFileManager = new TemporaryFileManager();
00118 $temporaryFileDao =& DAORegistry::getDAO('TemporaryFileDAO');
00119 $temporaryFile =& $temporaryFileDao->getTemporaryFile($temporaryFileId, $this->_userId);
00120 if ( !$temporaryFile ||
00121 !($this->_imageExtension = $temporaryFileManager->getImageExtension($temporaryFile->getFileType())) ||
00122 !($this->_sizeArray = getimagesize($temporaryFile->getFilePath())) ||
00123 $this->_sizeArray[0] <= 0 || $this->_sizeArray[1] <= 0
00124 ) {
00125 $this->addError('temporaryFileId', __('form.invalidImage'));
00126 return false;
00127 }
00128 }
00129 return parent::validate();
00130 }
00131
00135 function readInputData() {
00136 $this->readUserVars(array('name', 'parentId', 'path', 'description', 'temporaryFileId'));
00137
00138
00139 if ($categoryId = $this->getCategoryId()) {
00140 $categoryDao =& DAORegistry::getDAO('CategoryDAO');
00141 $category =& $categoryDao->getById($categoryId, $this->getPressId());
00142 $this->setData('oldPath', $category->getPath());
00143 }
00144 }
00145
00149 function fetch($request) {
00150 $categoryDao =& DAORegistry::getDAO('CategoryDAO');
00151 $press =& $request->getPress();
00152 $templateMgr =& TemplateManager::getManager();
00153 $templateMgr->assign('categoryId', $this->getCategoryId());
00154
00155
00156 $rootCategoriesIterator =& $categoryDao->getByParentId(0, $press->getId());
00157 $rootCategories = array(0 => __('common.none'));
00158 while ($category =& $rootCategoriesIterator->next()) {
00159 $categoryId = $category->getId();
00160 if ($categoryId != $this->getCategoryId()) {
00161
00162 $rootCategories[$categoryId] = $category->getLocalizedTitle();
00163 }
00164 unset($category);
00165 }
00166 $templateMgr->assign('rootCategories', $rootCategories);
00167
00168
00169
00170
00171 if ($this->getCategoryId()) {
00172 $children =& $categoryDao->getByParentId($this->getCategoryId(), $press->getId());
00173 if ($children->next()) {
00174 $templateMgr->assign('cannotSelectChild', true);
00175 }
00176 }
00177
00178 return parent::fetch($request);
00179 }
00180
00184 function execute(&$request) {
00185 $categoryId = $this->getCategoryId();
00186 $categoryDao =& DAORegistry::getDAO('CategoryDAO');
00187
00188
00189 if ($categoryId == null) {
00190 $category = $categoryDao->newDataObject();
00191 $category->setPressId($this->getPressId());
00192 } else {
00193 $category = $categoryDao->getById($categoryId, $this->getPressId());
00194 }
00195
00196
00197 $category->setTitle($this->getData('name'), null);
00198 $category->setDescription($this->getData('description'), null);
00199 $category->setParentId($this->getData('parentId'));
00200 $category->setPath($this->getData('path'));
00201
00202
00203 if ($temporaryFileId = $this->getData('temporaryFileId')) {
00204
00205 $temporaryFileDao =& DAORegistry::getDAO('TemporaryFileDAO');
00206
00207 $temporaryFile =& $temporaryFileDao->getTemporaryFile($temporaryFileId, $this->_userId);
00208 $temporaryFilePath = $temporaryFile->getFilePath();
00209 import('classes.file.PressFileManager');
00210 $pressFileManager = new PressFileManager($this->getPressId());
00211 $basePath = $pressFileManager->getBasePath() . '/categories/';
00212
00213
00214 $oldSetting = $category->getImage();
00215 if ($oldSetting) {
00216 $pressFileManager->deleteFile($basePath . $oldSetting['thumbnailName']);
00217 $pressFileManager->deleteFile($basePath . $oldSetting['name']);
00218 }
00219
00220
00221 assert($this->_sizeArray && $this->_imageExtension);
00222
00223
00224 switch ($this->_imageExtension) {
00225 case '.jpg': $image = imagecreatefromjpeg($temporaryFilePath); break;
00226 case '.png': $image = imagecreatefrompng($temporaryFilePath); break;
00227 case '.gif': $image = imagecreatefromgif($temporaryFilePath); break;
00228 }
00229 assert($image);
00230
00231 $thumbnailFilename = $category->getId() . '-category-thumbnail' . $this->_imageExtension;
00232 $xRatio = min(1, THUMBNAIL_MAX_WIDTH / $this->_sizeArray[0]);
00233 $yRatio = min(1, THUMBNAIL_MAX_HEIGHT / $this->_sizeArray[1]);
00234
00235 $ratio = min($xRatio, $yRatio);
00236
00237 $thumbnailWidth = round($ratio * $this->_sizeArray[0]);
00238 $thumbnailHeight = round($ratio * $this->_sizeArray[1]);
00239 $thumbnail = imagecreatetruecolor(THUMBNAIL_MAX_WIDTH, THUMBNAIL_MAX_HEIGHT);
00240 $whiteColor = imagecolorallocate($thumbnail, 255, 255, 255);
00241 imagefill($thumbnail, 0, 0, $whiteColor);
00242 imagecopyresampled($thumbnail, $image, (THUMBNAIL_MAX_WIDTH - $thumbnailWidth)/2, (THUMBNAIL_MAX_HEIGHT - $thumbnailHeight)/2, 0, 0, $thumbnailWidth, $thumbnailHeight, $this->_sizeArray[0], $this->_sizeArray[1]);
00243
00244
00245 $filename = $category->getId() . '-category' . $this->_imageExtension;
00246 $pressFileManager->copyFile($temporaryFile->getFilePath(), $basePath . $filename);
00247
00248 switch ($this->_imageExtension) {
00249 case '.jpg': imagejpeg($thumbnail, $basePath . $thumbnailFilename); break;
00250 case '.png': imagepng($thumbnail, $basePath . $thumbnailFilename); break;
00251 case '.gif': imagegif($thumbnail, $basePath . $thumbnailFilename); break;
00252 }
00253 imagedestroy($thumbnail);
00254 imagedestroy($image);
00255
00256 $category->setImage(array(
00257 'name' => $filename,
00258 'width' => $this->_sizeArray[0],
00259 'height' => $this->_sizeArray[1],
00260 'thumbnailName' => $thumbnailFilename,
00261 'thumbnailWidth' => THUMBNAIL_MAX_WIDTH,
00262 'thumbnailHeight' => THUMBNAIL_MAX_HEIGHT,
00263 'uploadName' => $temporaryFile->getOriginalFileName(),
00264 'dateUploaded' => Core::getCurrentDate(),
00265 ));
00266
00267
00268 import('classes.file.TemporaryFileManager');
00269 $temporaryFileManager = new TemporaryFileManager();
00270 $temporaryFileManager->deleteFile($temporaryFileId, $this->_userId);
00271 }
00272
00273
00274 if ($categoryId == null) {
00275 $categoryId = $categoryDao->insertObject($category);
00276 } else {
00277 $categoryDao->updateObject($category);
00278 }
00279
00280 return $category;
00281 }
00282 }
00283
00284 ?>