/** * Handles actually adding a folder to the databsae * @param {array} $data Submitted data * @param {Form} $form Submitting form * @return {string} HTML to be rendered */ public function doAddFolder($data, Form $form) { //Existing Check $existingCheck = SnippetFolder::get()->filter('Name:nocase', Convert::raw2sql($data['Name']))->filter('LanguageID', intval($data['LanguageID'])); if (array_key_exists('FolderID', $data)) { $existingCheck = $existingCheck->filter('ParentID', intval($data['FolderID'])); } else { $existingCheck->filter('ParentID', 0); } if ($existingCheck->Count() > 0) { $form->sessionMessage(_t('CodeBank.FOLDER_EXISTS', '_A folder already exists with that name'), 'bad'); return $this->redirectBack(); } $folder = new SnippetFolder(); $folder->Name = $data['Name']; $folder->LanguageID = $data['LanguageID']; if (array_key_exists('ParentID', $data)) { $folder->ParentID = $data['ParentID']; } //Write the folder to the database $folder->write(); //Find the next & previous nodes, for proper positioning (Sort isn't good enough - it's not a raw offset) $next = $prev = null; $next = SnippetFolder::get()->filter('LanguageID', $folder->LanguageID)->filter('ParentID', $folder->ParentID)->filter('Name:GreaterThan', Convert::raw2sql($folder->Title))->first(); if (!$next) { $prev = SnippetFolder::get()->filter('LanguageID', $folder->LanguageID)->filter('ParentID', $folder->ParentID)->filter('Name:LessThan', Convert::raw2sql($folder->Title))->reverse()->first(); } //Setup js that will add the node to the tree $html = CodeBank_TreeNode::create($folder, '', false)->forTemplate() . '</li>'; $parentFolder = $folder->Parent(); $outputData = array('folder-' . $folder->ID => array('html' => $html, 'ParentID' => !empty($parentFolder) && $parentFolder !== false && $parentFolder->ID != 0 ? 'folder-' . $folder->ParentID : 'language-' . $folder->LanguageID, 'NextID' => $next ? 'folder-' . $next->ID : null, 'PrevID' => $prev ? 'folder-' . $prev->ID : null)); Requirements::customScript('window.parent.updateCodeBankTreeNodes(' . json_encode($outputData) . ');'); //Re-render the form $form->setFields(new FieldList()); $form->setActions(new FieldList()); $form->setMessage(_t('CodeBank.FOLDER_ADDED', '_Folder added you may now close this dialog'), 'good'); return $this->customise(array('Content' => ' ', 'Form' => $form))->renderWith('CMSDialog'); }
/** * creates a form object with a free configurable markup * * @param ContentController $controller the calling controller instance * @param array $params optional parameters * @param array $preferences optional preferences * @param bool $barebone defines if a form should only be instanciated or be used too * * @return CustomHtmlForm * * @author Sebastian Diel <*****@*****.**>, * Sascha Koehler <*****@*****.**> * @since 13.01.2015 */ public function __construct($controller, $params = null, $preferences = null, $barebone = false) { $this->extend('onBeforeConstruct', $controller, $params, $preferences, $barebone); global $project; $this->barebone = $barebone; $this->controller = $controller; if (is_array($params)) { $this->customParameters = $params; } // Hook for setting preferences via a method call $this->preferences(); if (is_array($preferences)) { foreach ($preferences as $title => $setting) { if (!empty($title)) { $this->basePreferences[$title] = $setting; } } } $name = $this->getSubmitAction(); if (!$barebone) { $this->getFormFields(); } if ($this->securityTokenEnabled) { SecurityToken::enable(); } else { SecurityToken::disable(); } parent::__construct($this->getFormController($controller, $preferences), $name, new FieldList(), new FieldList()); if (!$barebone) { $this->getFormFields(); $this->fillInFieldValues(); } // Hook for setting preferences via a method call; we need to do this // a second time so that the standard Silverstripe mechanism can take // influence, too (i.e. _config.php files, init methods, etc). $this->preferences(); if (is_array($preferences)) { foreach ($preferences as $title => $setting) { if (!empty($title)) { $this->basePreferences[$title] = $setting; } } } // Counter for the form class, init or increment if (!isset(self::$classInstanceCounter[$this->class])) { self::$classInstanceCounter[$this->class] = 0; } if (!$barebone) { self::$classInstanceCounter[$this->class]++; } // new assignment required, because the controller will be overwritten in the form class $this->controller = $controller; // create group structure if (isset($this->formFields)) { $this->fieldGroups['formFields'] = $this->getFormFields(); } else { $this->fieldGroups['formFields'] = array(); } $this->name = str_replace('/', '', $this->class . '_' . $name . '_' . self::$classInstanceCounter[$this->class]); $this->jsName = $this->name; $this->SSformFields = $this->getForm(); $this->SSformFields['fields']->setForm($this); $this->SSformFields['actions']->setForm($this); parent::setFields($this->SSformFields['fields']); parent::setActions($this->SSformFields['actions']); // define form action $this->setFormAction($this->buildFormAction()); $this->setHTMLID($this->getName()); /* * load and init JS validators * form integration via FormAttributes() */ if (!$barebone) { $javascriptSnippets = $this->getJavascriptValidatorInitialisation(); if (!$this->getLoadShoppingCartModules()) { SilvercartShoppingCart::setLoadShoppingCartModules(false); } if ($this->getCreateShoppingCartForms() && class_exists('SilvercartShoppingCart')) { SilvercartShoppingCart::setCreateShoppingCartForms(false); } $this->controller->addJavascriptSnippet($javascriptSnippets['javascriptSnippets']); $this->controller->addJavascriptOnloadSnippet($javascriptSnippets['javascriptOnloadSnippets']); $this->controller->addJavascriptOnloadSnippet($this->getJavascriptFieldInitialisations()); } // Register the default module directory from mysite/_config.php self::registerModule($project); $this->extend('onAfterConstruct', $controller, $params, $preferences, $barebone); }