**/ class twIf_Generic_Editor extends twIf_Generic_Listing { // The default link we output for creating new objects const CREATE_LINK = 'create'; // Captions for editor and creator const EDITOR_CAPTION = 'Modify element'; const EDITOR_SUBMIT_CAPTION = 'Save'; const CREATOR_CAPTION = 'Create new element'; const CREATOR_SUBMIT_CAPTION = 'Create'; // Force strict mode when creating objects (means all constructor-parameters have to be objects or defined in the editor) const CREATE_STRICT = true; // Allow the user to create new objects const ALLOW_EDIT = true; const ALLOW_CREATE = true; const ALLOW_FEEDBACK = true; // Switch automatically to creator when no items are configured const AUTO_CREATE = false; // Close the editor automatically after a successfull save const EDITOR_AUTO_CLOSE = false; private $Item = null; private $Create = false; private $forceListing = false; // {{{ __construct /** * Create a new Editor Interface * * @param array $URL * @param string $Base (optional) * @param enum $urlMode (optional) * * @access friendly * @return void */ function __construct ($URL, $Base = '', $urlMode = self::MODE_URL) { // Load our classname $Class = $this->getClass (); $this->urlMode = $urlMode; parent::__construct ($URL, $Base, $urlMode); // Determine our current position switch ($this->urlMode) { case self::MODE_URL: if ((count ($URL) == 0) || ($URL [0] == '')) break; // Check wehter to create a new object if ($URL [0] == self::CREATE_LINK) $this->Create = true; // Check wheter there is an object-pointer elseif ($Class && $this->getAllowEdit ()) $this->setItem (call_user_func (array ($Class, 'getInstance'), $URL [0], twIf_Object::FLAG_ID_MD5)); break; case self::MODE_PARAMETER: if (!isset ($_GET ['twIfGenEditOp'])) break; // Check wehter to create a new object if ($_GET ['twIfGenEditOp'] == self::CREATE_LINK) $this->Create = true; elseif ($Class && $this->getAllowEdit ()) { $this->setItem (call_user_func (array ($Class, 'getInstance'), $_GET ['twIfGenEditOp'], twIf_Object::FLAG_ID_MD5)); } break; } } // }}} // {{{ getAllowEdit /** * Check if we may edit elements * * @access protected * @return bool */ protected function getAllowEdit () { return self::findConstant ('ALLOW_EDIT', true); } // }}} // {{{ getAllowCreate /** * Check if we may create elements * * @access protected * @return bool */ protected function getAllowCreate () { return self::findConstant ('ALLOW_CREATE', true); } // }}} // {{{ getEditorCaption /** * Get the caption for editor-page * * @access protected * @return string */ protected function getEditorCaption () { return self::findConstant ('EDITOR_CAPTION', 'Modify element'); } // }}} // {{{ getCreateCaption /** * Get the caption for new object-creation * @access protected * @return string */ protected function getCreateCaption () { return self::findConstant ('CREATOR_CAPTION', 'Create new element'); } // }}} // {{{ getEditorFields /** * Get field-definition for editor * * @access protected * @return array */ protected function getEditorFields () { return array (); } // }}} // {{{ getCreateClass /** * Retrive the name of the class we use to create an item * * @access public * @return string **/ protected function getCreateClass () { if (($rc = self::findConstant ('CREATE_CLASSNAME', null)) !== null) return $rc; return $this->getClass (); } // }}} // {{{ getCreateFields /** * @access protected * @return array */ protected function getCreateFields () { return $this->getEditorFields (); } // }}} // {{{ getCreateParams /** * Determine which params pass to constructor of our class * * @access protected * @return array */ protected function getCreateParams () { return array (); } // }}} // {{{ getCreateStrict /** * Check wheter to parse Create-parameters in strict mode * * @access protected * @return bool **/ protected function getCreateStrict () { return self::findConstant ('CREATE_STRICT', true); } // }}} // {{{ getAutoCreate /** * Automatically switch to creator-mode when not items are found * * @access protected * @return bool **/ protected function getAutoCreate () { return self::findConstant ('AUTO_CREATE', false); } // }}} // {{{ setItem /** * Go into editor-mode * * @param object $Item * * @access protected * @return bool **/ protected function setItem ($Item) { if (!is_object ($Item)) return false; $this->Item = $Item; $this->Create = false; return true; } // }}} // {{{ getItem /** * Retrive the selected item * * @access public * @return object */ public function getItem () { return $this->Item; } // }}} // {{{ onEditor /** * Check wheter we are displaying the editor * * @access public * @return bool */ public function onEditor () { return is_object ($this->getItem ()); } // }}} // {{{ onCreator /** * Check wheter we are creating a new item right now * * @access public * @return bool **/ public function onCreator () { return $this->Create; } // }}} // {{{ onListing /** * Check wheter we are on the listing-page at the moment * * @access public * @return bool **/ public function onListing () { return !$this->onEditor () && !$this->onCreator (); } // }}} // {{{ giveFeedback /** * Determine wheter the editor-widget should output a feedback or not * * @access protected * @return bool **/ protected function giveFeedback () { return self::findConstant ('ALLOW_FEEDBACK', true); } // }}} // {{{ getMessageSaved /** * Which message to output when an object was saved * * @access protected * @return string **/ protected function getMessageSaved () { return self::findConstant ('MSG_SAVED', self::MSG_SAVED); } // }}} // {{{ getMessageSaveFailed /** * Which message to output whenver save failed * * @access protected * @return string **/ protected function getMessageSaveFailed () { return self::findConstant ('MSG_SAVE_FAILED', self::MSG_SAVE_FAILED); } // }}} // {{{ getMessageNoChanges /** * Which message to output whenever no changes where made * * @access protected * @return string **/ protected function getMessageNoChanges () { return self::findConstant ('MSG_NO_CHANGES', self::MSG_NO_CHANGES); } // }}} // {{{ getDefintion /** * Retrive Page-Definition from this object * * @access public * @return array */ public function getDefinition () { if ($this->getAutoCreate () && ($this->countItems () == 0)) $this->Create = true; if ((($this->getAllowEdit () && is_object ($this->Item)) || $this->Create) && !$this->forceListing) return array ( // Caption of this section array ('Type' => 'Caption', 'Text' => ($this->Create ? $this->getCreateCaption () : $this->getEditorCaption ())), // The Editor array ( 'Type' => 'Editor', 'Mode' => ($this->Create ? 'new' : ''), 'Source' => ($this->Create ? $this->getCreateClass () : array ($this, 'getItem')), 'Fields' => array ($this, ($this->Create ? 'getCreateFields' : 'getEditorFields')), 'Strict' => $this->getCreateStrict (), 'SuccessURL' => parent::getBase (), 'onSuccess' => array ($this, ($this->Create ? 'callbackCreate' : 'callbackUpdate')), 'Constructor' => array ($this, 'getCreateParams'), 'Feedback' => $this->giveFeedback (), 'msgSaved' => $this->getMessageSaved (), 'msgSaveFailed' => $this->getMessageSaveFailed (), 'msgNoChanges' => $this->getMessageNoChanges (), 'msgSubmit' => $this->getSubmitCaption (), ), ); // Get listing-Definition from our parent $Def = parent::getDefinition (); // Append create-link if ($this->getAllowCreate ()) $Def [] = array ( 'Type' => 'Chooser', 'Items' => array ( $this->getCreateCaption () => $this->getCreatorURL (), ), ); return $Def; } // }}} // {{{ buildURL /** * Little hacky function to build URLs for us * * @param string $Param * * @access protected * @return string **/ protected function buildURL ($Param) { switch ($this->urlMode) { case self::MODE_URL: return self::getBase (true) . $Param; case self::MODE_PARAMETER: $Base = self::getBase (true); if (strpos ($Base, '?') === false) return $Base . '?twIfGenEditOp=' . $Param; return $Base . '&twIfGenEditOp=' . $Param; } } // }}} // {{{ getBase /** * Retrive assigned Base-URL * * @param bool $Minimal (optional) Do not append current item / section * @access public * @return string */ public function getBase ($Minimal = false) { if ($Minimal) return parent::getBase (); if (is_object ($this->Item)) return $this->getEditorURL (true); if ($this->Create) return $this->getCreatorURL (); return parent::getBase (); } // }}} // {{{ getOptions /** * Get Options for Listing * * @access protected * @return array */ protected function getOptions () { $myOptions = array (); if ($this->getAllowEdit ()) $myOptions ['Bearbeiten'] = $this->getEditorURL (false); return array_merge ($myOptions, parent::getOptions ()); } // }}} // {{{ getSubmitCaption /** * Retrive the caption of our submit-button * * @access protected * @return string **/ protected function getSubmitCaption () { if ($this->onEditor ()) return self::findConstant ('EDITOR_SUBMIT_CAPTION', 'Save'); elseif ($this->onCreator ()) return self::findConstant ('CREATOR_SUBMIT_CAPTION', 'Create'); return parent::getSubmitCaption (); } // }}} // {{{ getCreatorURL /** * Generate a full URL to our creator-widget * * @access public * @return string **/ public function getCreatorURL () { return self::buildURL (self::CREATE_LINK); } // }}} // {{{ getEditorURL /** * Generate a full URL to our editor-widget * * @param bool $withItem (optional) Don't just generate a dummy-link * * @access public * @return string **/ public function getEditorURL ($withItem = false) { if (!$withItem) return self::buildURL ('%{md5::getID()}'); if (!is_object ($Item = $this->getItem ())) return false; return self::buildURL (md5 ($Item->getID ())); } // }}} // {{{ callbackCreate /** * Callback for Item-Creation * * @param object $Handle The object that was created * * @access public * @return void **/ protected function callbackCreate ($Handle) { } // }}} // {{{ callbackUpdate /** * Callback for Item-Updates * * @param object $Handle The object which was updated * * @access protected * @return void **/ protected function callbackUpdate ($Handle) { if (self::findConstant ('EDITOR_AUTO_CLOSE', false)) return ($this->forceListing = true); } // }}} } ?>