Esempio n. 1
0
 /**
  * Execute the helper
  * {{CartHelper}}
  *
  * @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 = '';
     $cart = \Subbly\Subbly::api('subbly.cart')->content()->toArray();
     if (!$cart) {
         $template->setStopToken('else');
         $template->discard();
         $template->setStopToken(false);
         $buffer = $template->render($context);
     } elseif (is_array($cart) || $cart instanceof \Traversable) {
         $isList = is_array($cart) && array_keys($cart) === range(0, count($cart) - 1);
         $index = 0;
         $lastIndex = $isList ? count($cart) - 1 : false;
         foreach ($cart 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;
 }
Esempio n. 2
0
 public function testUpdate()
 {
     $setting = Subbly::api('subbly.setting')->all()->take(1)->toArray();
     $settingValue = reset($setting);
     $settingKey = key($setting);
     /**
      * NOT OK
      */
     // "setting" not defined
     $response = $this->callJSON('PATCH', "/api/v1/settings/{$settingKey}");
     $this->assertResponseStatus(400);
     $this->assertResponseJSONValid();
     $json = $this->getJSONContent();
     $this->assertObjectHasAttribute('error', $json->response);
     /**
      * OK
      */
     $settingKey = 'test.subbly.string_setting';
     // Test with string setting and with wrong array value
     $response = $this->callJSON('PATCH', "/api/v1/settings/{$settingKey}", array('value' => array()));
     $this->assertResponseStatus(400);
     $this->assertResponseJSONValid();
     $json = $this->getJSONContent();
     $this->assertObjectHasAttribute('error', $json->response);
     // "settings" with datas
     $value = TestCase::faker()->word;
     $response = $this->callJSON('PATCH', "/api/v1/settings/{$settingKey}", array('value' => $value));
     $this->assertResponseOk();
     $this->assertResponseJSONValid();
     // $json = $this->getJSONContent();
 }
Esempio n. 3
0
 /**
  * Update a Product
  *
  * @route PUT|PATCH /api/v1/products/:sku
  * @authentication required
  */
 public function update($sku)
 {
     if (!Input::has('product')) {
         return $this->jsonErrorResponse('"product" is required.');
     }
     $product = Subbly::api('subbly.product')->update($sku, Input::get('product'));
     return $this->jsonResponse(array('product' => $product), array('status' => array('message' => 'Product updated')));
 }
Esempio n. 4
0
 /**
  * Update a Order.
  *
  * @route PUT|PATCH /api/v1/orders/:id
  * @authentication required
  */
 public function update($sku)
 {
     if (!Input::has('order')) {
         return $this->jsonErrorResponse('"order" is required.');
     }
     $order = Subbly::api('subbly.order')->update($sku, Input::get('order'));
     return $this->jsonResponse(array('order' => $this->presenter->single($order)), array('status' => array('message' => 'Order updated')));
 }
Esempio n. 5
0
 /**
  *
  */
 private function uploadImage(ProductImage $model)
 {
     if (!$model->imageToUpload instanceof UploadedFile) {
         return;
     }
     $source = Subbly::getContainer()->get('media_resolver')->getSource($model, array('field' => 'filename'));
     $model->imageToUpload->move(dirname($source), $model->filename);
 }
Esempio n. 6
0
 /**
  * Update a Setting
  *
  * @route PUT|PATCH /api/settings/{setting_key}
  * @authentication required
  */
 public function update($setting_key)
 {
     if (!Input::has('value')) {
         return $this->jsonErrorResponse('"value" is required.');
     }
     $user = Subbly::api('subbly.setting')->update($setting_key, Input::get('value'));
     return $this->jsonResponse(array(), array('status' => array('code' => 200, 'message' => 'Setting updated')));
 }
Esempio n. 7
0
 /**
  * Update a User.
  *
  * @route PUT|PATCH /api/v1/users/:uid
  * @authentication required
  */
 public function update($uid)
 {
     if (!Input::has('user')) {
         return $this->jsonErrorResponse('"user" is required.');
     }
     $user = Subbly::api('subbly.user')->update($uid, Input::get('user'));
     return $this->jsonResponse(array('user' => $this->presenter->single($user)), array('status' => array('code' => 200, 'message' => 'User updated')));
 }
 /**
  * Set Product order.
  *
  * @route POST /api/v1/products/{sku}/images/sort
  * @authentication required
  */
 public function sort($product_sku)
 {
     if (!Input::has('sortable')) {
         return $this->jsonErrorResponse('"sortable" is required.');
     }
     $images = Subbly::api('subbly.product_image')->sort(Input::get('sortable'));
     return $this->jsonResponse(array(), array('status' => array('message' => 'Product updated')));
 }
Esempio n. 9
0
 public function showWelcome()
 {
     // Subbly\Subbly::init();
     // echo Config::get('subbly.backendUri', 'backend');
     $credentials = array('login' => '*****@*****.**', 'password' => 'michael');
     $authenticated = Subbly::api('subbly.user')->authenticate($credentials);
     var_dump($authenticated);
     // return View::make('hello');
 }
Esempio n. 10
0
 /**
  * Update a Setting.
  *
  * @route PUT|PATCH /api/settings
  * @authentication required
  */
 public function update()
 {
     if (!Input::has('settings')) {
         return $this->jsonErrorResponse('"settings" is required.');
     }
     $settings = Subbly::api('subbly.setting');
     $user = $settings->updateMany(Input::get('settings'));
     return $this->jsonResponse(array('settings' => $settings->all()), array('status' => array('code' => 200, 'message' => 'Settings updated')));
 }
Esempio n. 11
0
 /**
  * Execute the helper
  * {{CartCountHelper}}
  *
  * @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 );
     // if( count( $args ) != 1 )
     //   throw new \InvalidArgumentException(
     //       '"assets" helper expects exactly one argument.'
     //   );
     return Subbly::api('subbly.cart')->count();
 }
Esempio n. 12
0
 /**
  * Get formated datas for a collection.
  *
  * @param \Subbly\Model\Collection $collection
  *
  * @return \Illuminate\Support\Collection
  */
 public function collection(Collection $collection)
 {
     $entries = new Entries();
     foreach ($collection as $userAddress) {
         $entry = new Entry($userAddress);
         $entry->conditionalField('id', function () {
             return Subbly::api('subbly.user')->hasAccess('subbly.backend.auth');
         })->field('uid')->field('name')->field('firstname')->field('lastname')->field('address1')->field('address2')->field('zipcode')->field('city')->field('country')->field('phone_work')->field('phone_home')->field('phone_mobile')->field('others_informations')->dateField('created_at')->dateField('updated_at');
         $entries->addEntry($entry);
     }
     return $entries->toArray();
 }
Esempio n. 13
0
 /**
  * Get formated datas for a collection.
  *
  * @param \Subbly\Model\Collection $collection
  *
  * @return \Illuminate\Support\Collection
  */
 public function collection(Collection $collection)
 {
     $entries = new Entries();
     foreach ($collection as $productImage) {
         $entry = new Entry($productImage);
         $entry->conditionalField('id', function () {
             return Subbly::api('subbly.user')->hasAccess('subbly.backend.auth');
         })->field('filename')->field('product')->field('uid')->field('position')->dateField('created_at')->dateField('updated_at');
         $entries->addEntry($entry);
     }
     return $entries->toArray();
 }
Esempio n. 14
0
 /**
  * Get formated datas for a collection.
  *
  * @param \Subbly\Model\Collection $collection
  *
  * @return \Illuminate\Support\Collection
  */
 public function collection(Collection $collection)
 {
     $entries = new Entries();
     foreach ($collection as $order) {
         $entry = new Entry($order);
         $entry->conditionalField('id', function () {
             return Subbly::api('subbly.user')->hasAccess('subbly.backend.auth');
         })->field('uid')->field('status')->decimal('total_price', (double) $order->total_price)->integer('total_items', (int) $order->total_items)->decimal('shipping_cost', (double) $order->shipping_cost)->relationshipField('user', 'Subbly\\Presenter\\V1\\UserPresenter')->relationshipField('billing_address', 'Subbly\\Presenter\\V1\\OrderAddressPresenter')->relationshipField('shipping_address', 'Subbly\\Presenter\\V1\\OrderAddressPresenter')->relationshipField('products', 'Subbly\\Presenter\\V1\\OrderProductPresenter')->dateField('created_at')->dateField('updated_at');
         $entries->addEntry($entry);
     }
     return $entries->toArray();
 }
Esempio n. 15
0
 /**
  * Get formated datas for a collection.
  *
  * @param \Subbly\Model\Collection $collection
  *
  * @return \Illuminate\Support\Collection
  */
 public function collection(Collection $collection)
 {
     $entries = new Entries();
     foreach ($collection as $user) {
         $entry = new Entry($user);
         $entry->conditionalField('id', function () {
             return Subbly::api('subbly.user')->hasAccess('subbly.backend.auth');
         })->field('uid')->field('email')->field('firstname')->field('lastname')->compositeField('displayName', 'firstname', 'lastname')->relationshipField('addresses', 'Subbly\\Presenter\\V1\\UserAddressPresenter')->integer('orders_total', $user->countOrders())->relationshipField('orders', 'Subbly\\Presenter\\V1\\OrderPresenter')->relationshipField('groups', 'Subbly\\Presenter\\V1\\GroupPresenter')->dateField('created_at')->dateField('updated_at');
         $entries->addEntry($entry);
     }
     return $entries->toArray();
 }
Esempio n. 16
0
 /**
  * Get formated datas for a collection.
  *
  * @param \Subbly\Model\Collection $collection
  *
  * @return \Illuminate\Support\Collection
  */
 public function collection(Collection $collection)
 {
     $entries = new Entries();
     foreach ($collection as $group) {
         $entry = new Entry($group);
         $entry->conditionalField('id', function () {
             return Subbly::api('subbly.user')->hasAccess('subbly.backend.auth');
         })->field('name')->field('permissions')->dateField('created_at')->dateField('updated_at');
         $entries->addEntry($entry);
     }
     return $entries->toArray();
 }
 /**
  * Create/Update Product's 'categories.
  *
  * @route POST/PUT /api/v1/products/:product_sku/categories
  * @authentication required
  */
 private function manage($product_sku)
 {
     $product = Subbly::api('subbly.product')->find($product_sku);
     if (!Input::has('product_category')) {
         return $this->jsonErrorResponse('"product_category" is required.');
     }
     if (!is_array(Input::get('product_category'))) {
         return $this->jsonErrorResponse('"product_category" must be an array.');
     }
     Subbly::api('subbly.product_category')->create(Input::get('product_category'), $product);
     return $this->jsonResponse(array(), array('status' => array('code' => 201, 'message' => 'ProductCategory created')));
 }
Esempio n. 18
0
 /**
  * Get formated datas for a collection.
  *
  * @param \Subbly\Model\Collection $collection
  *
  * @return \Illuminate\Support\Collection
  */
 public function collection(Collection $collection)
 {
     $entries = new Entries();
     foreach ($collection as $product) {
         $entry = new Entry($product);
         $entry->conditionalField('id', function () {
             return Subbly::api('subbly.user')->hasAccess('subbly.backend.auth');
         })->field('position')->field('name')->field('description')->field('sku')->field('status')->field('price')->field('sale_price')->field('quantity')->relationshipField('images', 'Subbly\\Presenter\\V1\\ProductImagePresenter')->relationshipField('options', 'Subbly\\Presenter\\V1\\ProductOptionPresenter')->relationshipField('categories', 'Subbly\\Presenter\\V1\\ProductCategoryPresenter')->dateField('created_at')->dateField('updated_at');
         $entries->addEntry($entry);
     }
     return $entries->toArray();
 }
Esempio n. 19
0
 public function testApi()
 {
     $api = Subbly::api();
     $this->assertInstanceOf('Subbly\\Api\\Api', $api);
     $service = Subbly::api('subbly.user');
     $this->assertInstanceOf('Subbly\\Api\\Service\\Service', $service);
     $service = Subbly::api()->service('subbly.user');
     $this->assertInstanceOf('Subbly\\Api\\Service\\Service', $service);
     try {
         Subbly::api('subbly.a_very_wrong_service');
         $this->fail('Subbly\\Api\\Exception has not be raised.');
     } catch (\Subbly\Api\Exception $e) {
         $this->assertTrue(true);
     }
 }
Esempio n. 20
0
 /**
  * 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)
 {
     $buffer = '';
     $cart = \Subbly\Subbly::api('subbly.cart')->count();
     if (!count($cart)) {
         $template->setStopToken('else');
         $template->discard($context);
         $template->setStopToken(false);
         $buffer = $template->render($context);
     } else {
         $template->setStopToken('else');
         $buffer = $template->render($context);
         $template->setStopToken(false);
         $template->discard($context);
     }
     return $buffer;
 }
Esempio n. 21
0
 /**
  * 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;
 }
Esempio n. 22
0
 /**
  * Execute the helper
  * {{#isUserLogin}}
  *
  * @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 = Subbly::api('subbly.user')->check();
     if (!$tmp) {
         $template->setStopToken('else');
         $buffer = $template->render($context);
         $template->setStopToken(false);
         $template->discard($context);
     } else {
         $context->push(['user' => Subbly::api('subbly.user')->currentUser()]);
         $template->setStopToken('else');
         $template->discard($context);
         $template->setStopToken(false);
         $buffer = $template->render($context);
     }
     $context->pop();
     return $buffer;
 }
Esempio n. 23
0
 /**
  * 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;
 }
Esempio n. 24
0
 public function testUpdate()
 {
     $faker = TestCase::faker();
     $user = TestCase::getFixture('users.user_1');
     // Events
     Subbly::events()->listen($this->getService()->name() . ':updating', function ($model) use($user) {
         $this->assertEquals($user->id, $model->id);
         Subbly::events()->forget($this->getService()->name() . ':updating');
     });
     Subbly::events()->listen($this->getService()->name() . ':updated', function ($model) use($user) {
         $this->assertEquals($user->id, $model->id);
         Subbly::events()->forget($this->getService()->name() . ':updated');
     });
     $user->firstname = $faker->firstname;
     $user->lastname = $faker->lastname;
     $returnedUser = $this->getService()->update($user);
     $this->assertInstanceOf('Subbly\\Model\\User', $user);
     $this->assertInstanceOf('Subbly\\Model\\User', $returnedUser);
     $this->assertEquals($user->id, $returnedUser->id);
 }
Esempio n. 25
0
 /**
  * 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;
 }
Esempio n. 26
0
 /**
  * Perform file(s) upload to the server.
  *
  * @route POST /api/v1/uploader
  * @authentication required
  */
 public function store()
 {
     // TODO: to clean
     if (!Input::hasFile('file')) {
         return $this->jsonErrorResponse('"file" is required.');
     }
     Subbly::events()->fire('subbly.upload:creating', array());
     $file = Input::file('file');
     $file_type = Input::get('file_type', 'product_image');
     $destination = app_upload($file_type);
     $publicPath = public_upload($file_type);
     $filename = sprintf('%s.%s', uniqid(), $file->getClientOriginalExtension());
     $inputFile = Input::all();
     unset($inputFile['file']);
     $fileData = array('file' => array_merge($inputFile, array('filename' => $filename, 'file_path' => sprintf('%s/%s', $publicPath, $filename))));
     $uploadSuccess = $file->move($destination, $filename);
     if ($uploadSuccess) {
         Subbly::events()->fire('subbly.upload:created', $fileData);
     } else {
         Subbly::events()->fire('subbly.upload:error', $fileData);
     }
     return $this->jsonResponse($fileData, array('status' => array('code' => 201, 'message' => 'Upload done')));
 }
Esempio n. 27
0
 private function getService()
 {
     return Subbly::api('subbly.product_image');
 }
Esempio n. 28
0
 /**
  * Create a new Order.
  *
  * @example
  *     $order = Subbly\Model\Order;
  *     Subbly::api('subbly.order')->create($order);
  *
  *     Subbly::api('subbly.order')->create(array(
  *         'firstname' => 'Jon',
  *         'lastname'  => 'Snow',
  *     ));
  *
  * @param \Subbly\Model\Order|array $order
  *
  * @return \Subbly\Model\Order
  *
  * @api
  */
 public function create($order, $cart, $shipping, $billing = null)
 {
     if (is_array($order)) {
         $order = new Order($order);
     }
     if ($order instanceof Order) {
         if ($this->fireEvent('creating', array($order)) === false) {
             return false;
         }
         $order->setCaller($this);
         \DB::transaction(function () use($order, $cart, $shipping, $billing) {
             $shipping = \Subbly\Subbly::api('subbly.orderaddress')->create($shipping);
             $order->shipping_address_id = $shipping->id;
             if ($billing) {
                 $billing = \Subbly\Subbly::api('subbly.orderaddress')->create($billing);
                 $order->billing_address_id = $billing->id;
             }
             $order->save();
             foreach ($cart as $item) {
                 // TODO: add product sku, name and description in current locale.
                 // work as cache if product is not available later
                 $product = ['order_id' => $order->id, 'product_id' => $item['id'], 'price' => $item['price'], 'name' => $item['name'], 'sku' => $item['sku'], 'sale_price' => 0, 'quantity' => $item['qty'], 'options' => json_encode($item['options'])];
                 $ret = \Subbly\Subbly::api('subbly.orderproduct')->create($product);
             }
         });
         $this->fireEvent('created', array($order));
         // $order = $this->find($order->id);
         return $order;
     }
     throw new Exception(sprintf(Exception::CANT_CREATE_MODEL, 'Subbly\\Model\\Order', $this->name()));
 }
Esempio n. 29
0
 private function getService()
 {
     return Subbly::api('subbly.setting');
 }
Esempio n. 30
0
 /**
  *
  */
 protected function getCurrentUser()
 {
     return Subbly::api('subbly.user')->currentUser();
 }