/** * @param $data * @param $form * @return HTMLText|SS_HTTPResponse|ViewableData_Customised */ public function doPublish($data, $form, $request = null, $redirectURL = null) { $new_record = $this->record->ID == 0; $controller = $this->getToplevelController(); $list = $this->gridField->getList(); if ($list instanceof ManyManyList) { // Data is escaped in ManyManyList->add() $extraData = isset($data['ManyMany']) ? $data['ManyMany'] : null; } else { $extraData = null; } if (!$this->record->canEdit() || !$this->record->canPublish()) { return $controller->httpError(403); } if (isset($data['ClassName']) && $data['ClassName'] != $this->record->ClassName) { $newClassName = $data['ClassName']; // The records originally saved attribute was overwritten by $form->saveInto($record) before. // This is necessary for newClassInstance() to work as expected, and trigger change detection // on the ClassName attribute $this->record->setClassName($this->record->ClassName); // Replace $record with a new instance $this->record = $this->record->newClassInstance($newClassName); } try { $form->saveInto($this->record); $this->record->write(); $this->record->publish('Stage', 'Live'); $list->add($this->record, $extraData); } catch (ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad', false); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } // TODO Save this item into the given relationship $link = '<a href="' . $this->Link('edit') . '">"' . htmlspecialchars($this->record->Title, ENT_QUOTES) . '"</a>'; $message = _t('GridFieldDetailForm.SaveAndPublished', 'Saved and Published {name} {link}', array('name' => $this->record->i18n_singular_name(), 'link' => $link)); $form->sessionMessage($message, 'good', false); if ($new_record) { return $controller->redirect($this->Link()); } elseif ($this->gridField->getList()->byId($this->record->ID)) { // Return new view, as we can't do a "virtual redirect" via the CMS Ajax // to the same URL (it assumes that its content is already current, and doesn't reload) return $this->edit($controller->getRequest()); } else { // Changes to the record properties might've excluded the record from // a filtered list, so return back to the main view if it can't be found $noActionURL = $controller->removeAction($data['url']); $controller->getRequest()->addHeader('X-Pjax', 'Content'); return $controller->redirect($noActionURL, 302); } }
function testSelectsFragmentByHeader() { $negotiator = new PjaxResponseNegotiator(array( 'default' => function() {return 'default response';}, 'myfragment' => function() {return 'myfragment response';}, )); $request = new SS_HTTPRequest('GET', '/'); $request->addHeader('X-Pjax', 'myfragment'); $this->assertEquals('myfragment response', $negotiator->respond($request)); }
public function testFragmentsOverride() { $negotiator = new PjaxResponseNegotiator(array('alpha' => function () { return 'alpha response'; }, 'beta' => function () { return 'beta response'; })); $request = new SS_HTTPRequest('GET', '/'); $request->addHeader('X-Pjax', 'alpha'); $request->addHeader('Accept', 'text/json'); $response = $negotiator->setFragmentOverride(array('beta'))->respond($request); $json = json_decode($response->getBody()); $this->assertFalse(isset($json->alpha)); $this->assertObjectHasAttribute('beta', $json); }
public function doSave($data, $form) { if ($this->record->ID == 0 && $this->component instanceof LRGridFieldDetailForm && $this->component->getItemLimit() > 0 && $this->gridField->getList()->count() + 1 > $this->component->getItemLimit()) { $form->sessionMessage(_t('LimitedRelationsGridField.ITEM_LIMIT_REACHED', '_You cannot add any more items, you can only add {count} items. Please remove one then try again.', array('count' => $this->component->getItemLimit())), 'bad'); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); $controller = Controller::curr(); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } return parent::doSave($data, $form); }
public function doSave($data, $form) { /** * modelled after doSave method on GridFieldDetailForm_ItemRequest */ if (!$this->record->canEdit()) { return $controller->httpError(403); } if (isset($data['ClassName']) && $data['ClassName'] != $this->record->ClassName) { $newClassName = $data['ClassName']; // The records originally saved attribute was overwritten by $form->saveInto($record) before. // This is necessary for newClassInstance() to work as expected, and trigger change detection // on the ClassName attribute $this->record->setClassName($this->record->ClassName); // Replace $record with a new instance $this->record = $this->record->newClassInstance($newClassName); } try { $form->saveInto($this->record); $this->record->write(); } catch (ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad'); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } // object has been created; assign the relationship if ($this->gridField->isHaveOne()) { $childProperty = $this->gridField->getName(); $this->gridField->childObject->{$childProperty} = $this->record->ID; $this->gridField->childObject->write(); } else { $list = $this->gridField->getList(); //$list->add($this->record, $extraData); $extraData = $this->getExtraSavedData($this->record, $list); $list->add($this->record, $extraData); } return $this->edit(Controller::curr()->getRequest()); }
public function doApprove($data, $form) { if (!$this->owner->record->canApprove()) { return Controller::curr()->httpError(403); } $controller = Controller::curr(); $result = $this->owner->record->doApprove(); if (!$result) { $form->sessionMessage('操作失败,可能该电子邮件已被占用', 'bad', false); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } $redirectLink = 'admin/member-approval'; Controller::curr()->getResponse()->addHeader("X-Pjax", "Content"); return Controller::curr()->redirect($redirectLink); }
/** * Oh, the horror! DRY police be advised. This function is a serious offender. * Saves the form data and redirects to a given link * * @param array The form data * @param Form The form object * @param string The redirect link * @todo GridFieldDetailForm_ItemRequest::doSave is too monolithic, making overloading impossible. Most of this code is a direct copy. * */ protected function saveAndRedirect($data, $form, $redirectLink) { $new_record = $this->owner->record->ID == 0; $controller = Controller::curr(); $list = $this->owner->gridField->getList(); if ($list instanceof ManyManyList) { // Data is escaped in ManyManyList->add() $extraData = isset($data['ManyMany']) ? $data['ManyMany'] : null; } else { $extraData = null; } if (!$this->owner->record->canEdit()) { return $controller->httpError(403); } try { $form->saveInto($this->owner->record); $this->owner->record->write(); $list->add($this->owner->record, $extraData); } catch (ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad'); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } return Controller::curr()->redirect($redirectLink); }
public function Countries($request) { if ($request->isAjax()) { $controller = $this; $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$controller) { return $controller->CountriesForm()->forTemplate(); }, 'Content' => function () use(&$controller) { return $controller->renderWith('ShopAdminSettings_Content'); }, 'Breadcrumbs' => function () use(&$controller) { return $controller->renderWith('CMSBreadcrumbs'); }, 'default' => function () use(&$controller) { return $controller->renderWith($controller->getViewer('show')); }), $this->response); return $responseNegotiator->respond($this->getRequest()); } return $this->renderWith('ShopAdminSettings'); }
function doSave($data, $form) { $new_record = $this->record->ID == 0; $controller = Controller::curr(); try { $form->saveInto($this->record); $this->record->write(); $this->gridField->getList()->add($this->record); } catch (ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad'); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } // TODO Save this item into the given relationship $message = sprintf(_t('GridFieldDetailForm.Saved', 'Saved %s %s'), $this->record->singular_name(), '<a href="' . $this->Link('edit') . '">"' . htmlspecialchars($this->record->Title, ENT_QUOTES) . '"</a>'); $form->sessionMessage($message, 'good'); if ($new_record) { return Controller::curr()->redirect($this->Link()); } elseif ($this->gridField->getList()->byId($this->record->ID)) { // Return new view, as we can't do a "virtual redirect" via the CMS Ajax // to the same URL (it assumes that its content is already current, and doesn't reload) return $this->edit(Controller::curr()->getRequest()); } else { // Changes to the record properties might've excluded the record from // a filtered list, so return back to the main view if it can't be found $noActionURL = $controller->removeAction($data['url']); $controller->getRequest()->addHeader('X-Pjax', 'Content'); return $controller->redirect($noActionURL, 302); } }
/** * Saves the changes made in the bulk edit into the dataObject * * @return Redirect */ public function doAddToList($data, $form) { $className = $this->gridField->list->dataClass; $singleton = singleton($className); $return = array(); if (isset($data['RecordIDs'])) { $ids = explode(",", $data['RecordIDs']); } else { $ids = array(); } $list_id = isset($data['ContactListID']) ? $data['ContactListID'] : 0; $list = ContactList::get()->byID($list_id); try { foreach ($ids as $record_id) { if ($list_id) { $record = DataObject::get_by_id($className, $record_id); if ($record->hasMethod("Lists")) { $list->Contacts()->add($record); $list->write(); } $return[] = $record->ID; } } } catch (Exception $e) { $controller = $this->controller; $form->sessionMessage($e->getResult()->message(), 'bad', false); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } $controller = $this->getToplevelController(); $form = $controller->EditForm(); $message = "Added " . count($return) . " contacts to mailing list '{$list->Title}'"; $form->sessionMessage($message, 'good', false); // Changes to the record properties might've excluded the record from // a filtered list, so return back to the main view if it can't be found $link = $controller->Link(); $controller->getRequest()->addHeader('X-Pjax', 'Content'); return $controller->redirect($link); }
/** * @param $data * @param $form * @return mixed */ public function doSave($data, $form) { $treeClass = $this->config()->get('tree_class'); $page = $treeClass::get()->first(); $currentStage = Versioned::current_stage(); Versioned::reading_stage('Stage'); $controller = Controller::curr(); if (!$page->canEdit()) { return $controller->httpError(403); } try { $form->saveInto($page); $page->write(); } catch (ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad'); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } Versioned::reading_stage($currentStage); if ($page->isPublished()) { $this->publish($data, $form); } $link = '"' . $page->Title . '"'; $message = _t('GridFieldDetailForm.Saved', 'Saved {name} {link}', array('name' => $page->Title, 'link' => $link)); $form->sessionMessage($message, 'good'); $action = $this->edit(Controller::curr()->getRequest()); return $action; }
/** * This method tries to blend DetailForm::doSave behaviour with CMSMain * publish behaviour. It might not be rock solid.. * * @param array $data * @param Form $form * @return SS_HTTPResponse */ public function publish($data, $form) { if (!$this->record->canPublish()) { return $controller->httpError(403); } $controller = Controller::curr(); $list = $this->gridField->getList(); if ($list instanceof ManyManyList) { // Data is escaped in ManyManyList->add() $extraData = isset($data['ManyMany']) ? $data['ManyMany'] : null; } else { $extraData = null; } try { $this->record->writeWithoutVersion(); $form->saveInto($this->record); $this->record->write(); $list->add($this->record, $extraData); $this->record->doPublish(); } catch (ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad'); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } $link = '"' . $this->record->Title . '"'; $message = _t('GridFieldDetailForm.Saved', 'Saved {name} {link}', array('name' => $this->record->i18n_singular_name(), 'link' => $link)); $form->sessionMessage($message, 'good'); return $this->edit(Controller::curr()->getRequest()); }
public function doSave($data, $form) { try { $form->saveInto($this->record); $this->record->write(); $list = $this->gridfield->getList(); if ($list instanceof ManyManyList) { $extradata = array_intersect_key($data, $list->getField('extraFields')); $list->add($this->record, $extradata); } else { $list->add($this->record); } } catch (ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad'); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } return $this->customise(array('ExpandableForm' => $form))->renderWith($this->template); }
public function saveAttributeSettings($data, $form) { //Hack for LeftAndMain::getRecord() self::$tree_class = 'ShopConfig'; $config = ShopConfig::get()->First(); $form->saveInto($config); $config->write(); $form->sessionMessage('Saved Attribute Settings', 'good'); $controller = $this; $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$controller) { //return $controller->renderWith('ShopAdminSettings_Content'); return $controller->AttributeSettingsForm()->forTemplate(); }, 'Content' => function () use(&$controller) { //return $controller->renderWith($controller->getTemplatesWithSuffix('_Content')); }, 'Breadcrumbs' => function () use(&$controller) { return $controller->renderWith('CMSBreadcrumbs'); }, 'default' => function () use(&$controller) { return $controller->renderWith($controller->getViewer('show')); }), $this->response); return $responseNegotiator->respond($this->getRequest()); }
public function doChangeStatus($data, $form) { $new_record = $this->record->ID == 0; $controller = $this->getToplevelController(); $list = $this->gridField->getList(); try { $this->record->Status = $data["Status"]; $this->record->write(); } catch (ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad', false); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } $link = '<a href="' . $this->Link('edit') . '">"' . htmlspecialchars($this->record->Title, ENT_QUOTES) . '"</a>'; $message = _t('Orders.StatusChanged', 'Status Changed {name} {link}', array('name' => $this->record->i18n_singular_name(), 'link' => $link)); $form->sessionMessage($message, 'good', false); if ($this->gridField->getList()->byId($this->record->ID)) { // Return new view, as we can't do a "virtual redirect" via the CMS Ajax // to the same URL (it assumes that its content is already current, and doesn't reload) return $this->edit($controller->getRequest()); } else { // Changes to the record properties might've excluded the record from // a filtered list, so return back to the main view if it can't be found $noActionURL = $controller->removeAction($data['url']); $controller->getRequest()->addHeader('X-Pjax', 'Content'); return $controller->redirect($noActionURL, 302); } }
/** * @param $data * @param $form * @return mixed */ public function doSave($data, $form) { $page = $this->findOrMakePage(); $controller = Controller::curr(); $publish = isset($data['__publish__']); if (!$page->canEdit()) { return $controller->httpError(403); } try { $form->saveInto($page); $page->write(); if ($publish) { $page->doPublish(); } } catch (ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad'); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } $link = '"' . $page->Title . '"'; $message = _t($publish ? 'SinglePageAdmin.Published' : 'GridFieldDetailForm.Saved', ($publish ? 'Published' : 'Saved') . ' {name} {link}', array('name' => $page->ClassName, 'link' => $link)); $form->sessionMessage($message, 'good'); $action = $this->edit(Controller::curr()->getRequest()); return $action; }
public function doSend($data, $form) { //copied from parent $new_record = $this->record->ID == 0; $controller = Controller::curr(); try { $form->saveInto($this->record); $this->record->Status = 'Sending'; //custom: changing the status of to indicate we are sending $this->record->write(); $this->gridField->getList()->add($this->record); } catch (ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad'); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } //custom code $nsc = NewsletterSendController::inst(); $nsc->enqueue($this->record); $nsc->processQueueOnShutdown($this->record->ID); //javascript hides the success message appropriately Requirements::javascript(NEWSLETTER_DIR . '/javascript/NewsletterSendConfirmation.js'); $message = _t('NewsletterAdmin.SendMessage', 'Send-out process started successfully. Check the progress in the "Sent To" tab'); //end custom code $form->sessionMessage($message, 'good'); if ($new_record) { return Controller::curr()->redirect($this->Link()); } elseif ($this->gridField->getList()->byId($this->record->ID)) { // Return new view, as we can't do a "virtual redirect" via the CMS Ajax // to the same URL (it assumes that its content is already current, and doesn't reload) return $this->edit(Controller::curr()->getRequest()); } else { // Changes to the record properties might've excluded the record from // a filtered list, so return back to the main view if it can't be found $noActionURL = $controller->removeAction($data['url']); $controller->getRequest()->addHeader('X-Pjax', 'Content'); return $controller->redirect($noActionURL, 302); } }
/** * Save record as normal, then if the record was new invoke the record class (which should be derived from ReplicantAction) to perform the action. * * If the remote address is provided * * @param $data * @param $form * @return HTMLText|SS_HTTPResponse|ViewableData_Customised|void */ public function doSave($data, $form) { $list = $this->gridField->getList(); $controller = Controller::curr(); $new_record = !$this->record->isInDB(); $ok = false; try { $this->record->update($data); // $form->saveInto($this->record); $this->record->write(); // now actually run the action. If it is a dump action and the remote host is not localhost then we call dump on the remote host instead. if ($this->record->ClassName != 'ReplicantActionDump' || $this->record->RemoteHost == 'localhost') { $ok = $this->record->execute(); } else { $transport = Replicant::transportFactory($this->record->Protocol, $this->record->RemoteHost, $this->record->Proxy, $this->record->UserName, $this->record->Password); $path = "replicant/dump" . ($this->record->UseGZIP ? "&UseGZIP=1" : ""); $url = $transport->buildURL($path); $this->record->step("Dumping on remote system {$url}"); try { $result = $transport->fetchPage($path); } catch (Exception $e) { $result = $e->getMessage(); } // TODO SW better result checking here $ok = false !== strpos($result, 'Success'); if ($ok) { $this->record->success("Dumped Database on {$url}: {$result}"); } else { $this->record->failed("Failed calling {$url}: {$result}"); } } if ($ok) { $link = '"' . $this->record->Title . '"'; $message = _t('GridFieldDetailForm.Saved', 'Saved {name} {link}', array('name' => $this->record->i18n_singular_name(), 'link' => $link)); $form->sessionMessage($message, 'good'); } else { $message = _t('Error', 'Failed to {message}: {error}', array('message' => $this->record->i18n_singular_name(), 'error' => $this->record->ResultInfo)); $form->sessionMessage($message, 'bad'); } $list->add($this->record, null); } catch (ValidationException $e) { $this->record->failed($e->getResult()->message()); $form->sessionMessage($e->getResult()->message(), 'bad'); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } catch (Exception $e) { $this->record->failed($e->getMessage()); $form->sessionMessage($e->getMessage(), 'bad'); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); } $noActionURL = $controller->removeAction($data['url']); $controller->getRequest()->addHeader('X-Pjax', 'Content'); return $controller->redirect($noActionURL, 302); }
/** * Generate a response object for a form validation error * * @param Form $form The source form * @param ValidationException $e The validation error message * @return SS_HTTPResponse * @throws SS_HTTPResponse_Exception */ protected function generateValidationResponse($form, $e) { $controller = $this->getToplevelController(); $form->sessionMessage($e->getResult()->message(), 'bad', false); $responseNegotiator = new PjaxResponseNegotiator(array('CurrentForm' => function () use(&$form) { return $form->forTemplate(); }, 'default' => function () use(&$controller) { return $controller->redirectBack(); })); if ($controller->getRequest()->isAjax()) { $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); } return $responseNegotiator->respond($controller->getRequest()); }