public function create(GroupModel $group, SiteModel $site, UserAccountModel $creator)
 {
     global $DB;
     try {
         $DB->beginTransaction();
         $stat = $DB->prepare("SELECT max(slug) AS c FROM group_information WHERE site_id=:site_id");
         $stat->execute(array('site_id' => $site->getId()));
         $data = $stat->fetch();
         $group->setSlug($data['c'] + 1);
         $stat = $DB->prepare("INSERT INTO group_information (site_id, slug, title,url,description,created_at,twitter_username,approved_at) " . "VALUES (:site_id, :slug, :title, :url, :description, :created_at, :twitter_username,:approved_at) RETURNING id");
         $stat->execute(array('site_id' => $site->getId(), 'slug' => $group->getSlug(), 'title' => substr($group->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED), 'url' => substr($group->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED), 'twitter_username' => substr($group->getTwitterUsername(), 0, VARCHAR_COLUMN_LENGTH_USED), 'description' => $group->getDescription(), 'created_at' => \TimeSource::getFormattedForDataBase(), 'approved_at' => \TimeSource::getFormattedForDataBase()));
         $data = $stat->fetch();
         $group->setId($data['id']);
         $stat = $DB->prepare("INSERT INTO group_history (group_id, title, url, description, user_account_id  , created_at, approved_at, twitter_username, is_new) VALUES " . "(:group_id, :title, :url, :description, :user_account_id  , :created_at, :approved_at, :twitter_username, '1')");
         $stat->execute(array('group_id' => $group->getId(), 'title' => substr($group->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED), 'url' => substr($group->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED), 'twitter_username' => substr($group->getTwitterUsername(), 0, VARCHAR_COLUMN_LENGTH_USED), 'description' => $group->getDescription(), 'user_account_id' => $creator->getId(), 'created_at' => \TimeSource::getFormattedForDataBase(), 'approved_at' => \TimeSource::getFormattedForDataBase()));
         $data = $stat->fetch();
         $ufgr = new UserWatchesGroupRepository();
         $ufgr->startUserWatchingGroupIfNotWatchedBefore($creator, $group);
         $DB->commit();
     } catch (Exception $e) {
         $DB->rollBack();
     }
 }
 /**
  * @dataProvider dataForTestSet
  */
 function testSet($set, $result)
 {
     $group = new GroupModel();
     $group->setTwitterUsername($set);
     $this->assertEquals($result, $group->getTwitterUsername());
 }
 public function update(GroupModel $group, $fields, GroupEditMetaDataModel $groupEditMetaDataModel)
 {
     $alreadyInTransaction = $this->db->inTransaction();
     // Make Information Data
     $fieldsSQL1 = array();
     $fieldsParams1 = array('id' => $group->getId());
     foreach ($fields as $field) {
         $fieldsSQL1[] = " " . $field . "=:" . $field . " ";
         if ($field == 'title') {
             $fieldsParams1['title'] = substr($group->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED);
         } else {
             if ($field == 'description') {
                 $fieldsParams1['description'] = $group->getDescription();
             } else {
                 if ($field == 'url') {
                     $fieldsParams1['url'] = substr($group->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED);
                 } else {
                     if ($field == 'twitter_username') {
                         $fieldsParams1['twitter_username'] = substr($group->getTwitterUsername(), 0, VARCHAR_COLUMN_LENGTH_USED);
                     } else {
                         if ($field == 'is_duplicate_of_id') {
                             $fieldsParams1['is_duplicate_of_id'] = $group->getIsDuplicateOfId();
                         } else {
                             if ($field == 'is_deleted') {
                                 $fieldsParams1['is_deleted'] = $group->getIsDeleted() ? 1 : 0;
                             }
                         }
                     }
                 }
             }
         }
     }
     // Make History Data
     $fieldsSQL2 = array('group_id', 'user_account_id', 'created_at', 'approved_at');
     $fieldsSQLParams2 = array(':group_id', ':user_account_id', ':created_at', ':approved_at');
     $fieldsParams2 = array('group_id' => $group->getId(), 'user_account_id' => $groupEditMetaDataModel->getUserAccount() ? $groupEditMetaDataModel->getUserAccount()->getId() : null, 'created_at' => $this->timesource->getFormattedForDataBase(), 'approved_at' => $this->timesource->getFormattedForDataBase());
     if ($groupEditMetaDataModel->getEditComment()) {
         $fieldsSQL2[] = ' edit_comment ';
         $fieldsSQLParams2[] = ' :edit_comment ';
         $fieldsParams2['edit_comment'] = $groupEditMetaDataModel->getEditComment();
     }
     foreach ($this->possibleFields as $field) {
         if (in_array($field, $fields) || $field == 'title') {
             $fieldsSQL2[] = " " . $field . " ";
             $fieldsSQLParams2[] = " :" . $field . " ";
             if ($field == 'title') {
                 $fieldsParams2['title'] = substr($group->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED);
             } else {
                 if ($field == 'description') {
                     $fieldsParams2['description'] = $group->getDescription();
                 } else {
                     if ($field == 'url') {
                         $fieldsParams2['url'] = substr($group->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED);
                     } else {
                         if ($field == 'twitter_username') {
                             $fieldsParams2['twitter_username'] = substr($group->getTwitterUsername(), 0, VARCHAR_COLUMN_LENGTH_USED);
                         } else {
                             if ($field == 'is_duplicate_of_id') {
                                 $fieldsParams2['is_duplicate_of_id'] = $group->getIsDuplicateOfId();
                             } else {
                                 if ($field == 'is_deleted') {
                                     $fieldsParams2['is_deleted'] = $group->getIsDeleted() ? 1 : 0;
                                 }
                             }
                         }
                     }
                 }
             }
             $fieldsSQL2[] = " " . $field . "_changed ";
             $fieldsSQLParams2[] = " 0 ";
         } else {
             $fieldsSQL2[] = " " . $field . "_changed ";
             $fieldsSQLParams2[] = " -2 ";
         }
     }
     try {
         if (!$alreadyInTransaction) {
             $this->db->beginTransaction();
         }
         // Information SQL
         $stat = $this->db->prepare("UPDATE group_information  SET " . implode(",", $fieldsSQL1) . " WHERE id=:id");
         $stat->execute($fieldsParams1);
         // History SQL
         $stat = $this->db->prepare("INSERT INTO group_history (" . implode(",", $fieldsSQL2) . ") VALUES (" . implode(",", $fieldsSQLParams2) . ")");
         $stat->execute($fieldsParams2);
         if (!$alreadyInTransaction) {
             $this->db->commit();
         }
     } catch (Exception $e) {
         if (!$alreadyInTransaction) {
             $this->db->rollBack();
         }
         throw $e;
     }
 }