Beispiel #1
0
 /**
  * Processes submitting of the form which is generated in
  * {@link \Mibew\Controller\BanController::showEditFormAction()} method.
  *
  * @param Request $request Incoming request.
  * @return string Rendered page content.
  * @throws NotFoundException If the ban with specified ID is not found in
  *   the system.
  */
 public function submitEditFormAction(Request $request)
 {
     csrf_check_token($request);
     $operator = $this->getOperator();
     $errors = array();
     $page = array('banId' => '', 'saved' => false);
     // Get form fields and validate them
     $ban_id = $request->attributes->getInt('ban_id');
     $address = $request->request->get('address');
     $days = $request->request->get('days');
     $comment = $request->request->get('comment');
     if (!$address) {
         $errors[] = no_field('Visitor\'s Address');
     }
     if (!preg_match("/^\\d+\$/", $days)) {
         $errors[] = wrong_field('Days');
     }
     if (!$comment) {
         $errors[] = no_field('Comment');
     }
     // Check if the ban already exists in the database
     $existing_ban = Ban::loadByAddress($address);
     $ban_duplicate = !$ban_id && $existing_ban || $ban_id && $existing_ban && $ban_id != $existing_ban->id;
     if ($ban_duplicate) {
         $ban_url = $this->generateUrl('ban_edit', array('ban_id' => $existing_ban->id));
         $errors[] = getlocal('The specified address is already in use. Click <a href="{1}">here</a> if you want to edit it.', array($address, $ban_url));
     }
     if (count($errors) != 0) {
         $request->attributes->set('errors', $errors);
         // The form should be rebuild. Invoke appropriate action.
         return $this->showEditFormAction($request);
     }
     // Save ban into the database
     if (!$ban_id) {
         $ban = new Ban();
         $ban->created = time();
     } else {
         $ban = Ban::load($ban_id);
         if (!$ban) {
             throw new NotFoundException('The ban is not found.');
         }
     }
     $ban->till = time() + $days * 24 * 60 * 60;
     $ban->address = $address;
     $ban->comment = $comment;
     $ban->save();
     // Rerender the form page
     $page['saved'] = true;
     $page['address'] = $address;
     $page['title'] = getlocal('Block address');
     $page = array_merge($page, prepare_menu($operator, false));
     return $this->render('ban', $page);
 }
Beispiel #2
0
 /**
  * Save the ban to the database.
  *
  * Triggers {@link \Mibew\EventDispatcher\Events::BAN_CREATE} event.
  */
 public function save()
 {
     $db = Database::getInstance();
     if (!$this->id) {
         // This ban is new.
         $db->query("INSERT INTO {ban} (dtmcreated, dtmtill, address, comment) " . "VALUES (:created, :till, :address, :comment)", array(':created' => (int) $this->created, ':till' => (int) $this->till, ':address' => $this->address, ':comment' => $this->comment));
         $this->id = $db->insertedId();
         $args = array('ban' => $this);
         EventDispatcher::getInstance()->triggerEvent(Events::BAN_CREATE, $args);
     } else {
         // Get the original state of the ban for "update" event.
         $original_ban = Ban::load($this->id);
         // Update existing ban
         $db->query("UPDATE {ban} SET dtmtill = :till, address = :address, " . "comment = :comment WHERE banid = :id", array(':id' => $this->id, ':till' => (int) $this->till, ':address' => $this->address, ':comment' => $this->comment));
         $args = array('ban' => $this, 'original_ban' => $original_ban);
         EventDispatcher::getInstance()->triggerEvent(Events::BAN_UPDATE, $args);
     }
 }