/**
  * Handle the creation of a new comment
  *
  * @param array $data 
  * @return JsonModel
  */
 protected function createComment($data)
 {
     $userCommentsTable = $this->getUserCommentsTable();
     $usersTable = $this->getUsersTable();
     $user = $usersTable->getById($data['user_id']);
     $data['comment'] = array('user_ip' => $this->getRequest()->getServer('REMOTE_ADDR'), 'user_agent' => $this->getRequest()->getServer('HTTP_USER_AGENT'), 'comment_type' => 'comment', 'comment_author' => sprintf('%s %s', $user->name, $user->surname), 'comment_author_email' => $user->email, 'comment_content' => $data['comment']);
     switch ($data['type']) {
         case \Users\Model\UserStatusesTable::COMMENT_TYPE_ID:
             $validatorTable = \Users\Model\UserStatusesTable::TABLE_NAME;
             $table = $this->getUserStatusesTable();
             break;
         case \Users\Model\UserImagesTable::COMMENT_TYPE_ID:
             $validatorTable = \Users\Model\UserImagesTable::TABLE_NAME;
             $table = $this->getUserImagesTable();
             break;
         case \Users\Model\UserLinksTable::COMMENT_TYPE_ID:
             $validatorTable = \Users\Model\UserLinksTable::TABLE_NAME;
             $table = $this->getUserLinksTable();
             break;
     }
     $entry = $table->getById($data['entry_id']);
     $recipient = $usersTable->getById($entry['user_id']);
     $config = $this->getServiceLocator()->get('Config');
     $filters = $userCommentsTable->getInputFilter($validatorTable, $config['akismet']);
     $filters->setData($data);
     if ($filters->isValid()) {
         $data = $filters->getValues();
         $creationResult = $userCommentsTable->create($data['user_id'], $data['type'], $data['entry_id'], $data['comment']['comment_content']);
         $result = new JsonModel(array('result' => $creationResult));
         if ($creationResult && $recipient['id'] != $user['id']) {
             Mailer::sendContentNotificationEmail($recipient['email'], $recipient['name'], $user['name']);
         }
     } else {
         $result = new JsonModel(array('result' => false, 'errors' => $filters->getMessages()));
     }
     return $result;
 }
 /**
  * This method inspects the request and routes the data
  * to the correct method
  *
  * @return void
  */
 public function create($unfilteredData)
 {
     $usersTable = $this->getUsersTable();
     $filters = $usersTable->getInputFilter();
     $filters->setData($unfilteredData);
     if ($filters->isValid()) {
         $data = $filters->getValues();
         $avatarContent = array_key_exists('avatar', $unfilteredData) ? $unfilteredData['avatar'] : NULL;
         $bcrypt = new Bcrypt();
         $data['password'] = $bcrypt->create($data['password']);
         if ($usersTable->create($data)) {
             $user = $usersTable->getByUsername($data['username']);
             if (!empty($avatarContent)) {
                 $userImagesTable = $this->getUserImagesTable();
                 $filename = sprintf('public/images/%s.png', sha1(uniqid(time(), TRUE)));
                 $content = base64_decode($avatarContent);
                 $image = imagecreatefromstring($content);
                 if (imagepng($image, $filename) === TRUE) {
                     $userImagesTable->create($user['id'], basename($filename));
                 }
                 imagedestroy($image);
                 $image = $userImagesTable->getByFilename(basename($filename));
                 $usersTable->updateAvatar($image['id'], $user['id']);
             }
             Mailer::sendWelcomeEmail($user['email'], $user['name']);
             $result = new JsonModel(array('result' => true));
         } else {
             $result = new JsonModel(array('result' => false));
         }
     } else {
         $result = new JsonModel(array('result' => false, 'errors' => $filters->getMessages()));
     }
     return $result;
 }