コード例 #1
0
 /**
  * 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();
         }
     });
 }
コード例 #2
0
 /**
  * Returns a paginated attribute index.
  *
  * @param null|string $q
  * @return Paginator
  */
 public function query($q = null)
 {
     return Attribute::where(function ($query) use($q) {
         if ($q) {
             $query->where('description', 'like', "%{$q}%");
         }
     })->orderBy('description')->paginate();
 }
コード例 #3
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;
 }
コード例 #4
0
ファイル: Entity.php プロジェクト: OfficineDigitali/pendola
 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;
 }
コード例 #5
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);
     }
 }
コード例 #6
0
ファイル: User.php プロジェクト: nyrnzn/beautimetreapi
 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;
 }
コード例 #7
0
ファイル: Product.php プロジェクト: nyrnzn/beautimetreapi
 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);
 }