public function configuration() { $errorMessage = null; $form = $this->createForm('stockalert.configuration.form', 'form'); try { $configForm = $this->validateForm($form)->getData(); ConfigQuery::write(StockAlert::CONFIG_ENABLED, $configForm['enabled']); ConfigQuery::write(StockAlert::CONFIG_THRESHOLD, $configForm['threshold']); $emails = str_replace(' ', '', $configForm['emails']); ConfigQuery::write(StockAlert::CONFIG_EMAILS, $emails); return $this->generateSuccessRedirect($form); } catch (FormValidationException $e) { $errorMessage = $e->getMessage(); } catch (\Exception $e) { $errorMessage = $e->getMessage(); } $form->setErrorMessage($errorMessage); $this->getParserContext()->addForm($form)->setGeneralError($errorMessage); return $this->render("module-configure", ["module_code" => StockAlert::getModuleCode()]); }
public function onModuleConfiguration(HookRenderEvent $event) { $moduleId = $this->getModule()->getModuleId(); $config = StockAlert::getConfig(); $event->add($this->render("configuration.html", ['module_id' => $moduleId, 'config' => $config])); }
public function checkStockForAdmin(OrderEvent $event) { $order = $event->getOrder(); $config = StockAlert::getConfig(); $pseIds = []; foreach ($order->getOrderProducts() as $orderProduct) { $pseIds[] = $orderProduct->getProductSaleElementsId(); } if ($config['enabled']) { $threshold = $config['threshold']; $productIds = ProductQuery::create()->useProductSaleElementsQuery()->filterById($pseIds, Criteria::IN)->filterByQuantity($threshold, Criteria::LESS_EQUAL)->filterByWeight(0, Criteria::NOT_EQUAL)->endUse()->select('Id')->find()->toArray(); if (!empty($productIds)) { $this->sendEmailForAdmin($config['emails'], $productIds); } } }
protected function buildForm() { $config = StockAlert::getConfig(); $this->formBuilder->add('enabled', 'checkbox', ["required" => false, "data" => $config['enabled'], "label" => Translator::getInstance()->trans("Enabled", [], StockAlert::MESSAGE_DOMAIN), "label_attr" => ["for" => "enabled"]])->add('threshold', 'integer', ["required" => true, "constraints" => [new NotBlank()], "data" => $config['threshold'], "label" => Translator::getInstance()->trans("Threshold", [], StockAlert::MESSAGE_DOMAIN), "label_attr" => ["for" => "email", "help" => Translator::getInstance()->trans("You will recieve a notification when the quantity in stock is lower or equal to this value.", [], StockAlert::MESSAGE_DOMAIN)]])->add("emails", "text", ["constraints" => [new Callback(["methods" => [[$this, "checkEmails"]]])], "required" => false, "data" => implode(',', $config['emails']), "label" => Translator::getInstance()->trans("Email Address", [], StockAlert::MESSAGE_DOMAIN), "label_attr" => ["for" => "emails", "help" => Translator::getInstance()->trans("A comma separated list of email that will recieve notifications", [], StockAlert::MESSAGE_DOMAIN)]]); }