/**
  * product options.
  *
  * @param string $id
  *
  * @return Response
  */
 public function options(Request $request, string $id)
 {
     $product = Product::find($id);
     // get current options
     $options = isset($product->options) ? $product->options : [];
     $options = Option::whereIn('_id', $options)->get()->toArray();
     // get existing options - minus ones already present
     $current = [];
     foreach ($options as $option) {
         $current[] = $option['_id'];
     }
     $availableOptions = Option::all()->except($current);
     return view('admin/products/options', ['product' => $product, 'options' => $options, 'availableOptions' => $availableOptions]);
 }
 /**
  * Show Product.
  *
  * @return Response
  */
 public function show(Request $request, $slug)
 {
     $product = Product::where('slug', $slug)->first();
     // product options
     if ($product->options) {
         $product->options = Option::whereIn('_id', $product->options)->get();
         if ($product->option_values) {
             $first = $product->option_values;
             $product->first = reset($first);
         }
     }
     // load only available options based on selected
     $available = [];
     if ($product->option_values) {
         foreach ($product->option_values as $option) {
             foreach ($option['options'] as $key => $opt) {
                 $available[$key][$opt] = [];
                 //$this->available($product, $key, $opt) ;
             }
         }
     }
     // product images
     $file_size = key(array_reverse(config('image.image_sizes')));
     //smallest
     $product->files = $this->getFiles('images/products/' . $product->id . '/' . $file_size);
     return view('themes/kudos/products/show', ['product' => $product, 'available' => $available]);
 }
示例#3
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $id = $this->argument('blueprint');
     $type = $this->argument('type');
     $full = (int) $this->argument('full');
     /// $type, public_path('csv')
     $path = $full ? storage_path('app') : public_path('csv');
     $user = Auth::user();
     $blueprint = Blueprint::with("questions.options")->find($id);
     $title = $this->sluggable($blueprint->title);
     Excel::create($title, function ($excel) use($blueprint, $full) {
         // Set the title
         $excel->setTitle($blueprint->title);
         // Chain the setters
         $excel->setCreator('Tú Evalúas');
         //->setCompany('Transpar');
         // Call them separately
         $excel->setDescription("Resultado desagregados");
         // add a sheet for each day, and set the date as the name of the sheet
         $excel->sheet("encuestas", function ($sheet) use($blueprint, $full) {
             //var_dump($titles->toArray());
             $questions = $blueprint->questions;
             $titles = $questions->pluck("question");
             $titles = $titles->toArray();
             array_unshift($titles, "id");
             $sheet->appendRow($titles);
             $applicants = $blueprint->applicants()->has("answers")->with("answers")->get();
             foreach ($applicants as $applicant) {
                 $row = [];
                 $row[] = $applicant->id;
                 foreach ($questions as $question) {
                     if ($question->is_description) {
                         $row[] = "es descripción";
                     } elseif (in_array($question->type, ['location-a', 'location-b', 'location-c'])) {
                         $inegi_key = $applicant->answers()->where("question_id", $question->id)->get()->first();
                         $row[] = $this->find_location($question->type, $inegi_key);
                     } elseif ($question->type == "multiple-multiple") {
                         $open_answer = $applicant->answers()->where("question_id", $question->id)->get()->first();
                         if ($open_answer && !empty($open_answer->text_value)) {
                             $r = Option::whereIn("value", explode(",", $open_answer->text_value))->where("blueprint_id", $blueprint->id)->where("question_id", $question->id)->lists('description')->toArray();
                             $row[] = implode(",", $r);
                         } else {
                             $row[] = "no contestó";
                         }
                     } elseif (in_array($question->type, ['text', 'multiple'])) {
                         $open_answer = $applicant->answers()->where("question_id", $question->id)->get()->first();
                         //->text_value;
                         $row[] = $open_answer ? $open_answer->text_value : "no contestó";
                     } elseif ($question->type == "personal") {
                         $open_answer = $applicant->answers()->where("question_id", $question->id)->get()->first();
                         //->text_value;
                         $row[] = $open_answer && $full ? $open_answer->text_value : "";
                     } else {
                         $num_value = $applicant->answers()->where("question_id", $question->id)->get()->first();
                         //->text_value;
                         $row[] = $num_value ? $num_value->num_value : "-";
                     }
                 }
                 $sheet->appendRow($row);
             }
         });
     })->store($type, $path);
     $blueprint->csv_file = $title;
     $blueprint->update();
     $this->info('El archivo se guardó!');
 }