public function insert()
 {
     $rules = array('name' => 'required', 'desc' => 'required', 'badge' => 'required', 'totalposts' => 'required|numeric');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return Redirect::to('admin/addbadge')->withErrors($validator)->withInput();
     } else {
         $name = Input::get('name');
         $image = explode('/', Input::get('badge'));
         $image = urldecode(end($image));
         $extension = explode(".", $image);
         $extension = end($extension);
         $img = Image::make('files/' . $image);
         $img->resize(160, null, function ($constraint) {
             $constraint->aspectRatio();
             $constraint->upsize();
         })->save('files/' . $image);
         $newname = date('YmdHis') . '_' . Str::slug($name, '_') . '.' . $extension;
         File::move('files/' . $image, 'badges/' . $newname);
         $badge = new Badge();
         $badge->name = Input::get('name');
         $badge->description = Input::get('desc');
         $badge->image = $newname;
         $badge->total_posts = Input::get('totalposts');
         $badge->save();
         Session::flash('success', 'New badge added');
         return Redirect::to('admin/badges');
     }
 }
 public function processLevelAndBadgeValidation()
 {
     $return = true;
     $current_level = (int) Configuration::get('GF_CURRENT_LEVEL');
     $current_level_percent = (int) Configuration::get('GF_CURRENT_LEVEL_PERCENT');
     $not_viewed_badge = explode('|', ltrim(Configuration::get('GF_NOT_VIEWED_BADGE', ''), ''));
     $nbr_notif = Configuration::get('GF_NOTIFICATION', 0);
     $ids_badge = Badge::getIdsBadgesToValidate();
     if (count($ids_badge)) {
         $not_viewed_badge = array();
     }
     //reset the last badge only if there is new badge to validate
     foreach ($ids_badge as $id) {
         $badge = new Badge((int) $id);
         if ($badge->scoring + $current_level_percent >= 100) {
             $current_level++;
             $current_level_percent = $badge->scoring + $current_level_percent - 100;
         } else {
             $current_level_percent += $badge->scoring;
         }
         $badge->validated = 1;
         $return &= $badge->save();
         $nbr_notif++;
         $not_viewed_badge[] = $badge->id;
     }
     Configuration::updateGlobalValue('GF_NOTIFICATION', (int) $nbr_notif);
     Configuration::updateGlobalValue('GF_NOT_VIEWED_BADGE', implode('|', array_unique($not_viewed_badge)));
     Configuration::updateGlobalValue('GF_CURRENT_LEVEL', (int) $current_level);
     Configuration::updateGlobalValue('GF_CURRENT_LEVEL_PERCENT', (int) $current_level_percent);
     return $return;
 }
 public function executeAddbadge()
 {
     $c = new Criteria();
     $c->add(BadgePeer::NAME, $this->getRequestParameter('badge'));
     $exbadge = BadgePeer::doSelectOne($c);
     if ($exbadge) {
         $this->setFlash('notice', 'Badge could not be added. A badge with this name already exists.');
     } else {
         $badge = new Badge();
         $badge->setName($this->getRequestParameter('badge'));
         $badge->save();
         $this->setFlash('notice', 'Badge <b>' . $badge->getName() . '</b> added successfully.');
     }
     $this->redirect('/admin/badges');
 }