Source of file ProductQuestion_ProductControllerDecorator.php
Size: 3,840 Bytes - Last Modified: 2021-12-23T10:41:01+00:00
/var/www/docs.ssmods.com/process/src/code/model/ProductQuestion_ProductControllerDecorator.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 | <?php /** * adds functionality to ProductControllers * * * */ class ProductQuestion_ProductControllerDecorator extends Extension { /** * we need this here to * because otherwise the extension will not work */ private static $allowed_actions = array( "ProductQuestionsAnswerForm", "productquestionsanswerselect" ); /** * Stores the related OrderItem * @var OrderItem */ protected $productQuestionOrderItem = null; /** * renders a form with the product questions * @return String (HTML) */ public function productquestionsanswerselect() { $this->getProductQuestionOrderItem(); return $this->owner->customise( array( "Title" => $this->productQuestionOrderItem->getTableTitle(), "Form" => $this->ProductQuestionsAnswerForm() ) )->renderWith("productquestionsanswerselect") ; } /** * returns a form with questions * @param OrderItem (optional) * @return Form */ public function ProductQuestionsAnswerForm($orderItem = null) { //the second part (instanceof) is important, because otherwise it becomes a request ... if ($orderItem && $orderItem instanceof OrderItem) { $this->productQuestionOrderItem = $orderItem; } else { $this->getProductQuestionOrderItem(); } if ($this->productQuestionOrderItem) { return $this->productQuestionOrderItem->owner->ProductQuestionsAnswerForm($this->owner, $name = "ProductQuestionsAnswerForm"); } else { user_error("could not submit..."); } } /** * returns the fields from the form * @return FieldSet */ public function ProductQuestionsAnswerFormFields() { $fieldSet = new FieldList(); $product = $this->owner->dataRecord; $productQuestions = $product->ApplicableProductQuestions(); if ($productQuestions) { foreach ($productQuestions as $productQuestion) { $fieldSet->push($productQuestion->getFieldForProduct($product)); } } return $fieldSet; } /** * processes a form and * adds product question answer(s) to order item. * The answers are added as HTML and JSON * and redirects back to the previous page or a set BackURL * (set in the form data) * @param Array $data - form data * @param Form form - data from the form */ public function addproductquestionsanswer($data, $form) { $this->getProductQuestionOrderItem(); $data = Convert::raw2sql($data); if ($this->productQuestionOrderItem) { $this->productQuestionOrderItem->updateOrderItemWithProductAnswers( $answers = $data["ProductQuestions"], $write = true ); } if (isset($data["BackURL"]) && $data["BackURL"]) { $this->owner->redirect($data["BackURL"]); } else { $this->owner->redirectBack(); } } /** * retrieves order item from post / get variables. * @return OrderItem | Null */ protected function getProductQuestionOrderItem() { $id = intval($this->owner->request->param("ID")); if (!$id) { $id = intval($this->owner->request->postVar("OrderItemID")); } if (!$id) { $id = intval($this->owner->request->getVar("OrderItemID")); } if ($id) { $this->productQuestionOrderItem = OrderItem::get()->byID($id); } if (! $this->productQuestionOrderItem) { user_error("NO this->productQuestionOrderItem specified"); } return $this->productQuestionOrderItem; } } |