• Main Page
  • Modules
  • Classes
  • Files
  • File List

controllers/grid/settings/series/form/SeriesForm.inc.php

00001 <?php
00002 
00016 import('lib.pkp.classes.form.Form');
00017 
00018 define('THUMBNAIL_MAX_WIDTH', 106);
00019 define('THUMBNAIL_MAX_HEIGHT', 100);
00020 
00021 class SeriesForm extends Form {
00023    var $_seriesId;
00024 
00026    var $_userId;
00027 
00029    var $_imageExtension;
00030 
00032    var $_sizeArray;
00033 
00037    function SeriesForm($seriesId = null) {
00038       $this->setSeriesId($seriesId);
00039 
00040       $request =& Application::getRequest();
00041       $user =& $request->getUser();
00042       $this->_userId = $user->getUserId();
00043 
00044       parent::Form('controllers/grid/settings/series/form/seriesForm.tpl');
00045 
00046       // Validation checks for this form
00047       $this->addCheck(new FormValidatorLocale($this, 'title', 'required', 'manager.setup.form.series.nameRequired'));
00048       $this->addCheck(new FormValidatorPost($this));
00049    }
00050 
00056    function initData($args, &$request) {
00057       $press =& $request->getPress();
00058 
00059       $seriesDao =& DAORegistry::getDAO('SeriesDAO');
00060       $seriesId = $this->getSeriesId();
00061       if ($seriesId) {
00062          $series =& $seriesDao->getById($seriesId, $press->getId());
00063       }
00064 
00065       if (isset($series) ) {
00066          $this->_data = array(
00067             'seriesId' => $seriesId,
00068             'title' => $series->getTitle(null),
00069             'featured' => $series->getFeatured(),
00070             'path' => $series->getPath(),
00071             'description' => $series->getDescription(null),
00072             'prefix' => $series->getPrefix(null),
00073             'subtitle' => $series->getSubtitle(null),
00074             'image' => $series->getImage(),
00075          );
00076       }
00077    }
00078 
00082    function validate() {
00083       if ($temporaryFileId = $this->getData('temporaryFileId')) {
00084          import('classes.file.TemporaryFileManager');
00085          $temporaryFileManager = new TemporaryFileManager();
00086          $temporaryFileDao =& DAORegistry::getDAO('TemporaryFileDAO');
00087          $temporaryFile =& $temporaryFileDao->getTemporaryFile($temporaryFileId, $this->_userId);
00088          if (  !$temporaryFile ||
00089                !($this->_imageExtension = $temporaryFileManager->getImageExtension($temporaryFile->getFileType())) ||
00090                !($this->_sizeArray = getimagesize($temporaryFile->getFilePath())) ||
00091                $this->_sizeArray[0] <= 0 || $this->_sizeArray[1] <= 0
00092          ) {
00093             $this->addError('temporaryFileId', __('form.invalidImage'));
00094             return false;
00095          }
00096       }
00097       return parent::validate();
00098    }
00099 
00105    function fetch(&$request) {
00106       $templateMgr =& TemplateManager::getManager();
00107       $templateMgr->assign('seriesId', $this->getSeriesId());
00108 
00109       $press =& $request->getPress();
00110       $userGroupDao =& DAORegistry::getDAO('UserGroupDAO');
00111       $seriesEditorCount = $userGroupDao->getContextUsersCount($press->getId(), null, ROLE_ID_SERIES_EDITOR);
00112       $templateMgr->assign('seriesEditorCount', $seriesEditorCount);
00113 
00114       $categoryDao =& DAORegistry::getDAO('CategoryDAO');
00115       $categoryCount = $categoryDao->getCountByPressId($press->getId());
00116       $templateMgr->assign('categoryCount', $categoryCount);
00117       return parent::fetch($request);
00118    }
00119 
00124    function readInputData() {
00125       $this->readUserVars(array('seriesId', 'path', 'featured', 'title', 'description', 'seriesEditors', 'categories', 'prefix', 'subtitle', 'temporaryFileId'));
00126    }
00127 
00133    function execute($args, &$request) {
00134       $seriesDao =& DAORegistry::getDAO('SeriesDAO');
00135       $press =& $request->getPress();
00136 
00137       // Get or create the series object
00138       if ($this->getSeriesId()) {
00139          $series =& $seriesDao->getById($this->getSeriesId(), $press->getId());
00140       } else {
00141          import('classes.press.Series');
00142          $series = new Series();
00143          $series->setPressId($press->getId());
00144       }
00145 
00146       // Populate/update the series object from the form
00147       $series->setPath($this->getData('path'));
00148       $series->setFeatured($this->getData('featured'));
00149       $series->setTitle($this->getData('title'), null); // Localized
00150       $series->setDescription($this->getData('description'), null); // Localized
00151       $series->setPrefix($this->getData('prefix'), null); // Localized
00152       $series->setSubtitle($this->getData('subtitle'), null); // Localized
00153 
00154       // Handle the image upload if there was one.
00155       if ($temporaryFileId = $this->getData('temporaryFileId')) {
00156          // Fetch the temporary file storing the uploaded library file
00157          $temporaryFileDao =& DAORegistry::getDAO('TemporaryFileDAO');
00158 
00159          $temporaryFile =& $temporaryFileDao->getTemporaryFile($temporaryFileId, $this->_userId);
00160          $temporaryFilePath = $temporaryFile->getFilePath();
00161          import('classes.file.PressFileManager');
00162          $pressFileManager = new PressFileManager($press->getId());
00163          $basePath = $pressFileManager->getBasePath() . '/series/';
00164 
00165          // Delete the old file if it exists
00166          $oldSetting = $series->getImage();
00167          if ($oldSetting) {
00168             $pressFileManager->deleteFile($basePath . $oldSetting['thumbnailName']);
00169             $pressFileManager->deleteFile($basePath . $oldSetting['name']);
00170          }
00171 
00172          // The following variables were fetched in validation
00173          assert($this->_sizeArray && $this->_imageExtension);
00174 
00175          // Generate the surrogate image.
00176          switch ($this->_imageExtension) {
00177             case '.jpg': $image = imagecreatefromjpeg($temporaryFilePath); break;
00178             case '.png': $image = imagecreatefrompng($temporaryFilePath); break;
00179             case '.gif': $image = imagecreatefromgif($temporaryFilePath); break;
00180          }
00181          assert($image);
00182 
00183          $thumbnailFilename = $series->getId() . '-series-thumbnail' . $this->_imageExtension;
00184          $xRatio = min(1, THUMBNAIL_MAX_WIDTH / $this->_sizeArray[0]);
00185          $yRatio = min(1, THUMBNAIL_MAX_HEIGHT / $this->_sizeArray[1]);
00186 
00187          $ratio = min($xRatio, $yRatio);
00188 
00189          $thumbnailWidth = round($ratio * $this->_sizeArray[0]);
00190          $thumbnailHeight = round($ratio * $this->_sizeArray[1]);
00191          $thumbnail = imagecreatetruecolor(THUMBNAIL_MAX_WIDTH, THUMBNAIL_MAX_HEIGHT);
00192          $whiteColor = imagecolorallocate($thumbnail, 255, 255, 255);
00193          imagefill($thumbnail, 0, 0, $whiteColor);
00194          imagecopyresampled($thumbnail, $image, (THUMBNAIL_MAX_WIDTH - $thumbnailWidth)/2, (THUMBNAIL_MAX_HEIGHT - $thumbnailHeight)/2, 0, 0, $thumbnailWidth, $thumbnailHeight, $this->_sizeArray[0], $this->_sizeArray[1]);
00195 
00196          // Copy the new file over
00197          $filename = $series->getId() . '-series' . $this->_imageExtension;
00198          $pressFileManager->copyFile($temporaryFile->getFilePath(), $basePath . $filename);
00199 
00200          switch ($this->_imageExtension) {
00201             case '.jpg': imagejpeg($thumbnail, $basePath . $thumbnailFilename); break;
00202             case '.png': imagepng($thumbnail, $basePath . $thumbnailFilename); break;
00203             case '.gif': imagegif($thumbnail, $basePath . $thumbnailFilename); break;
00204          }
00205          imagedestroy($thumbnail);
00206          imagedestroy($image);
00207 
00208          $series->setImage(array(
00209                'name' => $filename,
00210                'width' => $this->_sizeArray[0],
00211                'height' => $this->_sizeArray[1],
00212                'thumbnailName' => $thumbnailFilename,
00213                'thumbnailWidth' => THUMBNAIL_MAX_WIDTH,
00214                'thumbnailHeight' => THUMBNAIL_MAX_HEIGHT,
00215                'uploadName' => $temporaryFile->getOriginalFileName(),
00216                'dateUploaded' => Core::getCurrentDate(),
00217          ));
00218 
00219          // Clean up the temporary file
00220          import('classes.file.TemporaryFileManager');
00221          $temporaryFileManager = new TemporaryFileManager();
00222          $temporaryFileManager->deleteFile($temporaryFileId, $this->_userId);
00223       }
00224 
00225       // Insert or update the series in the DB
00226       if ($this->getSeriesId()) {
00227          $seriesDao->updateObject($series);
00228       } else {
00229          $this->setSeriesId($seriesDao->insertObject($series));
00230       }
00231 
00232       import('lib.pkp.classes.controllers.listbuilder.ListbuilderHandler');
00233       // Save the series editor associations.
00234       ListbuilderHandler::unpack(
00235          $request,
00236          $this->getData('seriesEditors'),
00237          array(&$this, 'deleteSeriesEditorEntry'),
00238          array(&$this, 'insertSeriesEditorEntry'),
00239          array(&$this, 'updateSeriesEditorEntry')
00240       );
00241 
00242       // Save the category associations.
00243       ListbuilderHandler::unpack(
00244          $request,
00245          $this->getData('categories'),
00246          array(&$this, 'deleteCategoryEntry'),
00247          array(&$this, 'insertCategoryEntry'),
00248          array(&$this, 'updateCategoryEntry')
00249       );
00250 
00251       return true;
00252    }
00253 
00258    function getSeriesId() {
00259       return $this->_seriesId;
00260    }
00261 
00266    function setSeriesId($seriesId) {
00267       $this->_seriesId = $seriesId;
00268    }
00269 
00274    function insertSeriesEditorEntry(&$request, $newRowId) {
00275       $press =& $request->getPress();
00276       $seriesId = $this->getSeriesId();
00277       $userId = array_shift($newRowId);
00278 
00279       $seriesEditorsDao =& DAORegistry::getDAO('SeriesEditorsDAO');
00280 
00281       // Make sure the membership doesn't already exist
00282       if ($seriesEditorsDao->editorExists($press->getId(), $this->getSeriesId(), $userId)) {
00283          return false;
00284       }
00285 
00286       // Otherwise, insert the row.
00287       $seriesEditorsDao->insertEditor($press->getId(), $this->getSeriesId(), $userId, true, true);
00288       return true;
00289    }
00290 
00297    function deleteSeriesEditorEntry(&$request, $rowId) {
00298       $seriesEditorsDao =& DAORegistry::getDAO('SeriesEditorsDAO');
00299       $press =& $request->getPress();
00300 
00301       $seriesEditorsDao->deleteEditor($press->getId(), $this->getSeriesId(), $rowId);
00302       return true;
00303    }
00304 
00312    function updateSeriesEditorEntry(&$request, $rowId, $newRowId) {
00313       $this->deleteSeriesEditorEntry($request, $rowId);
00314       $this->insertSeriesEditorEntry($request, $newRowId);
00315       return true;
00316    }
00317 
00322    function insertCategoryEntry(&$request, $newRowId) {
00323       $press =& $request->getPress();
00324       $seriesId = $this->getSeriesId();
00325       $categoryId = array_shift($newRowId);
00326 
00327       $seriesDao =& DAORegistry::getDAO('SeriesDAO');
00328 
00329       // Make sure the membership doesn't already exist
00330       if ($seriesDao->categoryAssociationExists($this->getSeriesId(), $categoryId)) {
00331          return false;
00332       }
00333 
00334       // Otherwise, insert the row.
00335       $seriesDao->addCategory($this->getSeriesId(), $categoryId);
00336       return true;
00337    }
00338 
00345    function deleteCategoryEntry(&$request, $rowId) {
00346       $seriesDao =& DAORegistry::getDAO('SeriesDAO');
00347       $press =& $request->getPress();
00348 
00349       $seriesDao->removeCategory($this->getSeriesId(), $rowId);
00350       return true;
00351    }
00352 
00360    function updateCategoryEntry(&$request, $rowId, $newRowId) {
00361       $this->deleteCategoryEntry($request, $rowId);
00362       $this->insertCategoryEntry($request, $newRowId);
00363       return true;
00364    }
00365 }
00366 
00367 ?>

Generated on Mon Sep 17 2012 13:58:56 for Open Monograph Press by  doxygen 1.7.1