Source of file ProductQuestion_OrderItemExtension.php
Size: 11,237 Bytes - Last Modified: 2021-12-23T10:41:01+00:00
/var/www/docs.ssmods.com/process/src/code/model/ProductQuestion_OrderItemExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 | <?php /** * adds functionality to OrderItems. */ class ProductQuestion_OrderItemExtension extends DataExtension { private static $db = array( 'ProductQuestionsAnswer' => 'HTMLText', 'JSONAnswers' => 'Text', ); private static $casting = array( 'ProductQuestionsAnswerNOHTML' => 'Text', 'ConfigureLabel' => 'Varchar', 'ConfigureLink' => 'Varchar', ); public function updateCMSFields(FieldList $fields) { $fields->addFieldsToTab( 'Root.Questions', array( ReadOnlyField::create('ProductQuestionsAnswer', _t('ProductQuestions.ANSWERS', 'Answers')), ReadOnlyField::create('JSONAnswers', _t('ProductQuestions.JSON_ANSWERS', 'Answers as JSON')), ) ); return $fields; } /** * @return bool */ public function AllQuestionsAnswered() { if ($answers = $this->owner->ProductQuestionsAnswers()) { foreach ($answers as $productQuestion) { if (!$productQuestion->Answer) { return false; } } } return true; } /** * @return bool */ public function AllRequiredQuestionsAnswered() { if ($answers = $this->owner->ProductQuestionsAnswers()) { foreach ($answers as $productQuestion) { if ($productQuestion->AnswerRequired) { if (!$productQuestion->Answer) { return false; } } } } return true; } /** * casted variable. * * @return string */ public function ProductQuestionsAnswerNOHTML() { return $this->owner->getProductQuestionsAnswerNOHTML(); } public function getProductQuestionsAnswerNOHTML() { return strip_tags($this->owner->ProductQuestionsAnswer); } /** * can the order item be configured. * * @return bool */ public function canConfigure() { if ($this->owner->Order()->IsSubmitted()) { return false; } return true; } /** * can the order item be configured. * * @return bool */ public function HasRequiredQuestions() { if ($this->owner->AllRequiredQuestionsAnswered()) { return false; } return true; } /** * returns a link to configure an OrderItem * and adds the relevant requirements. * * @return string */ public function ConfigureLabel() { Requirements::javascript('ecommerce_product_questions/javascript/EcomProductQuestions.js'); return $this->owner->ProductQuestionsAnswerFormLabel(); } /** * returns a link to configure an OrderItem * and adds the relevant requirements. * * @return string */ public function ConfigureLink() { Requirements::javascript('ecommerce_product_questions/javascript/EcomProductQuestions.js'); return $this->owner->ProductQuestionsAnswerFormLink(); } /** * returns the link to edit the products. * * @return string */ public function ProductQuestionsAnswerFormLabel() { if ($this->owner->HasProductQuestions()) { $buyable = $this->productQuestionBuyable(); if ($buyable) { if ($label = $buyable->CustomConfigureLabel()) { return $label; } } return _t('ProductQuestion.CONFIGURE', 'Configure'); } return ''; } /** * returns the link to edit the products. * * @return string */ public function ProductQuestionsAnswerFormLink() { if ($this->owner->HasProductQuestions()) { $buyable = $this->productQuestionBuyable(); if ($buyable) { return $buyable->ProductQuestionsAnswerFormLink($this->owner->ID); } } return ''; } /** * cache only! * * @var array */ private static $_has_product_questions = array(); /** * Does the buyable associated with the orderitem * have product questions? * * @return bool */ public function HasProductQuestions() { if (!isset(self::$_has_product_questions[$this->owner->ID])) { $productQuestions = $this->owner->ProductQuestions(); if ($productQuestions && $productQuestions->count()) { self::$_has_product_questions[$this->owner->ID] = true; } else { self::$_has_product_questions[$this->owner->ID] = false; } } return self::$_has_product_questions[$this->owner->ID]; } /** * cache only! * * @var array */ private static $_product_questions = array(); /** * @alias for ProductQuestions */ public function ApplicableProductQuestions() { return $this->ProductQuestions(); } /** * @return DataList | Null */ public function ProductQuestions() { if (!isset(self::$_product_questions[$this->owner->ID])) { if ($buyable = $this->owner->productQuestionBuyable()) { self::$_product_questions[$this->owner->ID] = $buyable->ApplicableProductQuestions(); } else { self::$_product_questions[$this->owner->ID] = null; } } return self::$_product_questions[$this->owner->ID]; } /** * cache only! * * @var array */ private static $_product_question_product = null; /** * @return Product | Null */ public function productQuestionBuyable() { if (!isset(self::$_product_question_product[$this->owner->ID])) { self::$_product_question_product[$this->owner->ID] = $this->owner->Buyable(); } return self::$_product_question_product[$this->owner->ID]; } /** * * @return Form */ public function ProductQuestionsAnswerFormInCheckoutPage() { return ModelAsController::controller_for($this->owner->Buyable())->ProductQuestionsAnswerForm($this->owner); } /** * @return Form */ public function ProductQuestionsAnswerForm($controller = null, $name = 'productquestionsanswerselect') { $productQuestions = $this->owner->ProductQuestions(); $buyable = $this->productQuestionBuyable(); $backURL = Session::get('BackURL'); if ($backURL || empty($_GET['BackURL'])) { //do nothing } else { $backURL = $_GET['BackURL']; } if ($productQuestions && $productQuestions->count()) { $requiredfields = array(); $fields = new FieldList( new HiddenField('OrderItemID', 'OrderItemID', $this->owner->ID), new HiddenField('BackURL', 'BackURL', $backURL) ); $values = array(); if ($this->owner->JSONAnswers) { $values = json_decode($this->owner->JSONAnswers); } foreach ($productQuestions as $productQuestion) { $value = empty($values->{$productQuestion->ID}) ? null : $values->{$productQuestion->ID}; $fields->push($productQuestion->getFieldForProduct($buyable, $value)); //TODO: perhaps use a dropdown instead (eliminates need to use keyboard) } $actions = new FieldList( array( new FormAction('addproductquestionsanswer', _t('ProductQuestion.ANSWER_QUESTION', 'Update Selection')), ) ); $validator = new RequiredFields($requiredfields); $form = new Form($controller, $name, $fields, $actions, $validator); Requirements::themedCSS('Cart'); return $form; } } /** * @return ArrayList | NULL */ public function ProductQuestionsAnswers() { if ($this->owner->HasProductQuestions()) { $al = new ArrayList(); $values = json_decode($this->owner->JSONAnswers); if ($questions = $this->owner->ProductQuestions()) { foreach ($questions as $question) { $newQuestion = clone $question; $answer = empty($values->{$question->ID}) ? null : $values->{$question->ID}; if ($answer) { $newQuestion->Answer = $answer; } elseif ($newQuestion->AnswerRequired) { $newQuestion->Answer = null; } else { $newQuestion->Answer = $newQuestion->DefaultAnswer; } $al->push($newQuestion); } return $al; } } } /** * @param array $answers * ID = ProductQuestion.ID * "ID" => "Answer" (String) * @param bool $write */ public function updateOrderItemWithProductAnswers($answers, $write = true) { if ($this->owner->canEdit()) { $this->owner->ProductQuestionsAnswer = ''; if (is_array($answers) && count($answers)) { foreach ($answers as $productQuestionID => $productQuestionAnswer) { $question = ProductQuestion::get()->byID(intval($productQuestionID)); if ($question) { } //$form->addErrorMessage("ProductQuestions", $message, $type); } $this->owner->ProductQuestionsAnswer = $this->owner->renderWith('ProductQuestionsAnswers')->getValue(); } $this->owner->JSONAnswers = json_encode($answers); if ($write) { $this->owner->write(); } } } public function onBeforeWrite() { parent::onBeforeWrite(); if (!empty($this->owner->Parameters)) { if (!empty($this->owner->Parameters['productquestions'])) { $answers = array(); $params = $this->owner->Parameters['productquestions']; $params = urldecode($params); $items = explode('|', $params); if ($items && is_array($items) && count($items)) { foreach ($items as $item) { if ($item) { $itemArray = explode('=', $item); if (is_array($itemArray) && count($itemArray) == 2) { $key = intval(str_replace(array('ProductQuestions[', ']'), '', $itemArray[0])); $value = convert::raw2sql($itemArray[1]); $answers[$key] = $value; } } } } unset($this->owner->Parameters); $this->updateOrderItemWithProductAnswers($answers, false); } } } } |