PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/features/cerberusweb.fnr/api/App.php

https://github.com/Hildy/cerb5
PHP | 564 lines | 398 code | 129 blank | 37 comment | 30 complexity | 15d9f35b39763c83528aa50e8183482d MD5 | raw file
  1. <?php
  2. class ChFnrAjaxController extends DevblocksControllerExtension {
  3. private $_CORE_TPL_PATH = '';
  4. private $_TPL_PATH = '';
  5. function __construct($manifest) {
  6. $this->_CORE_TPL_PATH = APP_PATH . '/features/cerberusweb.core/templates/';
  7. $this->_TPL_PATH = dirname(dirname(__FILE__)) . '/templates/';
  8. parent::__construct($manifest);
  9. }
  10. function isVisible() {
  11. // check login
  12. $session = DevblocksPlatform::getSessionService();
  13. $visit = $session->getVisit();
  14. if(empty($visit)) {
  15. return false;
  16. } else {
  17. return true;
  18. }
  19. }
  20. /*
  21. * Request Overload
  22. */
  23. function handleRequest(DevblocksHttpRequest $request) {
  24. if(!$this->isVisible())
  25. return;
  26. $path = $request->path;
  27. $controller = array_shift($path); // timetracking
  28. @$action = DevblocksPlatform::strAlphaNumDash(array_shift($path)) . 'Action';
  29. switch($action) {
  30. case NULL:
  31. // [TODO] Index/page render
  32. break;
  33. default:
  34. // Default action, call arg as a method suffixed with Action
  35. if(method_exists($this,$action)) {
  36. call_user_func(array(&$this, $action));
  37. }
  38. break;
  39. }
  40. }
  41. function doFnrAction() {
  42. $q = DevblocksPlatform::importGPC(@$_POST['q'],'string','');
  43. $sources = DevblocksPlatform::importGPC(@$_POST['sources'],'array',array());
  44. @$sources = array_flip($sources);
  45. $tpl = DevblocksPlatform::getTemplateService();
  46. $tpl->assign('path', $this->_TPL_PATH);
  47. $feeds = array();
  48. $where = null;
  49. if(!empty($sources)) {
  50. $where = sprintf("%s IN (%s)",
  51. DAO_FnrExternalResource::ID,
  52. implode(',', array_keys($sources))
  53. );
  54. }
  55. $resources = DAO_FnrExternalResource::getWhere($where);
  56. $feeds = Model_FnrExternalResource::searchResources($resources, $q);
  57. $tpl->assign('terms', $q);
  58. $tpl->assign('feeds', $feeds);
  59. $tpl->assign('sources', $sources);
  60. $tpl->display('file:' . $this->_TPL_PATH . 'results.tpl');
  61. }
  62. };
  63. if (class_exists('Extension_ResearchTab')):
  64. class ChFnrResearchTab extends Extension_ResearchTab {
  65. const VIEW_RESEARCH_FNR_SEARCH = 'research_fnr_search';
  66. function __construct($manifest) {
  67. parent::__construct($manifest);
  68. }
  69. function showTab() {
  70. $visit = CerberusApplication::getVisit();
  71. $translate = DevblocksPlatform::getTranslationService();
  72. $tpl = DevblocksPlatform::getTemplateService();
  73. $tpl_path = dirname(dirname(__FILE__)) . '/templates/';
  74. $tpl->assign('path', $tpl_path);
  75. @$request_path = DevblocksPlatform::importGPC($_REQUEST['request'],'string','');
  76. $tpl->assign('request_path', $request_path);
  77. @$stack = explode('/', $request_path);
  78. @array_shift($stack); // research
  79. @array_shift($stack); // fnr
  80. @$action = array_shift($stack);
  81. switch($action) {
  82. default:
  83. // if(null == ($view = C4_AbstractViewLoader::getView(self::VIEW_RESEARCH_FNR_SEARCH))) {
  84. // $view = new C4_KbArticleView();
  85. // $view->id = self::VIEW_RESEARCH_FNR_SEARCH;
  86. // $view->name = $translate->_('common.search_results');
  87. // C4_AbstractViewLoader::setView($view->id, $view);
  88. // }
  89. //
  90. // $tpl->assign('view', $view);
  91. // $tpl->assign('view_fields', C4_KbArticleView::getFields());
  92. // $tpl->assign('view_searchable_fields', C4_KbArticleView::getSearchFields());
  93. // $tpl->assign('response_uri', 'research/fnr/search');
  94. $fnr_topics = DAO_FnrTopic::getWhere();
  95. $tpl->assign('fnr_topics', $fnr_topics);
  96. $tpl->display($tpl_path . 'research_tab/index.tpl');
  97. break;
  98. }
  99. }
  100. }
  101. endif;
  102. class ChFnrConfigTab extends Extension_ConfigTab {
  103. const ID = 'fnr.config.tab';
  104. function showTab() {
  105. $tpl = DevblocksPlatform::getTemplateService();
  106. $tpl_path = dirname(dirname(__FILE__)) . '/templates/';
  107. $tpl->assign('path', $tpl_path);
  108. @$topics = DAO_FnrTopic::getWhere();
  109. $tpl->assign('fnr_topics', $topics);
  110. @$resources = DAO_FnrExternalResource::getWhere();
  111. $tpl->assign('fnr_resources', $resources);
  112. $tpl->display('file:' . $tpl_path . 'config/index.tpl');
  113. }
  114. function getFnrResourceAction() {
  115. @$id = DevblocksPlatform::importGPC($_REQUEST['id'], 'integer', 0);
  116. $tpl = DevblocksPlatform::getTemplateService();
  117. $tpl_path = dirname(dirname(__FILE__)) . '/templates/';
  118. $tpl->assign('path', $tpl_path);
  119. if(!empty($id) && null != ($fnr_resource = DAO_FnrExternalResource::get($id)))
  120. $tpl->assign('fnr_resource', $fnr_resource);
  121. @$topics = DAO_FnrTopic::getWhere();
  122. $tpl->assign('fnr_topics', $topics);
  123. $tpl->display('file:' . $tpl_path . 'config/edit_fnr_resource.tpl');
  124. }
  125. function getFnrTopicAction() {
  126. @$id = DevblocksPlatform::importGPC($_REQUEST['id'], 'integer', 0);
  127. $tpl = DevblocksPlatform::getTemplateService();
  128. $tpl_path = dirname(dirname(__FILE__)) . '/templates/';
  129. $tpl->assign('path', $tpl_path);
  130. if(!empty($id) && null != ($fnr_topic = DAO_FnrTopic::get($id)))
  131. $tpl->assign('fnr_topic', $fnr_topic);
  132. $tpl->display('file:' . $tpl_path . 'config/edit_fnr_topic.tpl');
  133. }
  134. function saveTab() {
  135. @$form_type = DevblocksPlatform::importGPC($_REQUEST['form_type'],'string','');
  136. switch($form_type) {
  137. case 'fnr_topic':
  138. $this->_saveTabFnrTopic();
  139. break;
  140. case 'fnr_resource':
  141. $this->_saveTabFnrResource();
  142. break;
  143. }
  144. DevblocksPlatform::redirect(new DevblocksHttpResponse(array('config','fnr')));
  145. exit;
  146. }
  147. private function _saveTabFnrTopic() {
  148. // Form
  149. @$id = DevblocksPlatform::importGPC($_REQUEST['id'],'integer',0);
  150. @$name = DevblocksPlatform::importGPC($_REQUEST['name'],'string','');
  151. @$do_delete = DevblocksPlatform::importGPC($_REQUEST['do_delete'],'integer',0);
  152. if(DEMO_MODE) {
  153. DevblocksPlatform::setHttpResponse(new DevblocksHttpResponse(array('config','fnr')));
  154. return;
  155. }
  156. // Deletes
  157. if(!empty($do_delete)) {
  158. DAO_FnrTopic::delete($id);
  159. // [TODO] Delete all resources on this topic
  160. return;
  161. }
  162. $fields = array(
  163. DAO_FnrTopic::NAME => $name,
  164. );
  165. // Edit
  166. if(!empty($id)) {
  167. DAO_FnrTopic::update($id, $fields);
  168. // Add
  169. } else {
  170. $id = DAO_FnrTopic::create($fields);
  171. }
  172. }
  173. private function _saveTabFnrResource() {
  174. // Form
  175. @$id = DevblocksPlatform::importGPC($_REQUEST['id'],'integer',0);
  176. @$name = DevblocksPlatform::importGPC($_REQUEST['name'],'string','');
  177. @$url = DevblocksPlatform::importGPC($_REQUEST['url'],'string','');
  178. @$topic_id = DevblocksPlatform::importGPC($_REQUEST['topic_id'],'integer',0);
  179. @$do_delete = DevblocksPlatform::importGPC($_REQUEST['do_delete'],'integer',0);
  180. if(DEMO_MODE) {
  181. DevblocksPlatform::setHttpResponse(new DevblocksHttpResponse(array('config','fnr')));
  182. return;
  183. }
  184. // Deletes
  185. if(!empty($do_delete)) {
  186. DAO_FnrExternalResource::delete($id);
  187. return;
  188. }
  189. $fields = array(
  190. DAO_FnrExternalResource::NAME => $name,
  191. DAO_FnrExternalResource::URL => $url,
  192. DAO_FnrExternalResource::TOPIC_ID => $topic_id,
  193. );
  194. // Edit
  195. if(!empty($id)) {
  196. DAO_FnrExternalResource::update($id, $fields);
  197. // Add
  198. } else {
  199. $id = DAO_FnrExternalResource::create($fields);
  200. }
  201. }
  202. };
  203. class DAO_FnrQuery extends DevblocksORMHelper {
  204. const ID = 'id';
  205. const QUERY = 'query';
  206. const CREATED = 'created';
  207. const SOURCE = 'source';
  208. const NO_MATCH = 'no_match';
  209. static function create($fields) {
  210. $db = DevblocksPlatform::getDatabaseService();
  211. $id = $db->GenID('fnr_query_seq');
  212. $sql = sprintf("INSERT INTO fnr_query (id) ".
  213. "VALUES (%d)",
  214. $id
  215. );
  216. $db->Execute($sql);
  217. self::update($id, $fields);
  218. return $id;
  219. }
  220. static function update($ids, $fields) {
  221. parent::_update($ids, 'fnr_query', $fields);
  222. }
  223. /**
  224. * @param string $where
  225. * @return Model_FnrQuery[]
  226. */
  227. static function getWhere($where=null) {
  228. $db = DevblocksPlatform::getDatabaseService();
  229. $sql = "SELECT id, query, created, source, no_match ".
  230. "FROM fnr_query ".
  231. (!empty($where) ? sprintf("WHERE %s ",$where) : "").
  232. "ORDER BY id asc";
  233. $rs = $db->Execute($sql);
  234. return self::_getObjectsFromResult($rs);
  235. }
  236. /**
  237. * @param integer $id
  238. * @return Model_FnrQuery */
  239. static function get($id) {
  240. $objects = self::getWhere(sprintf("%s = %d",
  241. self::ID,
  242. $id
  243. ));
  244. if(isset($objects[$id]))
  245. return $objects[$id];
  246. return null;
  247. }
  248. /**
  249. * @param ADORecordSet $rs
  250. * @return Model_FnrQuery[]
  251. */
  252. static private function _getObjectsFromResult($rs) {
  253. $objects = array();
  254. if(is_a($rs,'ADORecordSet'))
  255. while(!$rs->EOF) {
  256. $object = new Model_FnrQuery();
  257. $object->id = $rs->fields['id'];
  258. $object->query = $rs->fields['query'];
  259. $object->created = $rs->fields['created'];
  260. $object->source = $rs->fields['source'];
  261. $object->no_match = $rs->fields['no_match'];
  262. $objects[$object->id] = $object;
  263. $rs->MoveNext();
  264. }
  265. return $objects;
  266. }
  267. static function delete($ids) {
  268. $db = DevblocksPlatform::getDatabaseService();
  269. $id_list = implode(',', $ids);
  270. $db->Execute(sprintf("DELETE QUICK FROM fnr_query WHERE id IN (%s)",$id_list));
  271. }
  272. };
  273. class DAO_FnrTopic extends DevblocksORMHelper {
  274. const _TABLE = 'fnr_topic';
  275. const ID = 'id';
  276. const NAME = 'name';
  277. public static function create($fields) {
  278. $db = DevblocksPlatform::getDatabaseService();
  279. $id = $db->GenID('generic_seq');
  280. $sql = sprintf("INSERT INTO %s (id,name) ".
  281. "VALUES (%d,'')",
  282. self::_TABLE,
  283. $id
  284. );
  285. $rs = $db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg()); /* @var $rs ADORecordSet */
  286. self::update($id, $fields);
  287. return $id;
  288. }
  289. public static function update($ids, $fields) {
  290. parent::_update($ids, self::_TABLE, $fields);
  291. }
  292. public static function delete($ids) {
  293. if(!is_array($ids)) $ids = array($ids);
  294. $db = DevblocksPlatform::getDatabaseService();
  295. $ids_string = implode(',', $ids);
  296. $sql = sprintf("DELETE QUICK FROM fnr_topic WHERE id IN (%s)", $ids_string);
  297. $db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg());
  298. $sql = sprintf("DELETE QUICK FROM fnr_external_resource WHERE topic_id IN (%s)", $ids_string);
  299. $db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg());
  300. }
  301. public function getWhere($where=null) {
  302. $db = DevblocksPlatform::getDatabaseService();
  303. $sql = sprintf("SELECT id, name ".
  304. "FROM %s ".
  305. (!empty($where) ? ("WHERE $where ") : " ").
  306. " ORDER BY name ",
  307. self::_TABLE
  308. );
  309. $rs = $db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg()); /* @var $rs ADORecordSet */
  310. return self::_createObjectsFromResultSet($rs);
  311. }
  312. public static function get($id) {
  313. $objects = self::getWhere(sprintf("id = %d", $id));
  314. if(isset($objects[$id]))
  315. return $objects[$id];
  316. return null;
  317. }
  318. public static function _createObjectsFromResultSet(ADORecordSet $rs) {
  319. $objects = array();
  320. if(is_a($rs,'ADORecordSet'))
  321. while(!$rs->EOF) {
  322. $object = new Model_FnrTopic();
  323. $object->id = intval($rs->fields['id']);
  324. $object->name = $rs->fields['name'];
  325. $objects[$object->id] = $object;
  326. $rs->MoveNext();
  327. }
  328. return $objects;
  329. }
  330. };
  331. class DAO_FnrExternalResource extends DevblocksORMHelper {
  332. const _TABLE = 'fnr_external_resource';
  333. const ID = 'id';
  334. const NAME = 'name';
  335. const URL = 'url';
  336. const TOPIC_ID = 'topic_id';
  337. public static function create($fields) {
  338. $db = DevblocksPlatform::getDatabaseService();
  339. $id = $db->GenID('generic_seq');
  340. $sql = sprintf("INSERT INTO %s (id,name,url,topic_id) ".
  341. "VALUES (%d,'','',0)",
  342. self::_TABLE,
  343. $id
  344. );
  345. $rs = $db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg()); /* @var $rs ADORecordSet */
  346. self::update($id, $fields);
  347. return $id;
  348. }
  349. public static function update($ids, $fields) {
  350. parent::_update($ids, self::_TABLE, $fields);
  351. }
  352. public static function delete($ids) {
  353. if(!is_array($ids)) $ids = array($ids);
  354. $db = DevblocksPlatform::getDatabaseService();
  355. $sql = sprintf("DELETE QUICK FROM %s WHERE id IN (%s)",
  356. self::_TABLE,
  357. implode(',', $ids)
  358. );
  359. $db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg()); /* @var $rs ADORecordSet */
  360. }
  361. public function getWhere($where=null) {
  362. $db = DevblocksPlatform::getDatabaseService();
  363. $sql = sprintf("SELECT id, name, url, topic_id ".
  364. "FROM %s ".
  365. (!empty($where) ? ("WHERE $where ") : " ").
  366. " ORDER BY name ",
  367. self::_TABLE
  368. );
  369. $rs = $db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg()); /* @var $rs ADORecordSet */
  370. return self::_createObjectsFromResultSet($rs);
  371. }
  372. public static function get($id) {
  373. $objects = self::getWhere(sprintf("id = %d", $id));
  374. if(isset($objects[$id]))
  375. return $objects[$id];
  376. return null;
  377. }
  378. public static function _createObjectsFromResultSet(ADORecordSet $rs) {
  379. $objects = array();
  380. if(is_a($rs,'ADORecordSet'))
  381. while(!$rs->EOF) {
  382. $object = new Model_FnrTopic();
  383. $object->id = intval($rs->fields['id']);
  384. $object->name = $rs->fields['name'];
  385. $object->topic_id = intval($rs->fields['topic_id']);
  386. $object->url = $rs->fields['url'];
  387. $objects[$object->id] = $object;
  388. $rs->MoveNext();
  389. }
  390. return $objects;
  391. }
  392. };
  393. class Model_FnrTopic {
  394. public $id = 0;
  395. public $name = '';
  396. function getResources() {
  397. $where = sprintf("%s = %d",
  398. DAO_FnrExternalResource::TOPIC_ID,
  399. $this->id
  400. );
  401. $resources = DAO_FnrExternalResource::getWhere($where);
  402. return $resources;
  403. }
  404. };
  405. class Model_FnrQuery {
  406. public $id;
  407. public $query;
  408. public $created;
  409. public $source;
  410. public $no_match;
  411. };
  412. class Model_FnrExternalResource {
  413. public $id = 0;
  414. public $name = '';
  415. public $url = '';
  416. public $topic_id = 0;
  417. public static function searchResources($resources, $query) {
  418. $feeds = array();
  419. $topics = DAO_FnrTopic::getWhere();
  420. if(is_array($resources))
  421. foreach($resources as $resource) { /* @var $resource Model_FnrExternalResource */
  422. try {
  423. $url = str_replace("#find#",rawurlencode($query),$resource->url);
  424. $feed = Zend_Feed::import($url);
  425. if($feed->count())
  426. $feeds[] = array(
  427. 'name' => $resource->name,
  428. 'topic_name' => @$topics[$resource->topic_id]->name,
  429. 'feed' => $feed
  430. );
  431. } catch(Exception $e) {}
  432. }
  433. return $feeds;
  434. }
  435. };