/** * Execute the helper * * @param \Handlebars\Template $template The template instance * @param \Handlebars\Context $context The current context * @param array $args The arguments passed the the helper * @param string $source The source * * @return mixed */ public function execute(Template $template, Context $context, $args, $source) { $tmp = $context->get($args); $buffer = ''; if (!$tmp) { $template->setStopToken('else'); $template->discard(); $template->setStopToken(false); $buffer = $template->render($context); } elseif (is_array($tmp) || $tmp instanceof \Traversable) { $isList = is_array($tmp) && array_keys($tmp) === range(0, count($tmp) - 1); $index = 0; $lastIndex = $isList ? count($tmp) - 1 : false; foreach ($tmp as $key => $var) { $specialVariables = array('@index' => $index, '@first' => $index === 0, '@last' => $index === $lastIndex); if (!$isList) { $specialVariables['@key'] = $key; } $context->pushSpecialVariables($specialVariables); $context->push($var); $template->setStopToken('else'); $template->rewind(); $buffer .= $template->render($context); $context->pop(); $context->popSpecialVariables(); $index++; } $template->setStopToken(false); } return $buffer; }
/** * Execute the helper * {{#formErrors login}} * <ul> * {{#each errors}} * <li>{{this}}</li> * {{/each}} * </ul> * {{/formErrors}} * Catch form validation's erros. * * @param \Handlebars\Template $template The template instance * @param \Handlebars\Context $context The current context * @param array $args The arguments passed the the helper * @param string $source The source * * @return mixed */ public function execute(Template $template, Context $context, $args, $source) { $buffer = ''; $args = $template->parseArguments($args); $errors = \Session::get('errors', new \Illuminate\Support\MessageBag()); // no form specified // return empty buffer if (!count($args)) { return $buffer; } // if MessageBag does not exists // return empty buffer if (!method_exists($errors, 'hasBag')) { return $buffer; } // Defined MessageBag exists // so we push errors list to the context if ($errors->hasBag($args[0])) { $context->push(['errors' => $errors->{$args[0]}->all()]); $template->rewind(); $buffer .= $template->render($context); $context->pop(); return $buffer; } // return empty buffer return $buffer; }
/** * Execute the helper * * @param \Handlebars\Template $template The template instance * @param \Handlebars\Context $context The current context * @param array $args The arguments passed the the helper * @param string $source The source * * @return mixed */ public function execute(Template $template, Context $context, $args, $source) { // $args = $this->parseArgs( $args ); $user = $context->get('user'); if (!$user) { throw new \Exception("User need to be log-in to access to his addresses"); } // Get Address // ----------------- $options = ['where' => [['uid', '=', $user->uid]]]; try { $addresses = Subbly::api('subbly.user_address')->findByUser($user, $options); } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { return false; } if (!count($addresses)) { $addresses = false; } $buffer = ''; if (!$addresses) { $template->setStopToken('else'); $template->discard(); $template->setStopToken(false); $buffer = $template->render($context); } elseif (is_array($addresses) || $addresses instanceof \Traversable) { $isList = is_array($addresses) && array_keys($addresses) === range(0, count($addresses) - 1); $index = 0; $lastIndex = $isList ? count($addresses) - 1 : false; foreach ($addresses as $key => $var) { $specialVariables = array('@index' => $index, '@first' => $index === 0, '@last' => $index === $lastIndex); if (!$isList) { $specialVariables['@key'] = $key; } $context->pushSpecialVariables($specialVariables); $context->push($var); $template->setStopToken('else'); $template->rewind(); $buffer .= $template->render($context); $context->pop(); $context->popSpecialVariables(); $index++; } $template->setStopToken(false); } return $buffer; }
/** * Execute the helper * * @param \Handlebars\Template $template The template instance * @param \Handlebars\Context $context The current context * @param array $args The arguments passed the the helper * @param string $source The source * * @return mixed */ public function execute(Template $template, Context $context, $args, $source) { $props = $this->parseProps($args, $context); $args = $this->parseArgs($args); $field = 'id'; $id = false; $buffer = ''; // no properties // so product's ID is in URL // or is th first arguments if (!$props) { $id = count($args) === 0 ? $context->get('inputs.productId') : $args[0]; } else { if (array_key_exists('productId', $props)) { $id = $props['productId']; } else { if (array_key_exists('productSku', $props)) { $id = $props['productSku']; $field = 'sku'; } } } if (!$id) { throw new \InvalidArgumentException('Can not find product identifier'); } // Product query // ---------------- // TODO: add status restriction if // current user is not loggued to Backend $productOptions = ['includes' => ['images', 'categories', 'options', 'translations'], 'where' => [['status', '!=', 'draft'], ['status', '!=', 'hidden']]]; // Get product // ----------------- try { $product = \Subbly\Subbly::api('subbly.product')->find($id, $productOptions, $field)->toArray(); } catch (\Exception $e) { throw new \InvalidArgumentException($e->getMessage()); } $context->push($product); $template->rewind(); $buffer .= $template->render($context); $context->pop(); return $buffer; }
/** * Execute the helper * * @param \Handlebars\Template $template The template instance * @param \Handlebars\Context $context The current context * @param array $args The arguments passed the the helper * @param string $source The source * * @return mixed */ public function execute(Template $template, Context $context, $args, $source) { $args = $this->ParseArgs($args); $dataSelector = implode(".", array_slice($args, 0, count($args) - 1)); $tmp = $context->get($dataSelector); if (count($args) == 1) { $limit = 0; } else { $limit = $args[count($args) - 1]; } $buffer = ''; if (!$tmp) { $template->setStopToken('else'); $template->discard(); $template->setStopToken(false); $buffer = $template->render($context); } elseif (is_array($tmp) || $tmp instanceof \Traversable) { $isList = is_array($tmp) && array_keys($tmp) === range(0, count($tmp) - 1); $index = 0; $lastIndex = $isList ? count($tmp) - 1 : false; foreach ($tmp as $key => $var) { if ($index + 1 > $limit && $limit > 0) { continue; } $specialVariables = array('@index' => $index, '@first' => $index === 0, '@last' => $index === $lastIndex); if (!$isList) { $specialVariables['@key'] = $key; } $context->pushSpecialVariables($specialVariables); $context->push($var); $template->setStopToken('else'); $template->rewind(); $buffer .= $template->render($context); $context->pop(); $context->popSpecialVariables(); $index++; } $template->setStopToken(false); } return $buffer; }
/** * Execute the helper * * @param \Handlebars\Template $template The template instance * @param \Handlebars\Context $context The current context * @param array $args The arguments passed the the helper * @param string $source The source * * @return mixed */ public function execute(Template $template, Context $context, $args, $source) { $isLogin = $context->get('isUserLogin'); if (!$isLogin) { throw new \Exception("User need to be log-in to access to his addresses"); } $props = $this->parseProps($args, $context); $args = $this->parseArgs($args); $id = false; $buffer = ''; // no properties // so address's ID is in URL // or is th first arguments if (!$props) { $id = count($args) === 0 ? $context->get('inputs.addressId') : $args[0]; } else { if (array_key_exists('addressId', $props)) { $id = $props['addressId']; } } // new address if (!$id) { return $template->render($context); } // throw new \InvalidArgumentException( 'Can not find user address ID'); // Get Address // ----------------- $user = $context->get('user'); try { $options = ['where' => [['uid', '=', $user->uid]]]; $address = Subbly::api('subbly.user_address')->find($id, $options)->toArray(); } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { throw new \InvalidArgumentException($e->getMessage()); } $buffer = ''; $context->push($address); $template->rewind(); $buffer .= $template->render($context); $context->pop(); return $buffer; }
/** * Execute the helper * * @param \Handlebars\Template $template The template instance * @param \Handlebars\Context $context The current context * @param array $args The arguments passed the the helper * @param string $source The source * * @return mixed */ public function execute(Template $template, Context $context, $args, $source) { $props = $this->parseProps($args, $context); $args = $this->parseArgs($args); // Products query // ---------------- $productsOptions = ['where' => [['status', '!=', 'draft'], ['status', '!=', 'hidden']]]; // Includes // ---------------- if (isset($props['includes']) && is_array($props['includes'])) { $productsOptions['includes'] = $props['includes']; } // Where // ---------------- if (isset($props['where']) && is_array($props['where'])) { // $productsOptions['where'] = []; foreach ($props['where'] as $condition) { if (is_array($condition) && count($condition) == 3) { $productsOptions['where'][] = $condition; } } } // Order By // ---------------- if (isset($props['order_by']) && is_array($props['order_by'])) { $productsOptions['order_by'] = []; foreach ($props['order_by'] as $order) { if (is_array($order) && count($order) == 2) { $productsOptions['order_by'][$order[0]] = $order[1]; } } } // Offset & limit // ---------------- if (isset($props['limit']) && is_integer($props['limit'])) { $productsOptions['limit'] = $props['limit']; } $offset = null; if (isset($props['offset']) && is_integer($props['offset']) && isset($productsOptions['limit'])) { $productsOptions['offset'] = $props['offset']; } if (!isset($productsOptions['offset']) && isset($props['page']) && is_integer($props['page'])) { $offset = ((int) $props['page'] - 1) * $productsOptions['limit']; $offset = $offset < 0 ? 0 : $offset; } $offset ?: 0; $productsOptions['offset'] = $offset; // Categories // ---------------- $hasCategories = is_array($props) && (array_key_exists('category', $props) || array_key_exists('subcategory', $props)); if ($hasCategories) { $categoriesOptions = []; if (isset($props['category'])) { $categoriesOptions[] = ['slug', $props['category']]; } if (isset($props['subcategory'])) { $categoriesOptions[] = ['slug', $props['subcategory']]; } if (count($categoriesOptions) > 0) { $categories = \Subbly\Subbly::api('subbly.category')->all(['has' => ['translations' => $categoriesOptions]]); } else { $hasCategories = false; } } if ($hasCategories && count($categories)) { $productsOptions['has']['categories'] = []; foreach ($categories as $category) { $productsOptions['has']['categories'][] = ['category_id', $category->id]; } } // Get products // ----------------- $products = \Subbly\Subbly::api('subbly.product')->all($productsOptions)->toArray(); $buffer = ''; if (!$products) { $template->setStopToken('else'); $template->discard(); $template->setStopToken(false); $buffer = $template->render($context); } elseif (is_array($products) || $products instanceof \Traversable) { $isList = is_array($products) && array_keys($products) === range(0, count($products) - 1); $index = 0; $lastIndex = $isList ? count($products) - 1 : false; foreach ($products as $key => $var) { $specialVariables = array('@index' => $index, '@first' => $index === 0, '@last' => $index === $lastIndex); if (!$isList) { $specialVariables['@key'] = $key; } $context->pushSpecialVariables($specialVariables); $context->push($var); $template->setStopToken('else'); $template->rewind(); $buffer .= $template->render($context); $context->pop(); $context->popSpecialVariables(); $index++; } $template->setStopToken(false); } return $buffer; }