| // | Author: Florentin Mocuta | // +---------------------------------------------------------------------------+ // $Id: ManagerTemplate.html,v 1.2 2005/04/17 02:15:02 demian Exp $ require_once SGL_MOD_DIR . '/navigation/classes/MenuBuilder.php'; require_once 'DB/DataObject.php'; /** * Photo Galley Admin class * * @package gallery * @author Rares Benea * @author Florentin Mocuta * @version $Revision: 1.1 $ */ class GalleryAdminMgr extends SGL_Manager { function GalleryAdminMgr() { SGL::logMessage(null, PEAR_LOG_DEBUG); parent::SGL_Manager(); $this->pageTitle = 'Photo Gallery'; $this->template = 'galleryList.html'; $this->masterTemplate = 'masterLeftCol.html'; $this->_aAllowedFileTypes = array( 'gif', 'jpeg', 'jpg', 'png', ); $this->_aActionsMapping = array( 'add' => array('add'), 'insert' => array('insert', 'redirectToDefault'), 'edit' => array('edit'), 'update' => array('update', 'redirectToDefault'), 'delete' => array('delete', 'redirectToDefault'), 'list' => array('list'), 'edit' => array('edit'), 'reindex' => array('reindex'), ); } function validate($req, &$input) { SGL::logMessage(null, PEAR_LOG_DEBUG); $this->validated = true; $input->error = array(); $input->pageTitle = $this->pageTitle; $input->masterTemplate = $this->masterTemplate; $input->template = $this->template; $input->action = ($req->get('action')) ? $req->get('action') : 'list'; $input->submitted = $req->get('submitted'); $input->gallery = (object)$req->get('gallery'); $input->from = ($req->get('frmFrom')) ? $req->get('frmFrom') : 0; $input->totalItems = $req->get('totalItems'); $input->aDelete = $req->get('frmDelete'); $input->catID = (int)$req->get('frmCatID'); $input->galleryID = $req->get('frmGalleryID'); $input->aEdit = $req->get('editID'); $input->imageFileArray = $req->get('imageFile'); $input->aReindexOps = $req->get('frmReindexOps'); if (SGL_Session::getRoleId() == SGL_ADMIN) { $input->isAdmin = true; } $input->javascriptSrc = array('TreeMenu.js'); $forceValid = false; // if document has been uploaded if ($input->submitted) { if ($input->action == 'delete') { if (!is_array($input->aDelete)) { SGL::raiseMsg('Please select images to delete', true, SGL_MESSAGE_ERROR); $input->action = 'list'; } } elseif ($input->action == 'insert') { $input->mode = 'Add'; $input->template = 'galleryAdd.html'; if (empty($input->imageFileArray['name'])) { $aErrors['file'] = 'Please select a file to upload'; } else { $ext = end(explode('.', $input->imageFileArray['name'])); // check uploaded file is of valid type if (!in_array(strtolower($ext), $this->_aAllowedFileTypes)) { $aErrors['file'] = SGL_String::translate( 'Error: Not a recognised file type'); } } } elseif ($input->action == 'update') { $input->template = 'galleryEdit.html'; if (!empty($input->imageFileArray['name'])) { $ext = end(explode('.', $input->imageFileArray['name'])); // check uploaded file is of valid type if (!in_array(strtolower($ext), $this->_aAllowedFileTypes)) { $aErrors['file'] = SGL_String::translate( 'Error: Not a recognised file type'); $input->action = 'edit'; $forceValid = true; } } } } // if errors have occured if (isset($aErrors) && count($aErrors)) { SGL::raiseMsg('Please fill in the indicated fields', true, SGL_MESSAGE_ERROR); $input->error = $aErrors; // if we update and have error, force valid and redirect to edit if (!$forceValid) { $this->validated = false; } } } function display(&$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); if ($output->catID > 1) { $output->currentCat = $output->catID; } elseif (!empty($output->gallery->category_id)) { $output->currentCat = $output->gallery->category_id; } else { $output->currentCat = $this->conf['GalleryAdminMgr']['galleryRootCatID']; } // prepare breadcrumbs and category changer $menu = & new MenuBuilder('SelectBox'); $htmlOptions = $menu->toHtml(); $output->aCategories = $htmlOptions; $output->breadCrumbs = $menu->getBreadCrumbs($output->currentCat); } /** * Add image * * @access public * */ function _cmd_add(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); $output->mode = 'Add'; $output->pageTitle = 'Photo Gallery'; $output->template = 'galleryAdd.html'; $output->catID = $input->catID; // build ordering select object $output->gallery = DB_DataObject::factory($this->conf['table']['gallery']); } /** * Insert image * * @access public * */ function _cmd_insert(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); SGL_DB::setConnection(); // insert record $newEntry = DB_DataObject::factory($this->conf['table']['gallery']); $newEntry->setFrom($input->gallery); $newEntry->gallery_id = $this->dbh->nextId($this->conf['table']['gallery']); $newName = $newEntry->gallery_id . '_' . substr($input->imageFileArray['name'], 0, strlen($input->imageFileArray['name']) - 4) . '.jpg'; $newEntry->image = $newName; $imageDir = SGL_WEB_ROOT.'/'.$this->conf['imageUpload']['directory']; $imageWidth = $this->conf['imageUpload']['imageWidth']; $imageHeight = $this->conf['imageUpload']['imageHeight']; $thumbDir = SGL_WEB_ROOT.'/'.$this->conf['imageUpload']['thumb']; $thumbWidth = $this->conf['imageUpload']['thumbWidth']; $thumbHeight = $this->conf['imageUpload']['thumbHeight']; if (!empty($input->imageFileArray['name'])) { $retImage = $this->processImage($input->imageFileArray['tmp_name'], $newName, $imageDir, $imageWidth, $imageHeight); $retThumb = $this->processImage($input->imageFileArray['tmp_name'], $newName, $thumbDir, $thumbWidth, $thumbHeight); } $success = false; if (!is_a($retImage,'PEAR_Error') && !is_a($retThumb,'PEAR_Error')) { $success = $newEntry->insert(); } if ($success) { SGL::raiseMsg('new gallery entry saved successfully', true, SGL_MESSAGE_INFO); } else { SGL::raiseError('There was a problem inserting the record', SGL_ERROR_NOAFFECTEDROWS); } } /** * Edit image * * @access public * */ function _cmd_edit(& $input, & $output){ SGL::logMessage(null, PEAR_LOG_DEBUG); $output->mode .= 'Edit'; $output->pageTitle = 'Photo Gallery'; $output->template = 'galleryEdit.html'; // build ordering select object $item = DB_DataObject::factory($this->conf['table']['gallery']); $item->get($input->galleryID); $output->gallery = $item; $output->thumbPath = $this->conf['imageUpload']['thumb']; $output->imagePath = $this->conf['imageUpload']['directory']; } /** * Update image * * @access public * */ function _cmd_update(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); SGL_DB::setConnection(); $newEntry = DB_DataObject::factory($this->conf['table']['gallery']); $newEntry->get($input->galleryID); $newEntry->setFrom($input->gallery); $imageDir = SGL_WEB_ROOT.'/'.$this->conf['imageUpload']['directory']; $imageWidth = $this->conf['imageUpload']['imageWidth']; $imageHeight = $this->conf['imageUpload']['imageHeight']; $thumbDir = SGL_WEB_ROOT.'/'.$this->conf['imageUpload']['thumb']; $thumbWidth = $this->conf['imageUpload']['thumbWidth']; $thumbHeight = $this->conf['imageUpload']['thumbHeight']; if (!empty($input->imageFileArray['name'])) { $retImage = $this->processImage($input->imageFileArray['tmp_name'], $newEntry->image, $imageDir, $imageWidth, $imageHeight); $retThumb = $this->processImage($input->imageFileArray['tmp_name'], $newEntry->image, $thumbDir, $thumbWidth, $thumbHeight); } $success = false; $success = $newEntry->update(); if ($success === false) { SGL::raiseError('There was a problem inserting the record', SGL_ERROR_NOAFFECTEDROWS); } else { SGL::raiseMsg('new gallery entry saved successfully', true, SGL_MESSAGE_INFO); } } /** * List image * * @access public * */ function _cmd_list(& $input, & $output) { SGL::logMessage(null, PEAR_LOG_DEBUG); $whereClause = ''; if ($input->catID) { $whereClause = ' WHERE category_id = "'.$input->catID.'" '; } $query = " SELECT * FROM {$this->conf['table']['gallery']} $whereClause ORDER BY item_order DESC"; $limit = $_SESSION['aPrefs']['resPerPage']; $pagerOptions = array( 'mode' => 'Sliding', 'delta' => 3, 'perPage' => $limit, 'totalItems'=> $input->totalItems, ); $aPagedData = SGL_DB::getPagedData($this->dbh, $query, $pagerOptions); if (is_a($aPagedData, 'DB_Error')) { SGL::raiseError('Error in : Get the paged data.', DB_Error); return; } if (is_array($aPagedData['data']) && count($aPagedData['data'])) { $output->pager = ($aPagedData['totalItems'] <= $limit) ? false : true; $output->totalItems = $aPagedData['totalItems']; $currentPage = $aPagedData['page_numbers']['current']; foreach($aPagedData['data'] as $key => $value) { $aPagedData['data'][$key]['detailsPage'] = ($currentPage - 1) * $limit + $key + 1; } } $output->aPagedData = $aPagedData; $output->thumbPath = $this->conf['imageUpload']['thumb']; $output->imagePath = $this->conf['imageUpload']['directory']; } /** * Delete image * * @access public * */ function _cmd_delete (& $input, & $output) { SGL::logMessage(null, PEAR_LOG_DEBUG); if (is_array($input->aDelete)) { foreach ($input->aDelete as $index => $gallery_id) { $oEntry = DB_DataObject::factory($this->conf['table']['gallery']); $oEntry->get($gallery_id); $imageFile = $this->conf['imageUpload']['directory'] . '/' . $oEntry->image; $thumbFile = $this->conf['imageUpload']['thumb'] . '/' . $oEntry->image; if (strlen($oEntry->image) && file_exists($imageFile)) { unlink($imageFile); } if (strlen($oEntry->image) && file_exists($thumbFile)) { unlink($thumbFile); } $oEntry->delete(); unset ($oEntry); } } else { SGL :: raiseError('Incorrect parameter passed to '.__CLASS__.'::'.__FUNCTION__, SGL_ERROR_INVALIDARGS); } SGL :: raiseMsg('Entry deleted successfully', true, SGL_MESSAGE_INFO); } /** * Reindex image gallery content: * - if there are new files in gallery dir, add them to DB * - if the files exist in DB check if resize is needed * - if you have orphan records in DB, delete them * * @access public * */ function _cmd_reindex(& $input, & $output) { SGL::logMessage(null,PEAR_LOG_DEBUG); SGL_DB::setConnection(); $output->mode = 'Reindex'; $output->pageTitle = 'Gallery Photos Reindex'; $output->template = 'galleryReindex.html'; if(empty($input->aReindexOps) || !is_array($input->aReindexOps) || count($input->aReindexOps) == 0) { // Exit if no operation was selected return; } if(in_array('new',$input->aReindexOps)) { $output->newOp = 1; } if(in_array('resize',$input->aReindexOps)) { $output->resizeOp = 1; } if(in_array('delete',$input->aReindexOps)) { $output->deleteOp = 1; } $output->resultForm = 1; require_once 'Image/Transform.php'; $imageDir = SGL_WEB_ROOT.'/'.$this->conf['imageUpload']['directory']; $imageWidth = $this->conf['imageUpload']['imageWidth']; $imageHeight = $this->conf['imageUpload']['imageHeight']; $thumbDir = SGL_WEB_ROOT.'/'.$this->conf['imageUpload']['thumb']; $thumbWidth = $this->conf['imageUpload']['thumbWidth']; $thumbHeight = $this->conf['imageUpload']['thumbHeight']; $importCatID = $this->conf['GalleryAdminMgr']['galleryRootCatID']; if (!is_dir($imageDir)) { SGL::raiseError("$imageDir is not a valid directory",'SGL_ERROR'); } $fArray = array(); // will contain the files from the gallery dir $aFotoOrphans = array(); // will contain the orphan files list $aFotoErrors = array(); // will contain the files with processing errors $aFoto = array(); // will contain the files without processing errors // put in $fArray all the files that we find in gallery dir $handle = opendir($imageDir); while ($entry = readdir($handle)) { if (is_file($imageDir .'/'.$entry)) { $ext = end(explode('.',$entry)); // check uploaded file is of valid type if (!in_array(strtolower($ext), $this->_aAllowedFileTypes)) { $aErrors['file'] = SGL_String::translate('Error: Not a ' . 'recognised file type'); } else { $fArray[] = array ('name' => $entry, 'ext' => $ext, 'tmp_name' => $imageDir . '/' .$entry); } } } closedir($handle); // get all the records from DB and check for orphan records $query = "SELECT * FROM ".$this->conf['table']['gallery']; $aResults = $this->dbh->getAll($query); foreach($aResults as $dbKey => $dbValue) { $bool = false; foreach ($fArray as $fKey => $fValue) { if ($dbValue->image == $fValue['name'] ) { // the file exists in DB too and we add it's gallery_id // into $fArray so we know later that this file is not new $fArray[$fKey]['gallery_id'] = $dbValue->gallery_id; $fArray[$fKey]['category_id'] = $dbValue->category_id; $bool = true; } } if (!$bool && isset($output->deleteOp)) { // we have an orphan record if(($input->catID > 0 && $input->catID == $dbValue->category_id) || $input->catID == 0) { // skip if we selected a category but is not ours // delete thumb if exist if (!empty($dbValue->image) && file_exists($thumbDir . '/' . $dbValue->image)) { unlink($thumbDir . '/' . $dbValue->image); } // delete from db $query = "DELETE FROM {$this->conf['table']['gallery']} WHERE gallery_id='{$dbValue->gallery_id}'"; $result = $this->dbh->query($query); if (is_a($result,'PEAR_Error')) { $aFotoErrors[] = array( 'name' => $dbValue->image , 'message' => SGL_String::translate('Error: Deleting ' . 'orphan image from database')); } else { $aFotoOrphans[] = array( 'name' => $dbValue->image , 'message' => SGL_String::translate('Orphan image ' . 'entry succesfully deleted from database')); } } } } if (count($fArray)) { foreach ($fArray as $key => $image) { // check if the image file is new and add it to DB if (empty($image['gallery_id'])) { if(isset($output->newOp)) { // We have a new file, let's insert it $newEntry = DB_DataObject::factory($this->conf['table']['gallery']); $newEntry->image = $image['name']; $newEntry->gallery_id = $this->dbh->nextId($this->conf['table']['gallery']); $newName = $newEntry->gallery_id . '_' . substr($image['name'],0,strlen($image['name']) - 4) . '.jpg'; $newEntry->image = $newName; $newEntry->category_id = $this->conf['GalleryAdminMgr']['galleryRootCatID']; //We replace the '_' with a ' ' and strip out the extension $newEntry->description = substr($image['name'],0, strlen($image['name']-strlen($image['ext']))); $newEntry->description = str_replace('_',' ',$newEntry->description); $retImage = $this->processImage($imageDir . '/' . $image['name'], $newName, $imageDir, $imageWidth, $imageHeight); $retThumb = $this->processImage($imageDir . '/' . $image['name'], $newName, $thumbDir, $thumbWidth, $thumbHeight); $success = false; if (!is_a($retImage,'PEAR_Error') && !is_a($retThumb,'PEAR_Error')) { $success = $newEntry->insert(); if (!$success) { $aFotoErrors[$image['name']] = SGL_String::translate('Error: Database ' . 'insertion failed'); unset($fArray[$key]); } else { $fArray[$key]['message'] = SGL_String::translate('Entry succesfully ' . 'added to database'); $fArray[$key]['gallery_id'] = $newEntry->gallery_id; // delete the tmp image file unlink($imageDir . '/' . $image['name']); } } else { $aFotoErrors[$image['name']] = SGL_String::translate('Error: Image upload failed'); unset($fArray[$key]); } unset($newEntry); } } else { // File allready exist, let's see if resize is needed if(isset($output->resizeOp)) { if(($input->catID > 0 && $input->catID == $image['category_id']) || $input->catID == 0) { // Check if image needs to be resized $retImage = $this->processImage($imageDir .'/' . $image['name'], $image['name'], $imageDir, $imageWidth, $imageHeight); if (is_a($retImage,'PEAR_Error')) { $aFotoErrors[$image['name']] = SGL_String::translate('Error: Image resize failed'); //unset($fArray[$key]); } elseif ($retImage === true) { $fArray[$key]['message'] = SGL_String::translate('Image resized'); } // Check if thumb needs to be resized if(file_exists($thumbDir .'/' . $image['name'])) { $thumbFile = $thumbDir .'/' . $image['name']; } else { // recreate thumb if the thub file does not exit // from the image file $thumbFile = $imageDir .'/' . $image['name']; } $retThumb = $this->processImage($thumbFile, $image['name'], $thumbDir, $thumbWidth, $thumbHeight); if (is_a($retThumb,'PEAR_Error')) { $aFotoErrors[$image['name']] = SGL_String::translate('Error: Thumb resize failed'); //unset($fArray[$key]); } elseif ($retThumb === true) { $fArray[$key]['message'] = SGL_String::translate('Thumb resized'); } } } } if(isset($output->deleteOp)) { // check for orphan thumb files $handle = opendir($thumbDir); while ($entry = readdir($handle)) { if (is_file($thumbDir .'/'.$entry)) { $ext = end(explode('.',$entry)); // check uploaded file is of valid type if (in_array(strtolower($ext), $this->_aAllowedFileTypes) && !file_exists($imageDir .'/'.$entry)) { if (!unlink($thumbDir .'/'.$entry)) { $aFotoErrors[] = array( 'name' => $entry , 'message' => SGL_String::translate('Error: Deleting ' . 'orphan thumb file from directory')); } else { $aFotoOrphans[] = array( 'name' => $entry , 'message' => SGL_String::translate('Orphan thumb ' . 'file succesfully deleted from directory')); } } } } closedir($handle); } } } $output->aFoto = $fArray; $output->aFotoErrors = $aFotoErrors; $output->aFotoOrphans = $aFotoOrphans; } /** * This will load the $soruceFileName, resize if necessary and * save as $destinatioFileName. * * @param string $srcFullFile full path file name * @param string $dstFile name of the destination file * without path. The path will be added * @param string $dstdir image processing directory * @param int $newWidth new (max) width of the image * @param int $newHeight new (max) height of the image * @return mixed true - success with image resize, * false - success without image resize, * PEAR_Error on error * * @access public */ function processImage($srcFullFile, $dstFile, $dstDir, $newWidth, $newHeight) { require_once 'Image/Transform.php'; $imageDriver = $this->conf['imageUpload']['imageDriver']; //if we don't have images/gallery directory, lets create it if (!file_exists($dstDir)){ include_once 'System.php'; $success = System::mkDir(array ($dstDir)); if (!$success) { return SGL::raiseError('The image upload directory does not ' . 'appear to be writable, please give the webserver ' . 'permissions to write to it', SGL_ERROR_FILEUNWRITABLE, PEAR_ERROR_DIE); } } // now lets check if they are writable... if (!is_writable($dstDir)) { return SGL::raiseError('The image or thumb upload directory does ' . 'not appear to be writable, please give the webserver ' . 'permissions to write to it', SGL_ERROR_FILEUNWRITABLE, PEAR_ERROR_DIE); } $im = Image_Transform::factory($imageDriver); if (is_a($im, 'PEAR_Error')) { return $im; } // // Process image // $ret = $im->load($srcFullFile); if(is_a($ret, 'PEAR_Error')) { return $im; } $size = $im->getImageSize(); if (isset($size['0']) && isset($size['1'])) { $width = $size['0']; $height = $size['1']; } else { return SGL::raiseError('Unable to get image size'); } $wasModified = false; if ($this->conf['imageUpload']['forceSize'] == 0) { if ($newWidth < $newHeight) { $maximum = $newWidth; } else { $maximum = $newHeight; } // scale only if necessary if ($width < $height && $height != $maximum) { $ret = $im->scale($maximum); $wasModified = true; } elseif ($width != $maximum) { $ret = $im->scale($maximum); $wasModified = true; } } else { // resize only if necessary if ($width != $newWidth || $height != $newHeight) { $ret = $im->resize($newWidth, $newHeight); } } if (is_a($ret, 'PEAR_Error')) { return $im; } // save if the file was modified or the file name is modified if ($wasModified || $dstDir.'/'.$dstFile != $srcFullFile) { $ret = $im->save($dstDir.'/'.$dstFile,'jpeg'); if (is_a($ret, 'PEAR_Error')) { return $im; } return true; } return false; } } ?>