From b90c61558a7ed2b1d3674074a4d1117130ad22ae Mon Sep 17 00:00:00 2001 From: Saophalkun Ponlu Date: Fri, 19 Feb 2010 03:36:29 +0000 Subject: [PATCH] FEATURE: added the ability to switch between two-step and three-step workflow --- _config.php | 3 + .../CMSWorkflowSiteConfigController.php | 28 +++ code/model/CMSWorkflowSiteConfigDecorator.php | 174 ++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 code/control/CMSWorkflowSiteConfigController.php create mode 100644 code/model/CMSWorkflowSiteConfigDecorator.php diff --git a/_config.php b/_config.php index 83e9059..9a46730 100644 --- a/_config.php +++ b/_config.php @@ -3,4 +3,7 @@ Object::add_extension('Member', 'FrameworkTestRole'); Object::add_extension('Member', 'FileUploadRole'); +Object::add_extension('SiteConfig', 'CMSWorkflowSiteConfigDecorator'); + +CMSWorkflowSiteConfigDecorator::apply_active_config(); ?> \ No newline at end of file diff --git a/code/control/CMSWorkflowSiteConfigController.php b/code/control/CMSWorkflowSiteConfigController.php new file mode 100644 index 0000000..98d8ed9 --- /dev/null +++ b/code/control/CMSWorkflowSiteConfigController.php @@ -0,0 +1,28 @@ +CMS Workflow has been set to $whichStep

"; + + // rebuild the database schema + Director::redirect("dev/build?flush=1"); + + return true; + } + + +} \ No newline at end of file diff --git a/code/model/CMSWorkflowSiteConfigDecorator.php b/code/model/CMSWorkflowSiteConfigDecorator.php new file mode 100644 index 0000000..ad71c8b --- /dev/null +++ b/code/model/CMSWorkflowSiteConfigDecorator.php @@ -0,0 +1,174 @@ + + * STEP = threestep + * OTHERCONFIG = value + * ... + * + * + * The STEP config can be either twostep or threestep where twostep is the default + * + */ +class CMSWorkflowSiteConfigDecorator extends DataObjectDecorator { + + static $step_config = "twostep" ; + + static $default_config = "STEP = twostep"; + + /** + * @TODO: add javascript that instantly appends "(active)" the links - "Set to Two-Step" and "Set to Three-Step" + */ + function updateEditFormFields(&$fields) { + $twoStepActive = ""; + $threeStepActive = ""; + $whichStep = self::get_step_config(); + + if($whichStep == 'threestep') { + $threeStepActive = "(active)"; + } + else { + $twoStepActive = "(active)"; + } + + $fields->addFieldToTab("Root.Main", new HeaderField("WorkflowHeader", "CMS Workflow Configuration")); + $fields->addFieldToTab("Root.Main", new LiteralField("WorkflowTwoStepLink", '

Set to Two Step ' . $twoStepActive . '

')); + $fields->addFieldToTab("Root.Main", new LiteralField("WorkflowThreeStepLink", '

Set to Three Step ' . $threeStepActive . '

')); + } + + /** + * Apply two step (config) or three step workflow config. + * Two step config is already set in the module so SiteConfig::WorkflowStepConfig is twostep we don't need to do anything + */ + static function apply_active_config() { + $whichStep = self::get_step_config(); + + if($whichStep == 'threestep') { + // remove two-step decorators + Object::remove_extension('WorkflowRequest', 'WorkflowTwoStepRequest'); + Object::remove_extension('SiteTree', 'SiteTreeCMSTwoStepWorkflow'); + Object::remove_extension('SiteConfig', 'SiteConfigTwoStepWorkflow'); + + // add three-step decorators + Object::add_extension('WorkflowRequest', 'WorkflowThreeStepRequest'); + Object::add_extension('SiteTree', 'SiteTreeCMSThreeStepWorkflow'); + Object::add_extension('LeftAndMain', 'LeftAndMainCMSThreeStepWorkflow'); + Object::add_extension('SiteConfig', 'SiteConfigThreeStepWorkflow'); + } + + } + + /** + * Get STEP config variable from file self::get_config_file_path() + * @return string | the value of the config variable + */ + static function get_step_config() { + $config = self::get_config_content(); + $value = self::get_config($config, "STEP"); + + return $value; + } + + /** + * Set Get STEP config varial from file self::get_config_file_path() + */ + static function set_step_config($val) { + $config = self::get_config_content(); + $newContent = self::set_config($config, "STEP", $val); + self::write_config_file($newContent); + } + + /** + * Config file path for frameworktest - two step and three step testing + */ + static function get_config_file_path() { + return dirname(__FILE__) . "/CMSWorkflow_Config.cfg"; + } + + /** + * Config file path for frameworktest - two step and three step testing + * @return file handler + */ + static function get_config_file($mode = "r+") { + $path = self::get_config_file_path(); + + // create if not existing + if(!file_exists($path)) { + $file = fopen($path, "w"); + // default config + fwrite($file, self::$default_config); + fclose($file); + } + + return fopen($path, $mode); + } + + /** + * Config file path for frameworktest - two step and three step testing + * @return file handler + */ + static function write_config_file($content) { + $file = self::get_config_file(); + + // clear the file first + ftruncate($file, 0); + + fwrite($file, $content); + fclose($file); + } + + /** + * Config file path for frameworktest - two step and three step testing + * @return string + */ + static function get_config_content() { + $file = self::get_config_file(); + $size = filesize(self::get_config_file_path()); + if($size < 1) $size = 1; + $content = fread($file, $size); + fclose($file); + return $content; + } + + /** + * + * @param content from the file return from self::get_config_file + * @param config variable + */ + static function get_config($content, $var) { + $pattern = "/$var\s*=\s*([\w_]*)/"; + preg_match($pattern, $content, $matches); + + if(count($matches) < 2) return false; + + return $matches[1]; + } + + /** + * @param content from the file return from self::get_config_file + * @param config variable + * @param value of the config variable + * @return string | new content + */ + static function set_config($content, $var, $value) { + $pattern = "/$var\s*=\s*([\w_]*)/"; + $replaced = preg_replace($pattern, "$var = $value", $content); + + // if there varial is not found in the config a new a new one + if(preg_match($pattern, $content) == 0) { + if (trim($replaced) == "") { + $replaced = self::$default_config; + } + else { + $replaced = trim($replaced) . "\n" . self::$default_config; + } + } + return $replaced; + } + + +} \ No newline at end of file