public function _preDelete()
 {
     $crit = new B2DBCriteria();
     $crit->addWhere(TBGIssueAffectsComponentTable::COMPONENT, $this->getID());
     B2DB::getTable('TBGIssueAffectsComponentTable')->doDelete($crit);
     $crit = new B2DBCriteria();
     $crit->addWhere(TBGEditionComponentsTable::COMPONENT, $this->getID());
     B2DB::getTable('TBGEditionComponentsTable')->doDelete($crit);
     $crit = new B2DBCriteria();
     $crit->addWhere(TBGComponentAssigneesTable::COMPONENT_ID, $this->getID());
     $crit->addWhere(TBGComponentAssigneesTable::SCOPE, TBGContext::getScope()->getID());
     B2DB::getTable('TBGComponentAssigneesTable')->doDelete($crit);
 }
 public function saveSetting($name, $module, $value, $uid, $scope)
 {
     $crit = $this->getCriteria();
     $crit->addWhere(self::NAME, $name);
     $crit->addWhere(self::MODULE, $module);
     $crit->addWhere(self::UID, $uid);
     $crit->addWhere(self::SCOPE, $scope);
     $res = $this->doSelectOne($crit);
     if ($res instanceof B2DBRow) {
         $theID = $res->get(self::ID);
         $crit2 = new B2DBCriteria();
         $crit2->addWhere(self::NAME, $name);
         $crit2->addWhere(self::MODULE, $module);
         $crit2->addWhere(self::UID, $uid);
         $crit2->addWhere(self::SCOPE, $scope);
         $crit2->addWhere(self::ID, $theID, B2DBCriteria::DB_NOT_EQUALS);
         $res2 = $this->doDelete($crit2);
         $crit = $this->getCriteria();
         $crit->addUpdate(self::NAME, $name);
         $crit->addUpdate(self::MODULE, $module);
         $crit->addUpdate(self::UID, $uid);
         $crit->addUpdate(self::VALUE, $value);
         $this->doUpdateById($crit, $theID);
     } else {
         $crit = $this->getCriteria();
         $crit->addInsert(self::NAME, $name);
         $crit->addInsert(self::MODULE, $module);
         $crit->addInsert(self::VALUE, $value);
         $crit->addInsert(self::SCOPE, $scope);
         $crit->addInsert(self::UID, $uid);
         $this->doInsert($crit);
     }
 }
 public function printSQL()
 {
     $str = '';
     if ($this->crit instanceof B2DBCriteria) {
         $str .= $this->crit->getSQL();
         foreach ($this->crit->getValues() as $val) {
             if (!is_int($val)) {
                 $val = '\'' . $val . '\'';
             }
             $str = substr_replace($str, $val, strpos($str, '?'), 1);
         }
     }
     return $str;
 }
 public function printSQL()
 {
     $str = '';
     if ($this->getCriteria() instanceof B2DBCriteria) {
         $str .= $this->crit->getSQL();
         foreach ($this->crit->getValues() as $val) {
             if (is_object($val)) {
                 throw new B2DBException('waat');
             }
             if (is_int($val)) {
                 $val = $val;
             } elseif (is_null($val)) {
                 $val = 'null';
             } else {
                 $val = '\'' . $val . '\'';
             }
             $str = substr_replace($str, $val, strpos($str, '?'), 1);
         }
     }
     return $str;
 }
 static function getTask()
 {
     try {
         $crit = new B2DBCriteria();
         $crit->addWhere(TBGIssueTypesTable::ICON, 'task');
         $crit->addWhere(TBGIssueTypesTable::SCOPE, TBGContext::getScope()->getID());
         $row = TBGIssueTypesTable::getTable()->doSelectOne($crit);
         if ($row instanceof B2DBRow) {
             return TBGContext::factory()->TBGIssuetype($row->get(TBGIssueTypesTable::ID), $row);
         } else {
             throw new Exception("Couldn't find any 'task' issue types");
         }
     } catch (Exception $e) {
         throw $e;
     }
 }
 public function getViews()
 {
     $crit = new B2DBCriteria();
     $crit->addWhere(TBGArticleViewsTable::ARTICLE_ID, $this->getID());
     return B2DB::getTable('TBGArticleViewsTable')->doCount($crit);
 }
 /**
  * Return the time when the issue was reopened
  * 
  * @return false if closed, otherwise a timestamp
  */
 public function whenReopened()
 {
     if ($this->isClosed()) {
         return false;
     }
     $crit = new B2DBCriteria();
     $crit->addSelectionColumn(TBGLogTable::TIME);
     $crit->addWhere(TBGLogTable::TARGET, $this->_id);
     $crit->addWhere(TBGLogTable::TARGET_TYPE, 1);
     $crit->addWhere(TBGLogTable::CHANGE_TYPE, 22);
     $crit->addOrderBy(TBGLogTable::TIME, 'desc');
     $res = TBGLogTable::getTable()->doSelect($crit);
     $ret_arr = array();
     if ($res->getNumberOfRows() == 0) {
         return false;
     }
     $row = $res->getNextRow();
     return $row->get(TBGLogTable::TIME);
 }
 public static function findTeams($details)
 {
     $crit = new B2DBCriteria();
     $crit->addWhere(TBGTeamsTable::NAME, "%{$details}%", B2DBCriteria::DB_LIKE);
     $teams = array();
     if ($res = B2DB::getTable('TBGTeamsTable')->doSelect($crit)) {
         while ($row = $res->getNextRow()) {
             $teams[$row->get(TBGTeamsTable::ID)] = TBGContext::factory()->TBGTeam($row->get(TBGTeamsTable::ID), $row);
         }
     }
     return $teams;
 }
 private static function _loadSetting($name, $module = 'core', $scope = 0)
 {
     $crit = new B2DBCriteria();
     $crit->addWhere(TBGSettingsTable::NAME, $name);
     $crit->addWhere(TBGSettingsTable::MODULE, $module);
     if ($scope == 0) {
         throw new Exception('BUGS has not been correctly installed. Please check that the default scope exists');
     }
     $crit->addWhere(TBGSettingsTable::SCOPE, $scope);
     $res = B2DB::getTable('TBGSettingsTable')->doSelect($crit);
     if ($res->count() > 0) {
         $retarr = array();
         while ($row = $res->getNextRow()) {
             $retarr[$row->get(TBGSettingsTable::UID)] = $row->get(TBGSettingsTable::VALUE);
         }
         return $retarr;
     } else {
         return null;
     }
 }
 public function getArticles($num_articles = 5, $news = false, $published = true)
 {
     $crit = new B2DBCriteria();
     $crit->addWhere(TBGArticlesTable::SCOPE, TBGContext::getScope()->getID());
     $crit->addWhere(TBGArticlesTable::NAME, 'Category:%', B2DBCriteria::DB_NOT_LIKE);
     $crit->addOrderBy(TBGArticlesTable::DATE, 'desc');
     if ($published) {
         $crit->addWhere(TBGArticlesTable::IS_PUBLISHED, 1);
     }
     $articles = array();
     if ($res = TBGArticlesTable::getTable()->doSelect($crit)) {
         while (($row = $res->getNextRow()) && count($articles) < $num_articles) {
             try {
                 $article = PublishFactory::article($row->get(TBGArticlesTable::ID), $row);
             } catch (Exception $e) {
                 continue;
             }
             if ($article->hasAccess()) {
                 $articles[] = $article;
             }
         }
     }
     return $articles;
 }
 public function getIssuesByProjectID($id)
 {
     $crit = new B2DBCriteria();
     $crit->addWhere(self::PROJECT_ID, $id);
     $crit->addWhere(self::DELETED, 0);
     $results = $this->doSelect($crit);
     if (!is_object($results) || $results->getNumberOfRows() == 0) {
         return false;
     }
     $data = array();
     /* Build revision details */
     while ($results->next()) {
         $data[] = TBGContext::factory()->TBGIssue($results->get(TBGIssuesTable::ID));
     }
     return $data;
 }
 public static function getAllModulePermissions($module, $uid, $tid, $gid)
 {
     $crit = new B2DBCriteria();
     $crit->addWhere(TBGModulePermissionsTable::MODULE_NAME, $module);
     //$sql = "select b2mp.allowed from tbg_2_modulepermissions b2mp where b2mp.module_name = '$module'";
     switch (true) {
         case $uid != 0:
             //$sql .= " and uid = $uid";
             $crit->addWhere(TBGModulePermissionsTable::UID, $uid);
         case $tid != 0:
             //$sql .= " and tid = $tid";
             $crit->addWhere(TBGModulePermissionsTable::TID, $tid);
         case $gid != 0:
             //$sql .= " and gid = $gid";
             $crit->addWhere(TBGModulePermissionsTable::GID, $gid);
     }
     if ($uid + $tid + $gid == 0) {
         //$sql .= " and uid = $uid and tid = $tid and gid = $gid";
         $crit->addWhere(TBGModulePermissionsTable::UID, $uid);
         $crit->addWhere(TBGModulePermissionsTable::TID, $tid);
         $crit->addWhere(TBGModulePermissionsTable::GID, $gid);
     }
     //$sql .= " AND b2mp.scope = " . TBGContext::getScope()->getID();
     $crit->addWhere(TBGModulePermissionsTable::SCOPE, TBGContext::getScope()->getID());
     //$res = b2db_sql_query($sql, B2DB::getDBlink());
     #print $sql;
     $permissions = array();
     $res = B2DB::getTable('TBGModulePermissionsTable')->doSelect($crit);
     while ($row = $res->getNextRow()) {
         $permissions[] = array('allowed' => $row->get(TBGModulePermissionsTable::ALLOWED));
     }
     return $permissions;
 }
 /**
  * Return all permissions available
  * 
  * @param string $type
  * @param integer $uid
  * @param integer $tid
  * @param integer $gid
  * @param integer $target_id[optional]
  * @param boolean $all[optional]
  *
  * @return array
  */
 public static function getAllPermissions($type, $uid, $tid, $gid, $target_id = null, $all = false)
 {
     $crit = new B2DBCriteria();
     $crit->addWhere(TBGPermissionsTable::SCOPE, self::getScope()->getID());
     $crit->addWhere(TBGPermissionsTable::PERMISSION_TYPE, $type);
     if ($uid + $tid + $gid == 0 && !$all) {
         $crit->addWhere(TBGPermissionsTable::UID, $uid);
         $crit->addWhere(TBGPermissionsTable::TID, $tid);
         $crit->addWhere(TBGPermissionsTable::GID, $gid);
     } else {
         switch (true) {
             case $uid != 0:
                 $crit->addWhere(TBGPermissionsTable::UID, $uid);
             case $tid != 0:
                 $crit->addWhere(TBGPermissionsTable::TID, $tid);
             case $gid != 0:
                 $crit->addWhere(TBGPermissionsTable::GID, $gid);
         }
     }
     if ($target_id != null) {
         $crit->addWhere(TBGPermissionsTable::TARGET_ID, $target_id);
     }
     $permissions = array();
     if ($res = B2DB::getTable('TBGPermissionsTable')->doSelect($crit)) {
         while ($row = $res->getNextRow()) {
             $permissions[] = array('p_type' => $row->get(TBGPermissionsTable::PERMISSION_TYPE), 'target_id' => $row->get(TBGPermissionsTable::TARGET_ID), 'allowed' => $row->get(TBGPermissionsTable::ALLOWED), 'uid' => $row->get(TBGPermissionsTable::UID), 'gid' => $row->get(TBGPermissionsTable::GID), 'tid' => $row->get(TBGPermissionsTable::TID), 'id' => $row->get(TBGPermissionsTable::ID));
         }
     }
     return $permissions;
 }
 /**
  * Perform an SQL delete by an id
  *
  * @param integer $id
  * 
  * @return B2DBResultset
  */
 public function doDeleteById($id)
 {
     try {
         $crit = new B2DBCriteria();
         $crit->setFromTable($this);
         $crit->addWhere($this->id_column, $id);
         $crit->generateDeleteSQL();
         $statement = B2DBStatement::getPreparedStatement($crit);
         $resultset = $statement->performQuery('delete');
         return $resultset;
     } catch (Exception $e) {
         if (B2DB::throwExceptionAsHTML()) {
             B2DB::fatalError($e);
             exit;
         } else {
             throw $e;
         }
     }
 }
 /**
  * Removes an issue from the list of flagged issues
  *
  * @param integer $issue_id ID of issue to remove
  */
 public function removeStarredIssue($issue_id)
 {
     $crit = new B2DBCriteria();
     $crit->addWhere(TBGUserIssuesTable::ISSUE, $issue_id);
     $crit->addWhere(TBGUserIssuesTable::UID, $this->_id);
     B2DB::getTable('TBGUserIssuesTable')->doDelete($crit);
     unset($this->_starredissues[$issue_id]);
     return true;
 }
 /**
  * Add a commit entry to the database
  *
  * @param $id Issue ID
  * @param $action A/D/U action applied to file
  * @param $commit_msg Log message
  * @param $file File changed
  * @param $new_rev New revision
  * @param $old_rev Old revision
  * @param $uid UID of changer
  * @param $date POSIX timestamp of change
  */
 public static function addEntry($id, $action, $commit_msg, $file, $new_rev, $old_rev, $uid, $date)
 {
     $crit = new B2DBCriteria();
     $crit->addInsert(TBGVCSIntegrationTable::ISSUE_NO, $id);
     $crit->addInsert(TBGVCSIntegrationTable::ACTION, $action);
     $crit->addInsert(TBGVCSIntegrationTable::LOG, $commit_msg);
     $crit->addInsert(TBGVCSIntegrationTable::FILE_NAME, $file);
     $crit->addInsert(TBGVCSIntegrationTable::NEW_REV, $new_rev);
     $crit->addInsert(TBGVCSIntegrationTable::OLD_REV, $old_rev);
     $crit->addInsert(TBGVCSIntegrationTable::AUTHOR, $uid);
     if ($date == null) {
         $crit->addInsert(TBGVCSIntegrationTable::DATE, time());
     } else {
         $crit->addInsert(TBGVCSIntegrationTable::DATE, $date);
     }
     $crit->addInsert(TBGVCSIntegrationTable::SCOPE, TBGContext::getScope()->getID());
     B2DB::getTable('TBGVCSIntegrationTable')->doInsert($crit);
 }
 public function addNewCommit($project, $commit_msg, $old_rev, $new_rev, $date = null, $changed, $author)
 {
     /* Find issues to update */
     $fixes_grep = "#((bug|issue|ticket|fix|fixes|fixed|fixing|applies to|closes|references|ref|addresses|re|see|according to|also see)\\s\\#?(([A-Z0-9]+\\-)?\\d+))#ie";
     $output = '';
     $f_issues = array();
     try {
         TBGContext::getI18n();
     } catch (Exception $e) {
         TBGContext::reinitializei18n();
     }
     try {
         $project = new TBGProject($project);
     } catch (Exception $e) {
         return TBGContext::getI18n()->__('Error: Invalid project ID');
     }
     if (preg_match_all($fixes_grep, $commit_msg, $f_issues)) {
         // Github
         if (is_array($changed)) {
             $entries = $changed;
             $changed = '';
             // Now handle changed files
             foreach ($entries[0] as $file) {
                 $changed .= 'M' . $file . "\n";
             }
             // Now handle new files
             foreach ($entries[1] as $file) {
                 $changed .= 'A' . $file . "\n";
             }
             // Now handle deleted files
             foreach ($entries[2] as $file) {
                 $changed .= 'D' . $file . "\n";
             }
         }
         $f_issues = array_unique($f_issues[3]);
         $file_lines = preg_split('/[\\n\\r]+/', $changed);
         $files = array();
         foreach ($file_lines as $aline) {
             $action = substr($aline, 0, 1);
             if ($action == "A" || $action == "U" || $action == "D" || $action == "M") {
                 $theline = trim(substr($aline, 1));
                 $files[] = array($action, $theline);
             }
         }
         foreach ($f_issues as $issue_no) {
             TBGContext::setCurrentProject($project);
             $theIssue = TBGIssue::getIssueFromLink($issue_no, true);
             if ($theIssue instanceof TBGIssue) {
                 $uid = 0;
                 /*
                  * Some VCSes use a different format of storing the committer's name. Systems like bzr, git and hg use the format
                  * Joe Bloggs <*****@*****.**>, instead of a classic username. Therefore a user will be found via 4 queries:
                  * a) First we extract the email if there is one, and find a user with that email
                  * b) If one is not found - or if no email was specified, then instead test against the real name (using the name part if there was an email)
                  * c) the username or full name is checked against the friendly name field
                  * d) and if we still havent found one, then we check against the username
                  * e) and if we STILL havent found one, we just say the user is id 0 (unknown user).
                  */
                 if (preg_match("/(?<=<)(.*)(?=>)/", $author, $matches)) {
                     $email = $matches[0];
                     // a)
                     $crit = new B2DBCriteria();
                     $crit->setFromTable(TBGUsersTable::getTable());
                     $crit->addSelectionColumn(TBGUsersTable::ID);
                     $crit->addWhere(TBGUsersTable::EMAIL, $email);
                     $row = TBGUsersTable::getTable()->doSelectOne($crit);
                     if ($row != null) {
                         $uid = $row->get(TBGUsersTable::ID);
                     } else {
                         // Not found by email
                         preg_match("/(?<=^)(.*)(?= <)/", $author, $matches);
                         $author = $matches[0];
                     }
                 }
                 // b)
                 if ($uid == 0) {
                     $crit = new B2DBCriteria();
                     $crit->setFromTable(TBGUsersTable::getTable());
                     $crit->addSelectionColumn(TBGUsersTable::ID);
                     $crit->addWhere(TBGUsersTable::REALNAME, $author);
                     $row = TBGUsersTable::getTable()->doSelectOne($crit);
                     if ($row != null) {
                         $uid = $row->get(TBGUsersTable::ID);
                     }
                 }
                 // c)
                 if ($uid == 0) {
                     $crit = new B2DBCriteria();
                     $crit->setFromTable(TBGUsersTable::getTable());
                     $crit->addSelectionColumn(TBGUsersTable::ID);
                     $crit->addWhere(TBGUsersTable::BUDDYNAME, $author);
                     $row = TBGUsersTable::getTable()->doSelectOne($crit);
                     if ($row != null) {
                         $uid = $row->get(TBGUsersTable::ID);
                     }
                 }
                 // d)
                 if ($uid == 0) {
                     $crit = new B2DBCriteria();
                     $crit->setFromTable(TBGUsersTable::getTable());
                     $crit->addSelectionColumn(TBGUsersTable::ID);
                     $crit->addWhere(TBGUsersTable::UNAME, $author);
                     $row = TBGUsersTable::getTable()->doSelectOne($crit);
                     if ($row != null) {
                         $uid = $row->get(TBGUsersTable::ID);
                     }
                 }
                 $theIssue->addSystemComment(TBGContext::getI18n()->__('Issue updated from code repository'), TBGContext::getI18n()->__('This issue has been updated with the latest changes from the code repository.<source>%commit_msg%</source>', array('%commit_msg%' => $commit_msg)), $uid);
                 foreach ($files as $afile) {
                     if ($date == null) {
                         $date = time();
                     }
                     TBGVCSIntegrationTable::addEntry($theIssue->getID(), $afile[0], $commit_msg, $afile[1], $new_rev, $old_rev, $uid, $date);
                 }
                 $output .= 'Updated ' . $theIssue->getFormattedIssueNo() . "\n";
             } else {
                 $output .= 'Can\'t find ' . $issue_no . ' so not updating that one.' . "\n";
             }
         }
     }
     return $output;
 }