PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/cerberusweb.fnr/api/App.php

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