Ejemplo n.º 1
0
 public function store(Request $request)
 {
     try {
         $type = $request->input('type');
         $e = EntityType::findOrFail($type);
         $n = new Entity();
         $n->type_id = $e->id;
         $n->name = $request->input('name');
         $n->save();
         foreach ($e->attributes as $attribute) {
             $name = $attribute->systemName();
             if ($request->has($name)) {
                 $a = new Attribute();
                 $a->entity_id = $n->id;
                 $a->type_id = $attribute->id;
                 $a->value = $request->input($name);
                 $a->save();
             }
         }
         Session::flash('message_type', 'success');
         Session::flash('message', 'Nuovo elemento salvato correttamente.');
     } catch (\Expection $e) {
         Session::flash('message_type', 'danger');
         Session::flash('message', 'Elemento non salvato correttamente.');
     }
     return redirect('entities/' . $e->id);
 }
 /**
  * Register any other events for your application.
  *
  * @param  \Illuminate\Contracts\Events\Dispatcher  $events
  * @return void
  */
 public function boot(DispatcherContract $events)
 {
     parent::boot($events);
     AttributeType::deleting(function ($attr) {
         $sub_attrs = Attribute::where('type_id', '=', $attr->id)->get();
         foreach ($sub_attrs as $sa) {
             $sa->delete();
         }
     });
     EntityType::deleting(function ($entity) {
         $entities = $entity->entities;
         foreach ($entities as $e) {
             $e->delete();
         }
     });
     Entity::deleting(function ($entity) {
         $alarms = $entity->alarms;
         foreach ($alarms as $a) {
             $a->delete();
         }
     });
     Alarm::deleting(function ($alarm) {
         $reminders = $alarm->history;
         foreach ($reminders as $r) {
             $r->delete();
         }
     });
 }
 public function index()
 {
     $campos = Anexo22::lists('a22_name', 'id');
     $atributo = Attribute::lists('name', 'id');
     $default = ['0' => 'Seleccione...'];
     $campos = $default + $campos->toArray();
     $atributo = $default + $atributo->toArray();
     return view('validation')->with(['campos' => $campos, 'atributo' => $atributo]);
 }
Ejemplo n.º 4
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $this->validate($request, ['profile.pitch' => 'required', 'profile.position' => 'required', 'profile.gender' => 'required', 'profile.website' => 'required', 'profile.city' => 'required', 'profile.country' => 'required']);
     $profile = Profile::create(['pitch' => $request->profile['pitch'], 'position' => $request->profile['position'], 'gender' => $request->profile['gender'], 'website' => $request->profile['website'], 'city' => $request->profile['city'], 'country' => $request->profile['country'], 'user_id' => Auth::user()->id]);
     foreach ($request->skills as $skill) {
         $profile->attributes()->save(Attribute::create(['name' => $skill['name'], 'content' => $skill['content'], 'profile_id' => $profile->id]));
     }
     return $profile;
 }
Ejemplo n.º 5
0
 public function attribute($type)
 {
     if (is_string($type)) {
         $name = $type;
     } else {
         $name = $type->slug;
     }
     $a = Attribute::where('entity_id', '=', $this->id)->whereHas('type', function ($query) use($name) {
         $query->where('slug', '=', $name);
     })->first();
     if ($a == null) {
         $a = new Attribute();
         $a->entity_id = $this->id;
         $a->type_id = $this->type->attribute($name)->id;
         $a->value = '';
         $a->save();
     }
     return $a;
 }
Ejemplo n.º 6
0
 public function getAttr($id = null)
 {
     if ($id == null) {
         return ['error' => 'Invalid Request.'];
     }
     $attr = Attribute::where('user_id', $id)->get();
     $content = [];
     foreach ($attr as $eachattr) {
         $content[$eachattr->key] = $eachattr->value;
     }
     return $content;
 }
Ejemplo n.º 7
0
 private function getIterableMails()
 {
     $mails = array();
     $users = User::all();
     foreach ($users as $u) {
         $mails[] = $u->email;
     }
     $attributes = Attribute::whereHas('type', function ($query) {
         $query->where('datatype', '=', 'mail');
     })->get();
     foreach ($attributes as $a) {
         $mails[] = $a->value;
     }
     return $mails;
 }
Ejemplo n.º 8
0
 /**
  * Define your route model bindings, pattern filters, etc.
  *
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 public function boot(Router $router)
 {
     parent::boot($router);
     // Route model binding for Article
     // $router->model('articles', 'App\Article');
     $router->bind('articles', function ($id) {
         return Article::findOrFail($id);
     });
     $router->bind('tags', function ($name) {
         return Tag::whereName($name)->firstOrFail();
     });
     $router->bind('products', function ($id) {
         return Product::findOrFail($id);
     });
     $router->bind('attributes', function ($id) {
         return Attribute::findOrFail($id);
     });
     $router->bind('users', function ($id) {
         return User::findOrFail($id);
     });
 }
Ejemplo n.º 9
0
 public function putUpdate(Request $request, User $user)
 {
     if ($request->header('Authorization') == null) {
         return response()->json(['error' => 'Authorization header not available'], 422);
     }
     try {
         $access_token = $request->header('Authorization');
         $hash = explode(':', $access_token);
         $id = $request->input('id');
         $userInput = $request->input('user');
         /*
          * Check if the header is correct
          */
         if (count($hash) < 2) {
             return response()->json(['error' => 'Invalid Authorization header.'], 422);
         } else {
             if ($id != $hash[0]) {
                 return response()->json(['error' => 'Invalid Request.'], 422);
             }
         }
         $userDet = $user->where('id', $id)->where('access_token', $hash[1])->first();
         /*
          * Check if data can be extracted from the provided header and the id
          */
         if ($userDet) {
             $attribute = new AttributeController();
             $userattr = $userDet->attributes;
             /*
              * Get the attribute list array of a user
              */
             $attrs = $attribute->getInit();
             $content = ['email' => $userDet->email, 'user_id' => $id, 'avatar_image' => $userDet->avatar_image];
             $content = array_merge($content, $attrs);
             /*
              * Update each attribute provided by the user
              */
             foreach ($userInput as $key => $eachattr) {
                 $attrValue = $eachattr;
                 if ($key == 'username') {
                     $userDet->username = $attrValue;
                     $userDet->save();
                 } else {
                     if ($key == 'avatar_image') {
                         if (!$eachattr) {
                             $userDet->avatar_image = "";
                             continue;
                         }
                         $date = date('Y_m_d_H_i_s');
                         $filename = $date . $user->name;
                         $uploadfile = $userDet->upload($attrValue, $filename);
                         $userDet->avatar_image = $uploadfile == 'Error' ? "" : $uploadfile;
                         $userDet->save();
                     } else {
                         $updateAttr = Attribute::where('user_id', $id)->where('key', $key)->first();
                         if (!$updateAttr) {
                             $updateAttr = new Attribute();
                             $updateAttr->user_id = $id;
                             $updateAttr->key = $key;
                         }
                         if (is_array($eachattr)) {
                             $attrValue = json_encode($eachattr);
                         }
                         $updateAttr->value = $attrValue;
                         $updateAttr->save();
                         // $content[$updateAttr->key] = $updateAttr->value;
                     }
                 }
                 $content[$key] = $attrValue;
             }
             foreach ($userattr as $eachattr) {
                 $attrValue = $eachattr->value;
                 if ($eachattr->value == "true") {
                     $attrValue = true;
                 } else {
                     if ($eachattr->value == "false") {
                         $attrValue = false;
                     } else {
                         if ($eachattr->value == "null") {
                             $attrValue = null;
                         } else {
                             if (json_decode($eachattr->value) != null) {
                                 $attrValue = json_decode($eachattr->value);
                             }
                         }
                     }
                 }
                 $content[$eachattr->key] = $attrValue;
             }
             return response()->json($content, 200);
         } else {
             return response()->json(['error' => 'Invalid Request.'], 422);
         }
     } catch (Exception $e) {
         return response()->json(['error' => $e->getMessage()], 422);
     }
 }
Ejemplo n.º 10
0
 private function special_circuimstances_similarity(Product $product)
 {
     $special_cir_user = Attribute::where('user_id', $this->id)->where('key', 'special_circumstances')->get();
     $special_cir_product = knowledge::where('product_id', $product->id)->where('key', 'special_circumstances')->get();
     //Return a score of 0 if the date is missing in any of the user's profile
     if (count($special_cir_product)) {
         $special_cir_product = $special_cir_product->first();
     } else {
         return 50;
     }
     if (count($special_cir_user)) {
         $special_cir_user = $special_cir_user->first();
     } else {
         return 50;
     }
     //Return a score of 0 if the date is missing in any of the user's profile
     $product_circumstances = json_decode($special_cir_product->value, true);
     $user_circumstances = json_decode($special_cir_user->value, true);
     if (array_key_exists('none', $product_circumstances)) {
         return 50;
     }
     if (array_key_exists('none', $user_circumstances)) {
         return 50;
     }
     $similar_count = 0;
     foreach ($product_circumstances as $key => $value) {
         if (array_key_exists($key, $user_circumstances)) {
             $similar_count++;
         }
     }
     $score = round($similar_count * 2 / (count($special_cir_user) + count($special_cir_product)) * 50, 0);
     return $score + 50;
 }
Ejemplo n.º 11
0
 private function get_age_category($user)
 {
     // 0 for age lt 15, 1:15-25 ...
     $usr_dob = Attribute::where('user_id', $user->id)->where('key', 'dob')->get();
     if (!count($usr_dob)) {
         return null;
     }
     $usr_dob = $usr_dob->first()->value;
     $usr_dob = DateTime::createFromFormat('j/n/Y', $usr_dob);
     $today = new DateTime("now");
     $age = date_diff($usr_dob, $today);
     //Calculate the age of the user in terms of years
     $age = $age->y + $age->m / 12;
     if ($age < 15) {
         return 1;
     }
     if ($age > 85) {
         return 8;
     }
     return (int) (round(($age - 15) / 10, 0, PHP_ROUND_HALF_DOWN) + 1);
 }
Ejemplo n.º 12
0
 /**
  * Execute the command.
  *
  * @param AttributeRepository $attributes
  * @return Attribute
  */
 public function handle(AttributeRepository $attributes)
 {
     $attribute = Attribute::register($this->description, $this->type);
     $attributes->save($attribute);
     return $attribute;
 }
Ejemplo n.º 13
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy(Attribute $attribute)
 {
     $attribute->delete();
     \Flash::success('Your attribute has been Deleted');
     return redirect('admin/attributes');
 }
 public function __construct()
 {
     $this->rules['type'] .= '|in:' . implode(',', Attribute::getAllowedTypes());
 }
Ejemplo n.º 15
0
 /**
  * Returns a collection with all attributes.
  *
  * @return Collection
  */
 public function all()
 {
     return Attribute::orderBy('description')->get();
 }
Ejemplo n.º 16
0
 /**
  * Show the form for creating a new resource.
  *
  * @return Response
  */
 public function create()
 {
     return view('attributes.create')->with('allowed_types', Attribute::getAllowedTypes());
 }
Ejemplo n.º 17
0
 function it_provides_a_list_of_possible_types()
 {
     $types = Attribute::getAllowedTypes();
     \PHPUnit_Framework_Assert::assertEquals(['string', 'numeric', 'in'], $types);
 }
Ejemplo n.º 18
0
 private function syncArray(ProductRequest $request)
 {
     $attributes_list = $request->input('attributes_list');
     $attributes = array();
     $attributes_index = 0;
     foreach (Attribute::get() as $key => $value) {
         $attributes[$value->id] = array('value' => $attributes_list[$attributes_index]);
         $attributes_index++;
     }
     return $attributes;
 }
 function it_returns_the_validation_rules()
 {
     $this->getRules()->shouldReturn(['description' => 'required|string', 'type' => 'required|in:' . implode(',', Attribute::getAllowedTypes())]);
 }
Ejemplo n.º 20
0
 public function calculatePrice(Request $request)
 {
     $product = Product::findOrFail($request->input('product_id'));
     $attributes = $request->input('attribute');
     $add_price = [];
     foreach ($attributes as $key => $attribute) {
         $add_price[] = Attribute::findOrFail($attribute)->add_price;
     }
     return ['amount' => $product->price + array_sum($add_price), 'final_amount' => $product->price - $product->price * $product->discount / 100 + array_sum($add_price), 'discount_amount' => $product->price * $product->discount / 100];
 }