/**
  * Management of search
  *
  */
 public function manage()
 {
     if (!isset($this->wsObject->urlFragments['query']) || !isset($this->wsObject->urlFragments['language'])) {
         throw new WebserviceException('You have to set both the \'language\' and \'query\' parameters to get a result', array(100, 400));
     }
     $objects_products = array();
     $objects_categories = array();
     $objects_products['empty'] = new Product();
     $objects_categories['empty'] = new Category();
     $this->_resourceConfiguration = $objects_products['empty']->getWebserviceParameters();
     if (!$this->wsObject->setFieldsToDisplay()) {
         return false;
     }
     $results = Search::find($this->wsObject->urlFragments['language'], $this->wsObject->urlFragments['query'], 1, 1, 'position', 'desc', true, false);
     $categories = array();
     foreach ($results as $result) {
         $current = new Product($result['id_product']);
         $objects_products[] = $current;
         $categories_result = $current->getWsCategories();
         foreach ($categories_result as $category_result) {
             foreach ($category_result as $id) {
                 $categories[] = $id;
             }
         }
     }
     $categories = array_unique($categories);
     foreach ($categories as $id) {
         $objects_categories[] = new Category($id);
     }
     $this->output .= $this->objOutput->getContent($objects_products, null, $this->wsObject->fieldsToDisplay, $this->wsObject->depth, WebserviceOutputBuilder::VIEW_LIST, false);
     // @todo allow fields of type category and product
     // $this->_resourceConfiguration = $objects_categories['empty']->getWebserviceParameters();
     // if (!$this->setFieldsToDisplay())
     // return false;
     $this->output .= $this->objOutput->getContent($objects_categories, null, $this->wsObject->fieldsToDisplay, $this->wsObject->depth, WebserviceOutputBuilder::VIEW_LIST, false);
 }
Example #2
0
 /**
  * Thanks to the (WebserviceOutputBuilder) WebserviceKey::objOutput
  * Method build the output depend on the WebserviceRequest::outputFormat
  * and set HTTP header parameters.
  *
  * @return array with displaying informations (used in the dispatcher).
  */
 protected function returnOutput()
 {
     $return = array();
     // write headers
     $this->objOutput->setHeaderParams('Access-Time', time())->setHeaderParams('X-Powered-By', 'PrestaShop Webservice')->setHeaderParams('PSWS-Version', _PS_VERSION_)->setHeaderParams('Execution-Time', round(microtime(true) - $this->_startTime, 3));
     $return['type'] = strtolower($this->outputFormat);
     // write this header only now (avoid hackers happiness...)
     if ($this->_authenticated) {
         $this->objOutput->setHeaderParams('PSWS-Version', _PS_VERSION_);
     }
     // If Specific Management is asked
     if ($this->objectSpecificManagement instanceof WebserviceSpecificManagementInterface) {
         try {
             $return['content'] = $this->objectSpecificManagement->getContent();
         } catch (WebserviceException $e) {
             if ($e->getType() == WebserviceException::DID_YOU_MEAN) {
                 $this->setErrorDidYouMean($e->getStatus(), $e->getMessage(), $e->getWrongValue(), $e->getAvailableValues(), $e->getCode());
             } elseif ($e->getType() == WebserviceException::SIMPLE) {
                 $this->setError($e->getStatus(), $e->getMessage(), $e->getCode());
             }
         }
     }
     // for use a general output
     if (!$this->hasErrors() && $this->objectSpecificManagement == null) {
         if (empty($this->objects)) {
             try {
                 $return['content'] = $this->objOutput->getResourcesList($this->keyPermissions);
             } catch (WebserviceException $e) {
                 if ($e->getType() == WebserviceException::DID_YOU_MEAN) {
                     $this->setErrorDidYouMean($e->getStatus(), $e->getMessage(), $e->getWrongValue(), $e->getAvailableValues(), $e->getCode());
                 } elseif ($e->getType() == WebserviceException::SIMPLE) {
                     $this->setError($e->getStatus(), $e->getMessage(), $e->getCode());
                 }
             }
         } else {
             try {
                 if (isset($this->urlSegment[1]) && !empty($this->urlSegment[1])) {
                     $type_of_view = WebserviceOutputBuilder::VIEW_DETAILS;
                 } else {
                     $type_of_view = WebserviceOutputBuilder::VIEW_LIST;
                 }
                 if (in_array($this->method, array('PUT', 'POST'))) {
                     $type_of_view = WebserviceOutputBuilder::VIEW_DETAILS;
                     $this->fieldsToDisplay = 'full';
                 }
                 $return['content'] = $this->objOutput->getContent($this->objects, $this->schemaToDisplay, $this->fieldsToDisplay, $this->depth, $type_of_view);
             } catch (WebserviceException $e) {
                 if ($e->getType() == WebserviceException::DID_YOU_MEAN) {
                     $this->setErrorDidYouMean($e->getStatus(), $e->getMessage(), $e->getWrongValue(), $e->getAvailableValues(), $e->getCode());
                 } elseif ($e->getType() == WebserviceException::SIMPLE) {
                     $this->setError($e->getStatus(), $e->getMessage(), $e->getCode());
                 }
             } catch (Exception $e) {
                 $this->setError(500, $e->getMessage(), $e->getCode());
             }
         }
     }
     // if the output is not enable, delete the content
     // the type content too
     if (!$this->_outputEnabled) {
         if (isset($return['type'])) {
             unset($return['type']);
         }
         if (isset($return['content'])) {
             unset($return['content']);
         }
     } elseif (isset($return['content'])) {
         $this->objOutput->setHeaderParams('Content-Sha1', sha1($return['content']));
     }
     // if errors happends when creating returned xml,
     // the usual xml content is replaced by the nice error handler content
     if ($this->hasErrors()) {
         $this->_outputEnabled = true;
         $return['content'] = $this->objOutput->getErrors($this->errors);
     }
     if (!isset($return['content']) || strlen($return['content']) <= 0) {
         $this->objOutput->setHeaderParams('Content-Type', '');
     }
     $return['headers'] = $this->objOutput->buildHeader();
     restore_error_handler();
     return $return;
 }