/**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store(Request $request)
 {
     $title = strtolower($request->get('title'));
     $user_id = $request->user()->id;
     if (!empty($title)) {
         $serial = Serial::where('title', $title)->get();
         // Сериал с таким названием уже был создан ранее
         if ($serial->count()) {
             $catalog = Catalog::where(['user_id' => $user_id, 'serial_id' => $serial->first()->id])->get();
             // У пользователя уже есть в списке этот сериал
             if ($catalog->count()) {
                 return response(['message' => 'У Вас уже есть сериал с таким названием'], 422)->header('Content-Type', 'application/json');
             } else {
                 // Иначе добавляем пользователю в каталог сериал
                 $catalog = new Catalog();
                 $catalog->user_id = $user_id;
                 $catalog->serial_id = $serial->first()->id;
                 $catalog->title = $title;
                 $catalog->season = 0;
                 $catalog->serie = 0;
                 $catalog->save();
                 return response()->json($catalog->toArray());
             }
             // Такого сериала еще не создано, поэтому создаем запись,
             // и добавляем в каталог пользователя
         } else {
             $new_serial = new Serial();
             $new_serial->title = $title;
             $new_serial->user_id = $user_id;
             $new_serial->save();
             $catalog = new Catalog();
             $catalog->user_id = $user_id;
             $catalog->serial_id = $new_serial->id;
             $catalog->title = $title;
             $catalog->season = 0;
             $catalog->serie = 0;
             $catalog->save();
             return response()->json($catalog->toArray());
         }
         return $serial;
     } else {
         return response(['message' => 'Поле не может быть пустым'], 422)->header('Content-Type', 'application/json');
     }
 }
Example #2
0
 public function isValid($data)
 {
     $tamanio = 0;
     $valor = 1;
     if ($data['entrada'] == 1) {
         $campo = 'pk_referencia';
     } else {
         $campo = 'folio_ds';
     }
     $consulta = \DB::connection('users')->table($data['tabla'])->where($campo, $data['referencia'])->get();
     if ($consulta != null) {
         foreach ($consulta as $query) {
             //============================== Validaciones ================================
             if ($data['attr_id'] == 4) {
                 if ($query->{$data}['campos'] == '') {
                     $valor = 0;
                 }
             }
             if ($data['attr_id'] == 2) {
                 $campo = explode(",", $data['campos']);
                 if ($data['attr_id'] == 2) {
                     for ($i = 0; $i < count($campo); $i++) {
                         $tamanio += strlen($query->{$campo}[$i]);
                     }
                     if ($tamanio != $data['data_val']) {
                         $valor = 0;
                     }
                 } else {
                     $campos = $campo[0];
                 }
             }
             if ($data['attr_id'] == 6 || $data['attr_id'] == 5) {
                 $catalogo = explode("|", $data['data_val']);
                 $tablaval = $catalogo[0];
                 $campoval = $catalogo[1];
                 Session::put('tablacat', $tablaval);
                 if ($data['attr_id'] == 5) {
                     $formula = explode(',', $catalogo[1]);
                     $campobd = $formula[0];
                     $operador = $formula[1];
                     $result = $formula[2];
                     $camposwh = Anexo22::find($result)->relationships->where('origin_id', $data['entrada'])->first();
                     $campowh = explode(',', $camposwh->field);
                     $val_campo = Catalog::where($catalogo[2], 'LIKE', '%' . $query->{$data}['campos'] . '%')->where($campobd, $query->{$campowh}[0])->count();
                 }
                 if ($data['attr_id'] == 6) {
                     $campo = explode(',', $data['campos']);
                     $campocat = $campo[0];
                     $val_campo = Catalog::where($campoval, $query->{$campocat})->count();
                 }
                 if ($val_campo == 0) {
                     $valor = 0;
                 }
             }
             if ($data['attr_id'] == 3) {
                 $camposval = explode(",", $data['data_val']);
                 $valanexo = Anexo22::find($camposval[0]);
                 $campanx = $valanexo->a22_field;
                 if ($query->{$campanx} != $camposval[1]) {
                     $valor = 0;
                 }
             }
             if ($data['attr_id'] == 8) {
                 //$camposval = explode(",", $data['campos']);
                 $valores = explode("|", $data['data_val']);
                 $identif = $valores[1];
                 $docum = $valores[0];
                 $documentos = \DB::connection('users')->table('opauimg')->where('pk_referencia', $data['referencia'])->where('imgtipodoc', $docum)->count();
                 if ($query->{$data}['campos'] != $identif && $documentos == 0) {
                     $valor = 0;
                 }
             }
         }
     }
     return $valor;
 }