* @copyright 2005 by gERD Schaufelberger * @package wombat * @subpackage admin */ /** * Gallery manager * * @version 1.1.0 * @package wombat * @subpackage Admin */ class wbAdminApp_Gallery extends wbAdminApp { /** * default values for request variables * @access protected * @var array $_requestDefaults * @see $_request */ var $_requestDefaults = array( 'action' => '_no_action_set', 'goto' => 'first', ); /** * name of this application * @var string $_appName */ var $_appName = 'Gallery'; /** * gallery entry * @var array $_gallery */ var $_gallery = array(); /** * wbImage object * @var array $_im */ var $_im; /** * wbDatasource object * @var array $_ds */ var $_ds; /** * image size * @var string $_imageSize */ var $_imageSize = 'thumb'; /** * upload directory * @var string $_uploadDir */ var $_uploadDir; /** * content folder in htdoc * @var string $_contentDir */ var $_contentDir; /** * upload directory of wbImage * @var string $_imageDir */ var $_imageDir; /** * initilaize clas * * @access public */ function __construct() { $this->_tmpl =& wbFactory::singleton( 'patTemplate' ); $this->_ds =& wbFactory::singleton( 'wbDatasource' ); $this->_ds->setCallback( $this ); $this->_ds->setOptions( array( // 'gallery' => array( 'limit' => 20 ), 'gallery_img' => array( 'limit' => 20 ) ) ); $this->_uploadDir = wbFactory::getParam( 'baseDir' ) . '/' . wbFactory::getParam( 'varDir' ) . '/upload/gallery'; $this->_imageDir = wbFactory::getParam( 'baseDir' ) . '/' . wbFactory::getParam( 'varDir' ) . '/upload/image/gal'; $this->_contentDir = wbFactory::getParam( 'baseDir' ) . '/' . wbFactory::getParam( 'htdocDir' ) . '/img/cnt/gal'; } /** * constructor wrapper for PHP4 * * @access public * @see __construct() */ function wbAdminApp_Gallery() { $this->__construct(); } /** * run admin interface * * @access public * @return boolean true on success */ function process( $path ) { // find gallery, if there is one... if( count( $path ) > 1 ) { $gallery = array_shift( $path ); $clause = array( array( 'field' => 'gallery', 'value' => $gallery ) ); $entry = $this->_ds->getEntry( 'gallery', null, null, $clause ); if( patErrorManager::isError( $entry ) ) { return $entry; } if( !is_array( $entry ) ) { wbDebugger::addMsg( 'Gallery', 'Recieved undefined gallery name: "' . $gallery . '"', 'Process' ); } else { $this->_gallery = $entry; $this->_tmpl->addGlobalVars( $this->_gallery, 'GALLERY_' ); } } // get action $action = 'List'; if( !empty( $path ) ) { $action = array_shift( $path ); } $function = '_processGallery' . $action; // gallery selected if( !empty( $this->_gallery ) ) { $function = '_processPicture' . $action; $this->_tmpl->addGlobalVars( $this->_gallery, 'gallery_' ); } if( !method_exists( $this, $function ) ) { return $this->_processGalleryList(); } return $this->$function(); return true; } /** * recieve submenu items * * Submenu contains all blogs * * @access public * @return mixed $submenu */ function getMenu() { $menu = array(); $entries = $this->_ds->getEntries( 'gallery' ); for( $i = 0; $i < count( $entries ); ++$i ) { $m = array( 'name' => $entries[$i]['gallery'], 'app' => $this->_appName . '/' . $entries[$i]['gallery'] . '/List', 'title' => $entries[$i]['title'], 'brief' => $entries[$i]['brief'], ); array_push( $menu, $m ); } // don't show emoty submenu if( empty( $menu ) ) { $m = array( 'name' => 'Add', 'app' => $this->_appName . '/Add', 'title' => 'New Gallery', 'brief' => 'Create a new gallery', ); array_push( $menu, $m ); } return $menu; } /** * display single picturein fullscreen mode * * @access private * @return boolean true on success */ function _processPictureView( ) { $id = $this->_request['id']; $this->_im =& wbFactory::singleton( 'wbImage' ); $this->_im->setBaseDir( 'gal/' . $this->_gallery['gallery'] ); $this->_imageSize = 'web'; $entry = $this->_ds->getEntry( 'gallery_img', $id ); if( patErrorManager::isError( $entry ) ) { return $entry; } if( !is_array( $entry ) ) { $this->_imageSize = 'thumb'; return $this->_processPictureList(); } $this->_fullScreen = true; $this->_loadTemplates( 'fullscreen', false ); $this->_loadTemplates( 'picture_view' ); $this->_tmpl->addVars( 'wbAdminApp_Gallery', $entry ); $html = $this->_tmpl->getParsedTemplate( 'wbAdminApp_Gallery' ); $this->_tmpl->addVar( 'page', 'content', $html ); return true; } /** * display picture list for selected gallery * * @access private * @return boolean true on success */ function _processPictureList() { $this->_im =& wbFactory::singleton( 'wbImage' ); $this->_im->setBaseDir( 'gal/' . $this->_gallery['gallery'] ); $this->_loadTemplates( 'pager', false ); $this->_loadTemplates( 'submit', false ); $this->_loadTemplates( 'formErrors', false ); // add pager info $pager = $this->_ds->getPager( 'gallery_img', $this->_request['goto'], $this->_gallery['id'] ); if( patErrorManager::isError( $pager ) ) { return $pager; } $pager['app'] = 'Gallery/' . $this->_gallery['gallery'] . '/List'; $this->_tmpl->addGlobalVars( $pager, 'pager_' ); $offset = $pager['offset'] * $pager['limit']; $list = $this->_ds->getEntries( 'gallery_img', $this->_gallery['id'], null, $offset ); // create form for $formPrototype = array( 'caption' => array( 'type' => 'String', 'attributes' => array( 'required' => 'no', 'display' => 'yes', 'edit' => 'yes', 'label' => _( 'Caption' ), 'title' => _( 'Caption' ), ) ), 'save' => array( 'type' => 'Switch', 'attributes' => array( 'required' => 'no', 'label' => _( 'Save caption?' ), 'title' => _( 'Save caption?' ), 'value' => 'yes', ) ) ); wbFactory::includeClass( 'patForms' ); $form =& patForms::createForm(); for( $i = 0; $i < count( $list ); ++$i ) { foreach( $formPrototype as $name => $prototype ) { $el =& $form->createElement( $name . '_' . $list[$i]['id'], $prototype['type'], $prototype['attributes'] ); if( $this->_request['action'] != 'save' ) { $el->setValue( $list[$i]['caption'] ); } $form->addElement( $el ); } } // save captions? if( $this->_request['action'] == 'save' ) { $saved = 0; $form->setSubmitted( true ); if( $form->validateForm() ) { $data = $form->getValues(); foreach( $data as $key => $value ) { list( $save, $id ) = explode( '_', $key, 2 ); // search data record if( $save != 'save' ) { continue; } if( $value != 'yes' ) { // set value as stored in databse foreach( $list as $l ) { if( $id == $l['id'] ) { $el =& $form->getElementByName( 'caption_' . $id ); $el->setValue( $l['caption'] ); break; } } continue; } // save value from submitted form $caption = array( 'caption' => $data['caption_' . $id] ); $el =& $form->getElementByName( 'save_' . $id ); $el->setValue( '' ); $this->_ds->save( 'gallery_img', $id, $caption ); ++$saved; } } if( $saved ) { $this->_addMsg( _( 'Saved {COUNT} picuture captions!' ), array( 'count' => $saved ) ); } else { $this->_addMsg( _( 'No captions saved. Please tick "Save" for each caption to be saved.' ) ); } } $renderer =& patForms::createRenderer( 'Array' ); $form->setRenderer( $renderer ); $elements = $form->renderForm(); $j = 0; for( $i = 0; $i < count( $list ); ++$i ) { $list[$i]['element_caption'] = $elements[$j++]['element']; $list[$i]['element_save'] = $elements[$j++]['element']; } $this->_loadTemplates( 'picture_list' ); $this->_tmpl->addRows( 'picture_entry', $list ); return true; } /** * add images * * @access private * @return boolean true on success */ function _processPictureAdd() { $uploadDir = $this->_uploadDir . '/' . $this->_gallery['gallery']; // start form $params = array( //'tmpl' => $this->_tmpl, 'template' => 'picture_form', 'template_dir' => $this->_appName, 'base_dir' => wbFactory::getParam( 'systemDir' ) . '/admin/templates' ); $form =& wbFactory::singleton( 'patForms', $params ); $el =& $form->getElement( 'upload' ); $el->setAttribute( 'uploaddir', $uploadDir ); if( $this->_request['action'] == 'save' ) { $form->setSubmitted( true ); if( $form->validateForm() ) { $data = $form->getValues(); $uploads = array(); $ext = array_pop( explode( '.', $data['upload'] ) ); switch( $ext ) { case 'zip': $tmpDir = $this->_unzipUpload( $data['upload'], $uploads ); $this->_addMsg( _( 'Found {COUNT} files in zip archive' ), array( 'count' => count( $uploads ) ) ); break; default: array_push( $uploads, $data['upload'] ); break; } // add each single image foreach( $uploads as $upload ) { // add images $file = $this->_addUploadFile( $upload ); if( patErrorManager::isError( $file ) ) { return $file; } if( !$file ) { $this->_addMsg( _( 'Skipped file of unknown type: "{FILE}"!' ), array( 'file' => $upload ) ); } $new = array(); $new['file'] = $file; $new['caption'] = $data['caption']; $foreign = $this->_ds->getPrimaryKey( 'gallery' ); $new[$foreign] = $this->_gallery['id']; $this->_ds->save( 'gallery_img', 'new', $new ); $this->_addMsg( _( 'Added picture "{PICTURE}"!' ), array( 'picture' => $upload ) ); } // cleanup switch( $ext ) { case 'zip': $this->_unlink( $tmpDir ); break; default: break; } } } $this->_tmpl->addGlobalVar( 'MAX_FILE_SIZE', ini_get( 'upload_max_filesize' ) ); $this->_loadTemplates( 'submit', false ); $form->renderForm( array( 'template' => 'form', 'errorTemplateContainer' => 'formErrors', 'errorTemplate' => 'formErrors_entry' ) ); // $this->_tmpl->dump(); return true; } /** * remove images * * @access private * @return boolean true on success */ function _processPictureDelete() { $id = $this->_request['id']; $this->_im =& wbFactory::singleton( 'wbImage' ); $this->_im->setBaseDir( 'gal/' . $this->_gallery['gallery'] ); if( $this->_request['action'] != 'force' ) { $data = $this->_ds->getEntry( 'gallery_img', $id ); if( patErrorManager::isError( $data ) ) { return $data; } if( !is_array( $data ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processPictureList(); } $this->_loadTemplates( 'picture_delete' ); $this->_tmpl->addVars( 'wbAdminApp_Gallery', $data ); return true; } // delete for real if( !$this->_checkReload() ) { $data = $this->_ds->getEntry( 'gallery_img', $id ); if( patErrorManager::isError( $data ) ) { return $data; } if( !is_array( $data ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processGalleryList(); } // erase images $res = $this->_im->erase( $data['file'] ); if( patErrorManager::isError( $res ) ) { return $res; } $res = $this->_ds->delete( 'gallery_img', $id ); if( patErrorManager::isError( $res ) ) { return $res; } } $this->_addMsg( _( 'Picture removed.' ) ); return $this->_processPictureList(); } /** * display list of galleries * * @access private * @return boolean true on success */ function _processGalleryList( ) { $this->_loadTemplates( 'pager', false ); $pager = $this->_ds->getPager( 'gallery', $this->_request['goto'] ); if( patErrorManager::isError( $pager ) ) { return $pager; } $pager['app'] = 'Gallery'; $this->_tmpl->addGlobalVars( $pager, 'pager_' ); $offset = $pager['offset'] * $pager['limit']; $list = $this->_ds->getEntries( 'gallery', null, null, $offset ); $this->_loadTemplates( 'index' ); $this->_tmpl->addRows( 'gallery_entry', $list ); return true; } /** * view gallery details * * @access private * @return boolean true on success */ function _processGalleryView() { $id = $this->_request['id']; $data = array(); if( $id != 'new' ) { $data = $this->_ds->getEntry( 'gallery', $id ); if( patErrorManager::isError( $data ) ) { return $data; } if( !is_array( $data ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processGalleryList(); } } $this->_loadTemplates( 'gallery_view' ); $this->_tmpl->addVars( 'wbAdminApp_Gallery', $data ); return true; } /** * add new gallery * * @access private * @return boolean true on success */ function _processGalleryAdd() { $this->_request['id'] = 'new'; return $this->_processGalleryEdit(); } /** * edit gallery * * @access private * @return boolean true on success */ function _processGalleryEdit() { // check Id... $id = $this->_request['id']; $data = array(); if( $id != 'new' ) { $data = $this->_ds->getEntry( 'gallery', $id ); if( patErrorManager::isError( $data ) ) { return $data; } if( !is_array( $data ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processGalleryList(); } } $this->_tmpl->addGlobalVar( 'id', $id ); // start form $params = array( 'template' => 'gallery_form', 'template_dir' => $this->_appName, 'base_dir' => wbFactory::getParam( 'systemDir' ) . '/admin/templates' ); $form =& wbFactory::singleton( 'patForms', $params ); $el =& $form->getElement( 'gallery' ); if( $id == 'new') { $rule =& patForms::createRule( 'RegexReplace' ); $el->addRule( $rule, PATFORMS_RULE_BEFORE_VALIDATION ); } else { if( $id != 'new' ) { $el->setValue( $data['gallery'] ); } $el->setAttribute( 'edit', 'no' ); } if( $this->_request['action'] == 'save' ) { $form->setSubmitted( true ); if( $form->validateForm() ) { $data = $form->getValues(); // skip actions on reload if( !$this->_checkReload() ) { // create folder for new gallery if( $id == 'new' ) { $res = $this->_createGalleryFiles( $data['gallery'] ); if( patErrorManager::isError( $res ) ) { return $res; } } // save/update table $res = $this->_ds->save( 'gallery', $id, $data ); if( patErrorManager::isError( $res ) ) { return $res; } } // add messages if( $id == 'new' ) { $this->_addMsg( _( 'Gallery "{TITLE}" sucessfully created' ), $data ); } else { $this->_addMsg( _( 'Details of gallery "{TITLE}" saved.' ), $data ); } // display list if everything went smoothly return $this->_processGalleryList(); } } else { $form->setValues( $data ); } $this->_loadTemplates( 'submit', false ); $form->renderForm( array( 'template' => 'form', 'errorTemplateContainer' => 'formErrors', 'errorTemplate' => 'formErrors_entry' ) ); return true; } /** * remove gallery * * @access private * @return boolean true on success */ function _processGalleryDelete() { $id = $this->_request['id']; if( $this->_request['action'] != 'force' ) { $data = $this->_ds->getEntry( 'gallery', $id ); if( patErrorManager::isError( $data ) ) { return $data; } if( !is_array( $data ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processGalleryList(); } $this->_loadTemplates( 'gallery_delete' ); $this->_tmpl->addVars( 'wbAdminApp_Gallery', $data ); return true; } // delete for real if( !$this->_checkReload() ) { $data = $this->_ds->getEntry( 'gallery', $id ); if( patErrorManager::isError( $data ) ) { return $data; } if( !is_array( $data ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processGalleryList(); } $res = $this->_removeGalleryFiles( $data['gallery'] ); if( patErrorManager::isError( $res ) ) { return $res; } $res = $this->_ds->delete( 'gallery', $id ); if( patErrorManager::isError( $res ) ) { return $res; } } $this->_addMsg( _( 'Gallery and all associated images removed.' ) ); return $this->_processGalleryList(); } /** * create all necessary files for a gallery * * -create folders: upload, image and content * -insert dummy image for all sizes * * @access public * @param string $gallery * @return boolean true */ function _createGalleryFiles( $gallery ) { umask( 0000 ); $dirs = array( $this->_uploadDir, $this->_imageDir, $this->_contentDir ); foreach( $dirs as $dir ) { if( !is_dir( $dir ) ) { wbDebugger::addMsg( 'Module:Gallery', $dir, 'mkdir' ); if( !mkdir( $dir, 0777 ) ) { return patErrorManager::raiseError( 'wbAdminApp:Gallery:1', 'Could not create gallery files', 'Failed to create general folder: "' . $dir . '"' ); } } if( !is_dir( $dir . '/' . $gallery ) ) { wbDebugger::addMsg( 'Module:Gallery', $dir . '/' . $gallery, 'mkdir' ); if( !mkdir( $dir . '/' . $gallery, 0777 ) ) { return patErrorManager::raiseError( 'wbAdminApp:Gallery:1', 'Could not create gallery files', 'Failed to create gallery folder: "' . $dir . '/' . $gallery . '"' ); } } } // copy "noImage.gif" to upload-folder and resize it // $src = $this->_contentDir . '/noImage.gif'; $src = wbFactory::getParam( 'systemDir' ) . '/' . wbFactory::getParam( 'adminDir' ) . '/resource/gallery/noImage.gif'; $des = $this->_imageDir . '/' . $gallery . '/noImage.gif'; wbDebugger::addMsg( 'Module:Gallery', 'Copy fallback image from "'.$src.'" to "'.$des.'"', 'noImage' ); copy( $src, $des ); $this->_im =& wbFactory::singleton( 'wbImage' ); $this->_im->setBaseDir( 'gal/' . $gallery ); return $this->_im->resize( 'noImage.gif' ); } /** * destroy gallery files * * @access public * @param string $gallery * @return boolean true */ function _removeGalleryFiles( $gallery ) { // wipe out all photos... $dirs = array( $this->_uploadDir, $this->_imageDir, $this->_contentDir ); foreach( $dirs as $dir) { if( !is_dir( $dir . '/' . $gallery ) ) { continue; } wbDebugger::addMsg( 'Module:Gallery', 'Delete picture folder "'. $dir . '/' . $gallery .'"', 'Remove' ); $res = $this->_removeDirRecursive( $dir . '/' . $gallery ); if( patErrorManager::isError( $res ) ) { return $res; } } return true; } /** * unzip uploaded file * * @access public * @param string $src * @return boolean true */ function _unzipUpload( $src, &$files ) { wbDebugger::addMsg( 'Module:Gallery', 'Unzip upload file: ' . $src, 'Unzip' ); $srcFile = $this->_uploadDir . '/' . $this->_gallery['gallery'] . '/' . $src; $unpackDir = tempnam( $this->_uploadDir . '/' . $this->_gallery['gallery'], 'upload_' ); unlink( $unpackDir ); $unpackDirName = substr( $unpackDir, strlen( $this->_uploadDir . '/' . $this->_gallery['gallery'] ) ); wbDebugger::addMsg( 'Module:Gallery', 'TempDir: ' . $unpackDir, 'Unzip' ); if( !mkdir( $unpackDir, 0777 ) ) { return patErrorManager::raiseError( 'wbAdminApp:Gallery:x', 'Unzip failed', 'Could not create temporary directory "' . $unpackDir . '"' ); } if( function_exists( 'zip_open' ) ) { $this->_unzipPhp( $srcFile, $unpackDir, $unpackDirName, $files ); } else { $this->_unzipExec( $srcFile, $unpackDir, $unpackDirName, $files ); } unlink( $srcFile ); return $unpackDir; } function _unzipExec( $srcFile, $unpackDir, $unpackDirName, &$files ) { $cwd = getcwd(); chdir( $unpackDir ); exec( 'unzip ' . $srcFile, $out, $ret ); if( $ret ) { chdir( $cwd ); return false; } foreach( $out as $line ) { if( !preg_match( '|inflating:\s+(.*)$|i', $line, $match ) ) { continue; } // echo "
\n"; print_r( $match ); echo "
\n"; array_push( $files, $unpackDirName . '/' . $match[1] ); } return true; } function _unzipPhp( $srcFile, $unpackDir, $unpackDirName, &$files ) { $zip = zip_open( $srcFile ); if( !$zip ) { return patErrorManager::raiseError( 'wbAdminApp:Gallery:x', 'Unpack failed', 'Could not open zip file: "' . $srcFile . '"' ); } while( ( $entry = zip_read( $zip ) ) !== false ) { wbDebugger::addMsg( 'Module:Gallery', 'Zip Entry: ' . zip_entry_name( $entry ), 'Unzip' ); $filename = zip_entry_name( $entry ); if( substr( $filename, -1, 1 ) == '/' ) { mkdir( $unpackDir . '/' . $filename, 0777 ); continue; } if( zip_entry_open( $zip, $entry, 'r' ) ) { $unpackFile = fopen( $unpackDir . '/' . $filename, 'w' ); fwrite( $unpackFile, zip_entry_read( $entry, zip_entry_filesize( $entry ) ) ); fclose( $unpackFile ); zip_entry_close( $entry ); array_push( $files, $unpackDirName . '/' . $filename ); } } zip_close( $zip ); return true; } /** * add uploaded images * * @access public * @param string $src * @return destination file name if everything went allright or false filetype does not match */ function _addUploadFile( $src ) { wbDebugger::addMsg( 'Module:Gallery', 'add upload file: ' . $src, 'Upload' ); $this->_im =& wbFactory::singleton( 'wbImage' ); $this->_im->setBaseDir( 'gal/' . $this->_gallery['gallery'] ); $srcFile = $this->_uploadDir . '/' . $this->_gallery['gallery'] . '/' . $src; $des = strtolower( basename( $src ) ); // file type guessing by extension // use pecl-fileinfo but, I don't have it now... $ext = array_pop( explode( '.', $des ) ); switch( strtolower( $ext ) ) { case 'png': case 'gif': case 'jpeg': case 'jpg': // make filename unique $foreign = $this->_ds->getPrimaryKey( 'gallery' ); $data = array( 'file' => $des, $foreign => $this->_gallery[$foreign], ); // resolve dublicates $dups = $this->_ds->getDuplicates( 'gallery_img', 'new', $data ); if( !empty( $dups ) ) { $name = explode( '.', $des ); $ext = array_pop( $name ); $name = implode( '.', $name ) . '_%03d'; $no = 0; while( !empty( $dups ) ) { ++$no; $des = sprintf( $name, $no ) . '.' . $ext; $data['file'] = $des; $dups = $this->_ds->getDuplicates( 'gallery_img', 'new', $data ); } } wbDebugger::addMsg( 'Module:Gallery', 'Add image "'. $des .'"', 'Upload' ); $desFile = $this->_imageDir . '/' . $this->_gallery['gallery'] . '/' . $des; rename( $srcFile, $desFile ); chmod( $desFile, 0666 ); $this->_addUploadImage( $des ); break; case 'zip': // not implemented yet wbDebugger::addMsg( 'Module:Gallery', 'uploading zip files not implemented', 'Upload' ); return false; break; default: // gracefully tell that I cannot add this file type return false; return patErrorManager::raiseError( 'wbAdminApp:Gallery:2', 'Wrong file uploaded', 'Cannot process file of filetype "' . $type . '"' ); break; } return $des; } /** * add image to gallery * * @access public * @param string $src * @return boolean true */ function _addUploadImage( $img ) { return $this->_im->resize( $img ); } /** * remove directory recursive * * @access private * @param string $dir * @return boolean true */ function _unlink( $dir ) { if( !is_dir( $dir ) ) { return false; } $dh = dir( $dir ); while( false !== ( $file = $dh->read() ) ) { //skip hidden files and dirs if( $file[0] == '.') { continue; } if( is_dir( $dir . '/' . $file ) ) { $this->_unlink( $dir . '/' . $file ); continue; } unlink( $dir . '/' . $file ); } $dh->close(); rmdir( $dir ); return true; } /** * callback function * * @access public * @param string $table * @param array $data * @param int $id * @return boolean true */ function callForGetentries( $table, &$data, $id ) { $data['id'] = $id; if( $table != 'gallery_img' ) { return true; } $info = $this->_im->getInfo( $data['file'], $this->_imageSize ); foreach( $info as $key => $value ) { $data['img_' . $key] = $value; } return true; } /** * callback function * * @access public * @param string $table * @param array $data * @param int $id * @return boolean true */ function callForGetentry( $table, &$data, $id ) { $data['id'] = $id; if( $table != 'gallery_img' ) { return true; } $info = $this->_im->getInfo( $data['file'], $this->_imageSize ); foreach( $info as $key => $value ) { $data['img_' . $key] = $value; } return true; } /** * callback function * * @access public * @param string $table * @param array $data * @param int $id * @return boolean true */ function callForSave( $table, &$data, $id ) { switch( $table ) { case 'gallery': if( $id == 'new' ) { $data['created'] = date( 'Y-m-d H:i:s' ); } else { // don't allow to change gallery names! unset( $data['gallery'] ); } $data['changed'] = date( 'Y-m-d H:i:s' ); break; case 'gallery_img': if( $id == 'new' ) { $data['created'] = date( 'Y-m-d H:i:s' ); $data['flags'] = 'enabled'; } break; } return true; } /** * like "rm -rf" * * @param string $dir directory name * @return boolean $result true on success */ function _removeDirRecursive( $dir ) { $dh = dir( $dir ); while( false !== ( $entry = $dh->read() ) ) { if( $entry == '.' || $entry == '..' ) { continue; } if( is_dir( $dir . '/' . $entry ) ) { $res = $this->_removeDirRecursive( $dir . '/' . $entry ); if( patErrorManager::isError( $res ) ) { return $res; } continue; } if( !unlink( $dir . '/' . $entry ) ) { return patErrorManager::raiseError( 'wbAdminApp:Gallery:2', 'Could not remove file', 'Failed to delete file: "' . $dir . '/' . $entry . '"' ); } } if( !rmdir( $dir ) ) { return patErrorManager::raiseError( 'wbAdminApp:Gallery:3', 'Could not remove directory', 'Failed to delete directory: "' . $dir . '/' . $entry . '"' ); } return true; } } ?>