/**
  * 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 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);
 }
 public function run()
 {
     Model::unguard();
     DB::table('users')->delete();
     DB::table('password_resets')->delete();
     DB::table('attribute_types')->delete();
     DB::table('attributes')->delete();
     DB::table('entity_types')->delete();
     DB::table('entities')->delete();
     DB::table('alarm_types')->delete();
     DB::table('alarms')->delete();
     $admin = User::create(['name' => 'Amministratore', 'email' => '*****@*****.**', 'password' => Hash::make('cippalippa')]);
     $personType = EntityType::create(['name' => 'Personale', 'icon' => 'user']);
     $attrs = ['Indirizzo Mail' => 'mail', 'Telefono' => 'string', 'Data di Nascita' => 'date'];
     foreach ($attrs as $n => $t) {
         AttributeType::create(['name' => $n, 'datatype' => $t, 'entity_type' => $personType->id]);
     }
     $vehicleType = EntityType::create(['name' => 'Veicoli', 'icon' => 'plane']);
     $attrs = ['Targa' => 'string'];
     foreach ($attrs as $n => $t) {
         AttributeType::create(['name' => $n, 'datatype' => $t, 'entity_type' => $vehicleType->id]);
     }
     $placeType = EntityType::create(['name' => 'Luoghi', 'icon' => 'home']);
     $attrs = ['Indirizzo' => 'address'];
     foreach ($attrs as $n => $t) {
         AttributeType::create(['name' => $n, 'datatype' => $t, 'entity_type' => $placeType->id]);
     }
     Model::reguard();
 }
 public function boot()
 {
     view()->composer('*', function ($view) {
         $view->with('users', User::all());
         $view->with('menu_dynamic_entities', EntityType::orderBy('name', 'asc')->get());
         if (Auth::check()) {
             $view->with('current_user_id', Auth::user()->id);
         }
     });
     setlocale(LC_TIME, 'it_IT.UTF-8');
 }
 public function destroy($id)
 {
     $e = EntityType::findOrFail($id);
     $e->delete();
     return redirect(url('/config'));
 }
 public function index()
 {
     $data['users'] = User::all();
     $data['entities'] = EntityType::all();
     return view('config.index', $data);
 }
 public function create(Request $request)
 {
     if ($request->has('action')) {
         $type = $request->input('action');
         switch ($type) {
             case 'details':
                 $data['type'] = AlarmType::findOrFail($request->input('type'));
                 return view('alarms.createdetails', $data);
                 break;
         }
     } else {
         $data['entitytype'] = EntityType::all();
         return view('alarms.create', $data);
     }
 }