Example #1
0
 static function updateFaq($faqDto)
 {
     $faq = BaseDao::getFaqById($faqDto->id);
     $result = new Result();
     try {
         $transaction = GenericDao::beginTransaction();
         $faq->pregunta = $faqDto->pregunta;
         $faq->respuesta = $faqDto->respuesta;
         $faq->enabled = $faqDto->enabled;
         $faq->updateDate = date("Y-m-d H:i:s", time());
         if ($faq->orden != $faqDto->orden) {
             $faqs = $data = BaseDao::getAllFaq();
             $faqsFiltered = Phinq::create($faqs)->orderBy(function ($item) {
                 return $item->orden;
             })->where(function ($item) use($faqDto) {
                 return $item->id != $faqDto->id && $item->orden >= $faqDto->orden;
             })->toArray();
             $firstOrden = $faqDto->orden;
             foreach ($faqsFiltered as $cf) {
                 $firstOrden++;
                 $cf->orden = $firstOrden;
                 BaseDao::updateFaq($cf, $transaction);
             }
         }
         $faq->orden = $faqDto->orden;
         BaseDao::updateFaq($faq, $transaction);
         $transaction->commit();
     } catch (\Exception $ex) {
         $transaction->rollBack();
         $result->hasErrors = true;
         $result->messages = array($ex->getMessage());
     }
     $response = new Response($result, NULL);
     return json_encode($response);
 }