Example #1
0
 function _save(Recipe $recipe = NULL)
 {
     /* Success on validation */
     # Zagruzka Recepta
     if ($recipe == NULL) {
         $recipe = new Recipe();
     }
     $recipe_image = new Recipe_Image();
     $recipe->prepare_time = $this->input->post('prep_time');
     $recipe->cook_time = $this->input->post('cook_time');
     $recipe->servings = $this->input->post('servings');
     $total_products = $this->input->post('total_products');
     #find filling fields of product and create array $product_name
     if ($this->save_object_name($recipe)) {
         #RECIPE IMAGE
         #esli net svazannoi image to sozdaetsa novaia i zadaetsa id
         if ($recipe->recipe_image->result_count() == 0) {
             $recipe_image_id = $recipe->recipe_image->result_count() + 1;
             $recipe_image = new Recipe_Image();
             $recipe_image->image_type = 1;
         } else {
             $recipe_image_id = $recipe->recipe_image->id;
             #beret sushestvushii ID image is Recipes_Image
         }
         #esli my_upload_image_lib proinicializirovalas' verno to image zagrujaetca i resize
         $this->upload_image_lib->initialize(array('type' => 'recipe', 'size' => 'tiny'));
         #vozvrashaet polnoe ima kartinki primer: re_id_id.jpg; v bazu sohranaetsa tol'ko $recipe_image_id
         if ($this->upload_image_lib->upload_resize_img('recipe_image', $recipe->id . '_' . $recipe_image_id)) {
             $recipe_image->save($recipe);
         }
         #else
         #$this->data['form_error'] = $this->upload->display_errors(); #vivodit log oshibok
         #PRODUKTI
         #zagruzka udaleniih productov
         $products_remove_selected = $this->input->post('hidden_product_removed');
         if (!$products_remove_selected) {
             $products_remove_selected = array();
         }
         for ($number = 1; $number <= $total_products; $number++) {
             $product_name = $this->input->post('product_' . $number);
             $product_qty = $this->input->post('qty_' . $number);
             $product_mera_id = $this->input->post('mera_' . $number);
             #
             if ($product_name && $product_qty && $product_mera_id && !in_array($number, $products_remove_selected)) {
                 $dm_product = new Product();
                 $dm_product->where_related('languages_product', 'name', $product_name)->get();
                 $recipe->save($dm_product);
                 $recipe->set_join_field($dm_product, array('mera_id' => $product_mera_id, 'value' => $product_qty));
             }
         }
         # udalit' vibrannie producti
         $products_name = array();
         #peremenaia spiska productov
         foreach ($products_remove_selected as $number) {
             #sozdaet spisok udalaemih productov
             array_push($products_name, $this->input->post('product_' . $number));
         }
         #esli spisok udalaemih productov ne pust to vipolnaetsa uslovie
         if (!empty($products_name)) {
             $dm_products = new Product();
             $dm_products->where_in_related('languages_product', 'name', $products_name)->get();
             $recipe->delete($dm_products->all);
         }
         #SHAGI
         $total_steps = $this->input->post('total_steps');
         for ($step_number = 1; $step_number <= $total_steps; $step_number++) {
             #Languages
             $total_languages = $this->input->post('total_language_' . $step_number);
             $dm_current_step = dm_get_object_by_field($step_number, $recipe->recipe_step, 'number');
             if (!$dm_current_step) {
                 $dm_current_step = new Recipe_Step();
                 $dm_current_step->number = $step_number;
                 $dm_current_step->save($recipe);
             }
             for ($lang_number = 1; $lang_number <= $total_languages; $lang_number++) {
                 $language_selected = $this->input->post('sel_languages_text_' . $step_number . '_' . $lang_number);
                 $language_text = $this->input->post('inp_text_' . $step_number . '_' . $lang_number);
                 $dm_current_step->save_by_language(array('text' => $language_text), $language_selected);
             }
             #IMAGE
             $form_name = 'step_photo_' . $number;
             $image_name = isset($recipe->recipe_step->id) ? $recipe->id . '_' . $recipe->recipe_step : $recipe->id . '_' . $recipe->recipe_step->count() + 1;
             #
             if ($this->upload_image_lib->initialize(array('type' => 'step', 'size' => 'tiny'))) {
                 $image_name = $this->upload_image_lib->upload_resize_img($form_name, $image_name);
                 if ($image_name) {
                     $recipe->recipe_step->image = $image_name;
                 } else {
                     $data['form_error'] = $this->upload_image_lib->get_errors() . $this->upload->display_errors();
                 }
             }
         }
         $this->data['form_success'] = 'Рецепт Сохранен';
     } else {
         $this->data['form_error'] = $recipe->error->string;
     }
 }