/**
  * Validates the data and generates the slug for the data
  * @param mixed $data The data object of the model
  * @return null
  * @throws zibo\library\validation\exception\ValidationException when the data is not valid
  */
 public function validate($data)
 {
     $slugString = $this->getSlugString($data);
     if ($slugString) {
         $slug = $baseSlug = String::safeString($slugString);
         $index = 1;
         do {
             $query = $this->createQuery();
             $query->addCondition('{slug} = %1%', $slug);
             if ($data->id) {
                 $query->addCondition('{id} <> %1%', $data->id);
             }
             if ($query->count()) {
                 $slug = $baseSlug . '-' . $index;
                 $index++;
             } else {
                 break;
             }
         } while (true);
         $data->slug = $slug;
     }
     parent::validate($data);
 }
 /**
  * Deletes a topic
  * @param integer|joppa\forum\model\data\ForumTopicData $data Id of the topic or the topic
  * @return joppa\forum\model\data\ForumTopicData The deleted topic
  */
 protected function deleteData($data)
 {
     $data = parent::deleteData($data);
     $boardModel = $this->getModel(ForumBoardModel::NAME);
     $board = $boardModel->createData(false);
     $board->id = $data->board->id;
     $board->numTopics = $data->board->numTopics - 1;
     $board->lastTopic = $this->getLastTopicIdForBoard($data->board->id);
     $boardModel->save($board);
     return $data;
 }
 /**
  * Deletes a site, makes sute a default site is selected
  * @param Site $site The site to save
  * @return null
  */
 protected function deleteData($site)
 {
     $site = parent::deleteData($site);
     if (!$site->isDefault) {
         return $site;
     }
     $query = $this->createQuery(0);
     $newDefaultSite = $query->queryFirst();
     if ($newDefaultSite) {
         $this->setDefaultSite($newDefaultSite);
     }
     return $site;
 }
 /**
  * Validates the provided subscriber
  * @param joppa\mailinglist\model\data\SubscriberData $data The subscriber to validate
  * @return null
  * @throws zibo\library\validation\exception\ValidationException when a validation exception occurs
  */
 public function validate($data)
 {
     try {
         parent::validate($data);
         $exception = new ValidationException();
     } catch (ValidationException $e) {
         $exception = $e;
     }
     $query = $this->createQuery(false);
     $query->addCondition('{email} = %1%', $data->email);
     if ($data->id) {
         $query->addCondition('{id} <> %1%', $data->id);
     }
     if ($query->count()) {
         $error = new ValidationError(self::TRANSLATION_ERROR_SUBSCRIBED, '%email% is already subscribed', array('email' => $data->email));
         $exception->addErrors('email', array($error));
     }
     if ($exception->hasErrors()) {
         throw $exception;
     }
 }
 /**
  * Deletes a post
  * @param integer|joppa\forum\model\data\ForumPostData $data Id of the post to delete or the post itself
  * @return joppa\forum\model\data\ForumPostData The deleted post
  */
 protected function deleteData($data)
 {
     $data = parent::deleteData($data);
     // substract a post from the author's total posts
     if ($data->author) {
         $profileModel = $this->getModel(ForumProfileModel::NAME);
         $profileModel->removePost($data->author);
     }
     // update the number of posts and the last post of the topic
     $topicModel = $this->getModel(ForumTopicModel::NAME);
     $topic = $topicModel->createData(false);
     $topic->id = $data->topic->id;
     $topic->numPosts = $data->topic->numPosts - 1;
     $topic->lastPost = $this->getLastPostIdForTopic($topic->id);
     if (!$topic->lastPost) {
         $topicModel->delete($topic);
         return $data;
     }
     $topicModel->save($topic);
     // update the number of posts and the last post of the board
     $boardModel = $this->getModel(ForumBoardModel::NAME);
     $query = $boardModel->createQuery();
     $query->setFields('{id}, {numPosts}');
     $query->addCondition('{id} = %1%', $data->topic->board);
     $board = $query->queryFirst();
     $board->numPosts--;
     $board->lastTopic = $topicModel->getLastTopicIdForBoard($board->id);
     $boardModel->save($board);
     // resync the topics post numbers
     $query = $this->createQuery();
     $query->setFields('{id}, {topicPostNumber}');
     $query->addCondition('{topicPostNumber} > %1% and {topic} = %2%', $data->topicPostNumber, $data->topic->id);
     $query->addOrderBy('{topicPostNumber}');
     $topicPostNumber = $data->topicPostNumber;
     $posts = $query->query();
     foreach ($posts as $post) {
         $post->topicPostNumber = $topicPostNumber;
         $this->save($post, 'topicPostNumber');
         $topicPostNumber++;
     }
     return $data;
 }