Esempio n. 1
0
 static function createFaq($faqDto)
 {
     $faq = new Faq(NULL, date("Y-m-d H:i:s", time()), TRUE, $faqDto->pregunta, $faqDto->respuesta, false, $faqDto->orden);
     $transaction = NULL;
     $result = new Result();
     $lastOrden = $faqDto->orden;
     try {
         $faqs = $data = BaseDao::getAllFaq();
         if ($lastOrden == 0 || $lastOrden == null) {
             $lastOrden = Phinq::create($faqs)->max(function ($item) {
                 return $item->orden;
             });
             $faq->orden = $lastOrden == null ? 1 : ++$lastOrden->orden;
         } else {
             $faqsFiltered = Phinq::create($faqs)->where(function ($item) use($faqDto) {
                 return $item->orden >= $faqDto->orden;
             })->orderBy(function ($item) {
                 return $item->orden;
             })->toArray();
             foreach ($faqsFiltered as $cf) {
                 $lastOrden++;
                 $cf->orden = $lastOrden;
                 BaseDao::updateFaq($cf, $transaction);
             }
         }
         $transaction = GenericDao::beginTransaction();
         BaseDao::createFaq($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);
 }