PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/api/dao/github_user.php

https://github.com/rmiddle/wgm.github
PHP | 528 lines | 325 code | 94 blank | 109 comment | 25 complexity | 06301cbd52ace4b3d6fa7a073eb04748 MD5 | raw file
  1. <?php
  2. class DAO_GithubUser extends C4_ORMHelper {
  3. const ID = 'id';
  4. const NUMBER = 'number';
  5. const LOGIN = 'login';
  6. const NAME = 'name';
  7. const EMAIL = 'email';
  8. static function create($fields) {
  9. $db = DevblocksPlatform::getDatabaseService();
  10. $sql = "INSERT INTO github_user () VALUES ()";
  11. $db->Execute($sql);
  12. $id = $db->LastInsertId();
  13. self::update($id, $fields);
  14. return $id;
  15. }
  16. static function update($ids, $fields) {
  17. parent::_update($ids, 'github_user', $fields);
  18. }
  19. static function updateWhere($fields, $where) {
  20. parent::_updateWhere('github_user', $fields, $where);
  21. }
  22. /**
  23. * @param string $where
  24. * @param mixed $sortBy
  25. * @param mixed $sortAsc
  26. * @param integer $limit
  27. * @return Model_GithubUser[]
  28. */
  29. static function getWhere($where=null, $sortBy=null, $sortAsc=true, $limit=null) {
  30. $db = DevblocksPlatform::getDatabaseService();
  31. list($where_sql, $sort_sql, $limit_sql) = self::_getWhereSQL($where, $sortBy, $sortAsc, $limit);
  32. // SQL
  33. $sql = "SELECT id, number, login, name, email ".
  34. "FROM github_user ".
  35. $where_sql.
  36. $sort_sql.
  37. $limit_sql
  38. ;
  39. $rs = $db->Execute($sql);
  40. return self::_getObjectsFromResult($rs);
  41. }
  42. /**
  43. * @param integer $id
  44. * @return Model_GithubUser
  45. */
  46. static function get($id) {
  47. $objects = self::getWhere(sprintf("%s = %d",
  48. self::ID,
  49. $id
  50. ));
  51. if(isset($objects[$id]))
  52. return $objects[$id];
  53. return null;
  54. }
  55. /**
  56. * @param string $login
  57. * @return Model_GithubUser[]
  58. */
  59. static function getByLogin($login) {
  60. $db = DevblocksPlatform::getDatabaseService();
  61. $user = self::getWhere(sprintf("%s = %s",
  62. self::LOGIN,
  63. $db->qstr($login)
  64. ));
  65. if(!empty($user))
  66. return array_shift($user);
  67. return null;
  68. }
  69. /**
  70. * @param resource $rs
  71. * @return Model_GithubUser[]
  72. */
  73. static private function _getObjectsFromResult($rs) {
  74. $objects = array();
  75. while($row = mysql_fetch_assoc($rs)) {
  76. $object = new Model_GithubUser();
  77. $object->id = $row['id'];
  78. $object->number = $row['number'];
  79. $object->login = $row['login'];
  80. $object->name = $row['name'];
  81. $object->email = $row['email'];
  82. $objects[$object->id] = $object;
  83. }
  84. mysql_free_result($rs);
  85. return $objects;
  86. }
  87. static function delete($ids) {
  88. if(!is_array($ids)) $ids = array($ids);
  89. $db = DevblocksPlatform::getDatabaseService();
  90. if(empty($ids))
  91. return;
  92. $ids_list = implode(',', $ids);
  93. $db->Execute(sprintf("DELETE FROM github_user WHERE id IN (%s)", $ids_list));
  94. // Fire event
  95. /*
  96. $eventMgr = DevblocksPlatform::getEventService();
  97. $eventMgr->trigger(
  98. new Model_DevblocksEvent(
  99. 'context.delete',
  100. array(
  101. 'context' => 'cerberusweb.contexts.',
  102. 'context_ids' => $ids
  103. )
  104. )
  105. );
  106. */
  107. return true;
  108. }
  109. public static function getSearchQueryComponents($columns, $params, $sortBy=null, $sortAsc=null) {
  110. $fields = SearchFields_GithubUser::getFields();
  111. // Sanitize
  112. if('*'==substr($sortBy,0,1) || !isset($fields[$sortBy]))
  113. $sortBy=null;
  114. list($tables,$wheres) = parent::_parseSearchParams($params, $columns, $fields, $sortBy);
  115. $select_sql = sprintf("SELECT ".
  116. "github_user.id as %s, ".
  117. "github_user.number as %s, ".
  118. "github_user.login as %s, ".
  119. "github_user.name as %s, ".
  120. "github_user.email as %s ",
  121. SearchFields_GithubUser::ID,
  122. SearchFields_GithubUser::NUMBER,
  123. SearchFields_GithubUser::LOGIN,
  124. SearchFields_GithubUser::NAME,
  125. SearchFields_GithubUser::EMAIL
  126. );
  127. $join_sql = "FROM github_user ";
  128. // Custom field joins
  129. //list($select_sql, $join_sql, $has_multiple_values) = self::_appendSelectJoinSqlForCustomFieldTables(
  130. // $tables,
  131. // $params,
  132. // 'github_user.id',
  133. // $select_sql,
  134. // $join_sql
  135. //);
  136. $has_multiple_values = false; // [TODO] Temporary when custom fields disabled
  137. $where_sql = "".
  138. (!empty($wheres) ? sprintf("WHERE %s ",implode(' AND ',$wheres)) : "WHERE 1 ");
  139. $sort_sql = (!empty($sortBy)) ? sprintf("ORDER BY %s %s ",$sortBy,($sortAsc || is_null($sortAsc))?"ASC":"DESC") : " ";
  140. return array(
  141. 'primary_table' => 'github_user',
  142. 'select' => $select_sql,
  143. 'join' => $join_sql,
  144. 'where' => $where_sql,
  145. 'has_multiple_values' => $has_multiple_values,
  146. 'sort' => $sort_sql,
  147. );
  148. }
  149. /**
  150. * Enter description here...
  151. *
  152. * @param array $columns
  153. * @param DevblocksSearchCriteria[] $params
  154. * @param integer $limit
  155. * @param integer $page
  156. * @param string $sortBy
  157. * @param boolean $sortAsc
  158. * @param boolean $withCounts
  159. * @return array
  160. */
  161. static function search($columns, $params, $limit=10, $page=0, $sortBy=null, $sortAsc=null, $withCounts=true) {
  162. $db = DevblocksPlatform::getDatabaseService();
  163. // Build search queries
  164. $query_parts = self::getSearchQueryComponents($columns,$params,$sortBy,$sortAsc);
  165. $select_sql = $query_parts['select'];
  166. $join_sql = $query_parts['join'];
  167. $where_sql = $query_parts['where'];
  168. $has_multiple_values = $query_parts['has_multiple_values'];
  169. $sort_sql = $query_parts['sort'];
  170. $sql =
  171. $select_sql.
  172. $join_sql.
  173. $where_sql.
  174. ($has_multiple_values ? 'GROUP BY github_user.id ' : '').
  175. $sort_sql;
  176. if($limit > 0) {
  177. $rs = $db->SelectLimit($sql,$limit,$page*$limit) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg()); /* @var $rs ADORecordSet */
  178. } else {
  179. $rs = $db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg()); /* @var $rs ADORecordSet */
  180. $total = mysql_num_rows($rs);
  181. }
  182. $results = array();
  183. $total = -1;
  184. while($row = mysql_fetch_assoc($rs)) {
  185. $result = array();
  186. foreach($row as $f => $v) {
  187. $result[$f] = $v;
  188. }
  189. $object_id = intval($row[SearchFields_GithubUser::ID]);
  190. $results[$object_id] = $result;
  191. }
  192. // [JAS]: Count all
  193. if($withCounts) {
  194. $count_sql =
  195. ($has_multiple_values ? "SELECT COUNT(DISTINCT github_user.id) " : "SELECT COUNT(github_user.id) ").
  196. $join_sql.
  197. $where_sql;
  198. $total = $db->GetOne($count_sql);
  199. }
  200. mysql_free_result($rs);
  201. return array($results,$total);
  202. }
  203. };
  204. class SearchFields_GithubUser implements IDevblocksSearchFields {
  205. const ID = 'gu_id';
  206. const NUMBER = 'gu_number';
  207. const LOGIN = 'gu_login';
  208. const NAME = 'gu_name';
  209. const EMAIL = 'gu_email';
  210. /**
  211. * @return DevblocksSearchField[]
  212. */
  213. static function getFields() {
  214. $translate = DevblocksPlatform::getTranslationService();
  215. $columns = array(
  216. self::ID => new DevblocksSearchField(self::ID, 'github_user', 'id', $translate->_('github_user.id')),
  217. self::NUMBER => new DevblocksSearchField(self::NUMBER, 'github_user', 'number', $translate->_('github_user.number')),
  218. self::LOGIN => new DevblocksSearchField(self::LOGIN, 'github_user', 'login', $translate->_('github_user.login')),
  219. self::NAME => new DevblocksSearchField(self::NAME, 'github_user', 'name', $translate->_('github_user.name')),
  220. self::EMAIL => new DevblocksSearchField(self::EMAIL, 'github_user', 'email', $translate->_('github_user.email')),
  221. );
  222. // Custom Fields
  223. //$fields = DAO_CustomField::getByContext(CerberusContexts::XXX);
  224. //if(is_array($fields))
  225. //foreach($fields as $field_id => $field) {
  226. // $key = 'cf_'.$field_id;
  227. // $columns[$key] = new DevblocksSearchField($key,$key,'field_value',$field->name);
  228. //}
  229. // Sort by label (translation-conscious)
  230. uasort($columns, create_function('$a, $b', "return strcasecmp(\$a->db_label,\$b->db_label);\n"));
  231. return $columns;
  232. }
  233. };
  234. class Model_GithubUser {
  235. public $id;
  236. public $number;
  237. public $login;
  238. public $name;
  239. public $email;
  240. };
  241. class View_GithubUser extends C4_AbstractView {
  242. const DEFAULT_ID = 'github_user';
  243. function __construct() {
  244. $translate = DevblocksPlatform::getTranslationService();
  245. $this->id = self::DEFAULT_ID;
  246. // [TODO] Name the worklist view
  247. $this->name = $translate->_('GithubUser');
  248. $this->renderLimit = 25;
  249. $this->renderSortBy = SearchFields_GithubUser::ID;
  250. $this->renderSortAsc = true;
  251. $this->view_columns = array(
  252. SearchFields_GithubUser::ID,
  253. SearchFields_GithubUser::NUMBER,
  254. SearchFields_GithubUser::LOGIN,
  255. SearchFields_GithubUser::NAME,
  256. SearchFields_GithubUser::EMAIL,
  257. );
  258. // [TODO] Filter fields
  259. $this->addColumnsHidden(array(
  260. ));
  261. // [TODO] Filter fields
  262. $this->addParamsHidden(array(
  263. ));
  264. $this->doResetCriteria();
  265. }
  266. function getData() {
  267. $objects = DAO_GithubUser::search(
  268. $this->view_columns,
  269. $this->getParams(),
  270. $this->renderLimit,
  271. $this->renderPage,
  272. $this->renderSortBy,
  273. $this->renderSortAsc,
  274. $this->renderTotal
  275. );
  276. return $objects;
  277. }
  278. function getDataSample($size) {
  279. return $this->_doGetDataSample('DAO_GithubUser', $size);
  280. }
  281. function render() {
  282. $this->_sanitize();
  283. $tpl = DevblocksPlatform::getTemplateService();
  284. $tpl->assign('id', $this->id);
  285. $tpl->assign('view', $this);
  286. // Custom fields
  287. //$custom_fields = DAO_CustomField::getByContext(CerberusContexts::XXX);
  288. //$tpl->assign('custom_fields', $custom_fields);
  289. // [TODO] Set your template path
  290. $tpl->display('devblocks:example.plugin::path/to/view.tpl');
  291. }
  292. function renderCriteria($field) {
  293. $tpl = DevblocksPlatform::getTemplateService();
  294. $tpl->assign('id', $this->id);
  295. // [TODO] Move the fields into the proper data type
  296. switch($field) {
  297. case SearchFields_GithubUser::ID:
  298. case SearchFields_GithubUser::NUMBER:
  299. case SearchFields_GithubUser::LOGIN:
  300. case SearchFields_GithubUser::NAME:
  301. case SearchFields_GithubUser::EMAIL:
  302. case 'placeholder_string':
  303. $tpl->display('devblocks:cerberusweb.core::internal/views/criteria/__string.tpl');
  304. break;
  305. case 'placeholder_number':
  306. $tpl->display('devblocks:cerberusweb.core::internal/views/criteria/__number.tpl');
  307. break;
  308. case 'placeholder_bool':
  309. $tpl->display('devblocks:cerberusweb.core::internal/views/criteria/__bool.tpl');
  310. break;
  311. case 'placeholder_date':
  312. $tpl->display('devblocks:cerberusweb.core::internal/views/criteria/__date.tpl');
  313. break;
  314. /*
  315. default:
  316. // Custom Fields
  317. if('cf_' == substr($field,0,3)) {
  318. $this->_renderCriteriaCustomField($tpl, substr($field,3));
  319. } else {
  320. echo ' ';
  321. }
  322. break;
  323. */
  324. }
  325. }
  326. function renderCriteriaParam($param) {
  327. $field = $param->field;
  328. $values = !is_array($param->value) ? array($param->value) : $param->value;
  329. switch($field) {
  330. default:
  331. parent::renderCriteriaParam($param);
  332. break;
  333. }
  334. }
  335. function getFields() {
  336. return SearchFields_GithubUser::getFields();
  337. }
  338. function doSetCriteria($field, $oper, $value) {
  339. $criteria = null;
  340. // [TODO] Move fields into the right data type
  341. switch($field) {
  342. case SearchFields_GithubUser::ID:
  343. case SearchFields_GithubUser::NUMBER:
  344. case SearchFields_GithubUser::LOGIN:
  345. case SearchFields_GithubUser::NAME:
  346. case SearchFields_GithubUser::EMAIL:
  347. case 'placeholder_string':
  348. // force wildcards if none used on a LIKE
  349. if(($oper == DevblocksSearchCriteria::OPER_LIKE || $oper == DevblocksSearchCriteria::OPER_NOT_LIKE)
  350. && false === (strpos($value,'*'))) {
  351. $value = $value.'*';
  352. }
  353. $criteria = new DevblocksSearchCriteria($field, $oper, $value);
  354. break;
  355. case 'placeholder_number':
  356. $criteria = new DevblocksSearchCriteria($field,$oper,$value);
  357. break;
  358. case 'placeholder_date':
  359. @$from = DevblocksPlatform::importGPC($_REQUEST['from'],'string','');
  360. @$to = DevblocksPlatform::importGPC($_REQUEST['to'],'string','');
  361. if(empty($from)) $from = 0;
  362. if(empty($to)) $to = 'today';
  363. $criteria = new DevblocksSearchCriteria($field,$oper,array($from,$to));
  364. break;
  365. case 'placeholder_bool':
  366. @$bool = DevblocksPlatform::importGPC($_REQUEST['bool'],'integer',1);
  367. $criteria = new DevblocksSearchCriteria($field,$oper,$bool);
  368. break;
  369. /*
  370. default:
  371. // Custom Fields
  372. if(substr($field,0,3)=='cf_') {
  373. $criteria = $this->_doSetCriteriaCustomField($field, substr($field,3));
  374. }
  375. break;
  376. */
  377. }
  378. if(!empty($criteria)) {
  379. $this->addParam($criteria, $field);
  380. $this->renderPage = 0;
  381. }
  382. }
  383. function doBulkUpdate($filter, $do, $ids=array()) {
  384. @set_time_limit(600); // 10m
  385. $change_fields = array();
  386. $custom_fields = array();
  387. // Make sure we have actions
  388. if(empty($do))
  389. return;
  390. // Make sure we have checked items if we want a checked list
  391. if(0 == strcasecmp($filter,"checks") && empty($ids))
  392. return;
  393. if(is_array($do))
  394. foreach($do as $k => $v) {
  395. switch($k) {
  396. // [TODO] Implement actions
  397. case 'example':
  398. //$change_fields[DAO_GithubUser::EXAMPLE] = 'some value';
  399. break;
  400. /*
  401. default:
  402. // Custom fields
  403. if(substr($k,0,3)=="cf_") {
  404. $custom_fields[substr($k,3)] = $v;
  405. }
  406. break;
  407. */
  408. }
  409. }
  410. $pg = 0;
  411. if(empty($ids))
  412. do {
  413. list($objects,$null) = DAO_GithubUser::search(
  414. array(),
  415. $this->getParams(),
  416. 100,
  417. $pg++,
  418. SearchFields_GithubUser::ID,
  419. true,
  420. false
  421. );
  422. $ids = array_merge($ids, array_keys($objects));
  423. } while(!empty($objects));
  424. $batch_total = count($ids);
  425. for($x=0;$x<=$batch_total;$x+=100) {
  426. $batch_ids = array_slice($ids,$x,100);
  427. DAO_GithubUser::update($batch_ids, $change_fields);
  428. // Custom Fields
  429. //self::_doBulkSetCustomFields(ChCustomFieldSource_GithubUser::ID, $custom_fields, $batch_ids);
  430. unset($batch_ids);
  431. }
  432. unset($ids);
  433. }
  434. };