Thread: [pLog-CVS] plog_devel adminaddtemplateaction.class.php,NONE,1.1 admindeletetemplatesaction.class.php
Brought to you by:
jondaley
Update of /cvsroot/plog/plog_devel In directory sc8-pr-cvs1:/tmp/cvs-serv3954 Modified Files: admincontroller.class.php admincontrollermap.properties.php templateservice.class.php Added Files: adminaddtemplateaction.class.php admindeletetemplatesaction.class.php adminedittemplatesaction.class.php adminnewtemplateaction.class.php baseunpacker.class.php tarbz2unpacker.class.php targzunpacker.class.php zipunpacker.class.php Log Message: added classes to handle the removal of templates, add new and so forth --- NEW FILE: adminaddtemplateaction.class.php --- <?php include_once( "siteadminaction.class.php" ); include_once( "admintemplatedview.class.php" ); include_once( "adminerrorview.class.php" ); include_once( "unpacker.class.php" ); /** * Shows a form to add a new locale file */ class AdminAddTemplateAction extends SiteAdminAction { function AdminAddTemplateAction( $actionInfo, $request ) { $this->SiteAdminAction( $actionInfo, $request ); } function validate() { $this->_subOp = $this->_request->getValue( "subOp" ); if( $this->_subOp == "uploadTemplate" ) { // check that we have a file!! $files = HttpVars::getFiles(); if( count($files) == 0 ) { $this->_view = new AdminErrorView( $this->_blogInfo ); $this->_view->setValue( "You must upload a file."); $this->setCommonData(); return false; } } return true; } function _performUploadTemplate() { // unpack first the file to the temp folder $unpacker = new Unpacker(); $files = HttpVars::getFiles(); $uploads = new FileUploads( $files ); $config =& Config::getConfig(); $tmpFolder = $config->getValue( "temp_folder" ); // move it to the temporary folder $result = $uploads->process( $tmpFolder ); // and from there, unpack it $upload = new FileUpload( $files["templateFile"] ); $unpacker->unpack( $tmpFolder."/".$upload->getFileName(), $tmpFolder ); } function perform() { if( $this->_subOp == "uploadTemplate" ) $this->_performUploadTemplate(); } } ?> --- NEW FILE: admindeletetemplatesaction.class.php --- <?php include_once( "blogowneradminaction.class.php" ); include_once( "adminerrorview.class.php" ); include_once( "templateservice.class.php" ); /** * Removes a global template from disk. */ class AdminDeleteTemplatesAction extends SiteAdminAction { var $_templateIds; function AdminDeleteTemplatesAction( $actionInfo, $request ) { $this->SiteAdminAction( $actionInfo, $request ); } function validate() { $this->_templateIds = $this->_request->getValue( "deleteTemplate" ); if( count($this->_templateIds) == 0 ) { $this->_view = new AdminErrorView( $this->_blogInfo ); $this->_view->setMessage( "No templates were selected to remove." ); $this->setCommonData(); return false; } return true; } /** * Carries out the specified action */ function perform() { $ts = new TemplateService(); foreach( $this->_templateIds as $templateId ) { if( !$ts->removeTemplate( $templateId )) $message .= "There was an error removing the template \"$templateId\".<br/><br/>"; else $message .= "Template \"$templateId\" removed successfully.<br/><br/>"; } $this->_view = new AdminMessageView( $this->_blogInfo ); $this->_view->setMessage( $message ); $this->setCommonData(); return true; } } ?> --- NEW FILE: adminedittemplatesaction.class.php --- <?php include_once( "blogowneradminaction.class.php" ); include_once( "adminerrorview.class.php" ); include_once( "templateservice.class.php" ); class AdminEditTemplatesAction extends SiteAdminAction { function AdminEditTemplatesAction( $actionInfo, $request ) { $this->SiteAdminAction( $actionInfo, $request ); } /** * Carries out the specified action */ function perform() { $ts = new TemplateService(); $globalTemplates = $ts->getGlobalTemplates(); $this->_view = new AdminTemplatedView( $this->_blogInfo, "sitetemplates" ); $this->_view->setValue( "templates", $globalTemplates ); $this->setCommonData(); return true; } } ?> --- NEW FILE: adminnewtemplateaction.class.php --- <?php include_once( "siteadminaction.class.php" ); include_once( "admintemplatedview.class.php" ); /** * Shows a form to add a new locale file */ class AdminNewTemplateAction extends SiteAdminAction { function AdminNewLocaleAction( $actionInfo, $request ) { $this->SiteAdminAction( $actionInfo, $request ); } function perform() { $this->_view = new AdminTemplatedView( $this->_blogInfo, "newglobaltemplate" ); $this->setCommonData(); return true; } } ?> --- NEW FILE: baseunpacker.class.php --- <?php include_once( "object.class.php" ); class BaseUnpacker extends Object { function BaseUnpacker() { $this->Object(); } function unpack( $file, $destFolder ) { throw( new Exception( "This method must be implemented by child classes!" )); die(); } } ?> --- NEW FILE: tarbz2unpacker.class.php --- <?php include_once( "object.class.php" ); include_once( "baseunpacker.class.php" ); include_once( "config.class.php" ); // default paths where 'tar' and 'gzip' can be found... this should be true // for at least linux-based machines (this is where they are located in // my gentoo box) define( "DEFAULT_TAR_PATH", "/bin/tar" ); define( "DEFAULT_BZIP2_PATH", "/bin/bzip2" ); /** * Unpacks .tar.gz files. */ class TarBz2Unpacker extends BaseUnpacker { function TarBz2Unpacker() { $this->BaseUnpacker(); } function unpack( $file, $destFolder ) { // get the paths where tar and gz are $config =& Config::getConfig(); $tarPath = $config->getValue( "tar_path" ); if( $tarPath == "" ) $tarPath = DEFAULT_TAR_PATH; $bzip2Path = $config->getValue( "bzip2_path" ); if( $bzip2Path == "" ) $bzip2Path = DEFAULT_BZIP2_PATH; // and now build the command //$file = escapeshellarg($file); //$destFolder = escapeshellarg($destFolder); // // :DANGER: // what if the user voluntarily sets the path of gzip and tar // to something else? we are doing no checks here to make sure that // the user is giving us a valid commnand so... how could we make // sure that it'll work? // $cmd = "$bzip2Path -dc $file | $tarPath xv -C $destFolder"; //print("unpacking $file to $destFolder...<br/>"); //print("cmd = $cmd<br/>"); $result = exec( $cmd, &$output, &$retval ); return $retval; } } ?> --- NEW FILE: targzunpacker.class.php --- <?php include_once( "object.class.php" ); include_once( "baseunpacker.class.php" ); include_once( "config.class.php" ); // default paths where 'tar' and 'gzip' can be found... this should be true // for at least linux-based machines (this is where they are located in // my gentoo box) define( "DEFAULT_TAR_PATH", "/bin/tar" ); define( "DEFAULT_GZIP_PATH", "/bin/gzip" ); /** * Unpacks .tar.gz files. */ class TarGzUnpacker extends BaseUnpacker { function TarGzUnpacker() { $this->BaseUnpacker(); } function unpack( $file, $destFolder ) { // get the paths where tar and gz are $config =& Config::getConfig(); $tarPath = $config->getValue( "tar_path" ); if( $tarPath == "" ) $tarPath = DEFAULT_TAR_PATH; $gzipPath = $config->getValue( "gzip_path" ); if( $gzipPath == "" ) $gzipPath = DEFAULT_GZIP_PATH; // and now build the command //$file = escapeshellarg($file); //$destFolder = escapeshellarg($destFolder); // // :DANGER: // what if the user voluntarily sets the path of gzip and tar // to something else? we are doing no checks here to make sure that // the user is giving us a valid commnand so... how could we make // sure that it'll work? // $cmd = "$gzipPath -dc $file | $tarPath xv -C $destFolder"; print("unpacking $file to $destFolder...<br/>"); print("cmd = $cmd<br/>"); $result = exec( $cmd, &$output, &$retval ); return $retval; } } ?> --- NEW FILE: zipunpacker.class.php --- <?php include_once( "object.class.php" ); include_once( "baseunpacker.class.php" ); class ZipUnpacker extends BaseUnpacker { function ZipUnpacker() { $this->BaseUnpacker(); } function unpack( $file, $destFolder ) { } } ?> Index: admincontroller.class.php =================================================================== RCS file: /cvsroot/plog/plog_devel/admincontroller.class.php,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** admincontroller.class.php 23 Sep 2003 21:47:01 -0000 1.26 --- admincontroller.class.php 30 Sep 2003 21:29:26 -0000 1.27 *************** *** 68,71 **** --- 68,75 ---- include_once( "adminnewlocaleaction.class.php" ); include_once( "adminaddlocaleaction.class.php" ); + include_once( "adminedittemplatesaction.class.php" ); + include_once( "admindeletetemplatesaction.class.php" ); + include_once( "adminnewtemplateaction.class.php" ); + include_once( "adminaddtemplateaction.class.php" ); /** Index: admincontrollermap.properties.php =================================================================== RCS file: /cvsroot/plog/plog_devel/admincontrollermap.properties.php,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** admincontrollermap.properties.php 23 Sep 2003 21:47:01 -0000 1.26 --- admincontrollermap.properties.php 30 Sep 2003 21:29:26 -0000 1.27 *************** *** 140,142 **** --- 140,150 ---- // adds a new locale to the server $actions["addLocale"] = "AdminAddLocaleAction"; + // shows a list of the locales available + $actions["templates"] = "AdminEditTemplatesAction"; + // deletes templates + $actions["deleteTemplates"] = "AdminDeleteTemplatesAction"; + // shows the form to add a new template + $actions["newTemplate"] = "AdminNewTemplateAction"; + // adds a new template to the system + $actions["addTemplate"] = "AdminAddTemplateAction"; ?> Index: templateservice.class.php =================================================================== RCS file: /cvsroot/plog/plog_devel/templateservice.class.php,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** templateservice.class.php 23 Jul 2003 16:25:26 -0000 1.5 --- templateservice.class.php 30 Sep 2003 21:29:26 -0000 1.6 *************** *** 5,8 **** --- 5,12 ---- include_once( "config.class.php" ); include_once( "blogsettings.class.php" ); + include_once( "blogs.class.php" ); + include_once( "glob.class.php" ); + + define( "BLOG_BASE_TEMPLATE_FOLDER", "blog_" ); /** *************** *** 52,55 **** --- 56,192 ---- return $t; + } + + /** + * Returns an array that contains the name of all the 'global' templates + * available. + * + * @return Returns an array of strings + */ + function getGlobalTemplates() + { + $config =& Config::getConfig(); + + return $config->getValue( "templates" ); + } + + /** + * Returns a list with all the custom templates available *only* + * to this blog. + * + * @return An array of string containing the identifiers of the template + * sets available for the given blog + */ + function getBlogTemplates( $blogId ) + { + $blogs = new Blogs(); + $blogSettings = $blogs->getBlogSettings( $blogId ); + + return $blogSettings->getValue( "blog_templates" ); + } + + /** + * Returns an array with all the global templates and the custom ones + * from the given blog. + * + * @return Array + * @see Config::getGlobalTemplates + * @see Config::getBlogTemplates + */ + function getAllTemplates( $blogId ) + { + $globalTemplates = $this->getGlobalTemplates(); + $blogTemplates = $this->getBlogTemplates( $blogId ); + + return $blogTemplates+$globalTemplates; + } + + /** + * Returns the path to where the given template should be. If blogId == 0, + * then the template will be considered global or else it will be considered + * as a custom template for the given blog. + * + * @return Returns a string with the path to the folder. + */ + function getTemplateFolder( $templateName, $blogId = 0 ) + { + $config =& Config::getConfig(); + $baseFolder = $config->getValue( "template_folder" ); + if( $baseFolder[strlen($baseFolder)-1] != "/" ) + $baseFolder = $baseFolder."/"; + + if( $blogId == 0 ) { + $templateFolder = $baseFolder.$templateName."/"; + } + else { + $templateFolder = $baseFolder.BLOG_BASE_TEMPLATE_FOLDER."/".$templateName."/"; + } + + return $templateFolder; + } + + /** + * @private + */ + function _removeGlobalTemplate( $templateName ) + { + $config =& Config::getConfig(); + + $templateFolder = $this->getTemplateFolder( $templateName ); + + // we need to get a list of the files that are inside that folder + $files = Glob::Glob( $templateFolder ); + + foreach( $files as $file ) { + File::delete( $file ); + } + + $result =File::delete( $templateFolder ); + + // remove the entry for that template from the configuration + $availableTemplates = $config->getValue( "templates" ); + + $newTemplateList = Array(); + foreach( $availableTemplates as $template ) { + if( $template != $templateName ) + array_push( $newTemplateList, $template ); + } + + $config->saveValue( "templates", $newTemplateList ); + + return $result; + } + + /** + * @private + */ + function _removeBlogTemplate( $templateName, $blogId ) + { + $config =& Config::getConfig(); + + $templateFolder = $this->getTemplateFolder( $templateName, $blogId ); + + print("removing $templateFolder<br/>"); + + return true; + } + + /** + * Removes a template from the templates folder. If blogId == 0, then we will + * look for it as a global template, or else we will look for the template + * in the folder of the blog. + * + * @return Returns true if removed successfully or false otherwise. + */ + function removeTemplate( $templateName, $blogId = 0 ) + { + if( $blogId == 0 ) { + $result = $this->_removeGlobalTemplate( $templateName ); + } + else { + $result = $this->_removeBlogTemplate( $templateName, $blogId ); + } + + return $result; } } |