PageRenderTime 29ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/application/models/feeds.php

http://rsslounge.googlecode.com/
PHP | 404 lines | 286 code | 44 blank | 74 comment | 11 complexity | cd5287c0e97165ef12f346e4b90bb80c MD5 | raw file
  1. <?PHP
  2. /**
  3. * Model for accessing and edit the feeds
  4. *
  5. * @package application_models
  6. * @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
  7. * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
  8. */
  9. class application_models_feeds extends application_models_base {
  10. /**
  11. * set up the table name
  12. *
  13. * @return void
  14. */
  15. protected function _setupTableName() {
  16. $this->_name = Zend_Registry::get('config')->resources->db->prefix . 'feeds';
  17. parent::_setupTableName();
  18. }
  19. /**
  20. * set up metadata as other reference table objects
  21. * and dependend table objects
  22. *
  23. * @return void
  24. */
  25. protected function _setupMetadata() {
  26. $this->_referenceMap = array(
  27. 'categories' => array(
  28. 'columns' => 'category',
  29. 'refTableClass' => 'application_models_categories',
  30. 'refColumn' => 'id'
  31. )
  32. );
  33. parent::_setupMetadata();
  34. }
  35. /**
  36. * set new feed order
  37. *
  38. * @return array all affected categories
  39. * @param Zend_Db_Table_Row $cat new category
  40. * @param array $feeds all ids of the feeds
  41. */
  42. public function sort($cat, $feeds) {
  43. if(!is_array($feeds))
  44. $feeds = array($feeds);
  45. // positioncounter
  46. $position = 0;
  47. // save old categories for correcting positions
  48. $oldCategories = array();
  49. // reorder feeds
  50. foreach($feeds as $feed) {
  51. $c = $this->find($feed)->current();
  52. if($cat->id!=$c->category)
  53. $oldCategories[] = $c->category;
  54. $c->category = $cat->id;
  55. $c->position = $position++;
  56. $c->save();
  57. }
  58. // fix positions of old categories
  59. $categoryModel = new application_models_categories();
  60. foreach(array_unique($oldCategories) as $current)
  61. $categoryModel->fixPositions($categoryModel->find($current)->current());
  62. $oldCategories[] = $cat->id;
  63. return $oldCategories;
  64. }
  65. /**
  66. * returns amount of feeds
  67. *
  68. * @return int minimum priority
  69. * @param int $startPriority the minimal priority
  70. * @param int $endPriority the maximal priority
  71. * @param string $view the current view mode
  72. */
  73. public function count($startPriority, $endPriority, $view = 'both') {
  74. $p = Zend_Registry::get('config')->resources->db->prefix;
  75. $select = $this->getAdapter()->select()->from($p.'feeds', 'Count(*)')
  76. ->where('priority>=?', $startPriority)
  77. ->where('priority<=?', $endPriority);
  78. if($view=='multimedia')
  79. $select->where('multimedia=1');
  80. elseif($view=='messages')
  81. $select->where('multimedia=0');
  82. return $this->getAdapter()->fetchOne($select);
  83. }
  84. /**
  85. * returns minimum priority of all feeds
  86. *
  87. * @return int minimum priority
  88. */
  89. public function minPriority() {
  90. $p = Zend_Registry::get('config')->resources->db->prefix;
  91. return $this->getAdapter()->fetchOne('SELECT MIN(priority) FROM '.$p.'feeds');
  92. }
  93. /**
  94. * returns maximum priority of all feeds
  95. *
  96. * @return int maximum priority
  97. */
  98. public function maxPriority() {
  99. $p = Zend_Registry::get('config')->resources->db->prefix;
  100. return $this->getAdapter()->fetchOne('SELECT MAX(priority) FROM '.$p.'feeds');
  101. }
  102. /**
  103. * add new feed
  104. *
  105. * @return array|int id of new feed, or error messages
  106. * @param array $data the post data array
  107. */
  108. public function add($data) {
  109. // validate data
  110. $input = $this->validate($data);
  111. if(is_array($input))
  112. return $input;
  113. $categoryModel = new application_models_categories();
  114. // save new feed
  115. $id = $this->insert(
  116. array_merge(
  117. $input->getEscaped(),
  118. array(
  119. 'icon' => Zend_Controller_Action_HelperBroker::getStaticHelper('pluginloader')->getPlugin($input->getEscaped('source'))->icon,
  120. 'position' => $categoryModel->feeds($input->getEscaped('category')), // get new position
  121. 'multimedia' => Zend_Controller_Action_HelperBroker::getStaticHelper('pluginloader')->getPlugin($input->getEscaped('source'))->multimedia, // get multimedia or not
  122. 'htmlurl' => ''
  123. )
  124. )
  125. );
  126. return $id;
  127. }
  128. /**
  129. * edit feed
  130. *
  131. * @return array|int id of edited feed, or error messages
  132. * @param array $data the post data array
  133. */
  134. public function edit($data) {
  135. // validate data
  136. $input = $this->validate($data, true);
  137. if(is_array($input))
  138. return $input;
  139. $categoryModel = new application_models_categories();
  140. $id = $input->getEscaped('id');
  141. $feed = $this->find($id)->current();
  142. // delete old items on source type change
  143. if($input->getEscaped('source')!=$feed->source) {
  144. $itemsModel = new application_models_items();
  145. $itemsModel->delete('feed='.$id);
  146. }
  147. // save new feed
  148. $this->update(
  149. array_merge(
  150. $input->getEscaped(),
  151. array(
  152. 'icon' => Zend_Controller_Action_HelperBroker::getStaticHelper('pluginloader')->getPlugin($input->getEscaped('source'))->icon,
  153. 'multimedia' => Zend_Controller_Action_HelperBroker::getStaticHelper('pluginloader')->getPlugin($input->getEscaped('source'))->multimedia // get multimedia or not
  154. )
  155. ),
  156. 'id='.$id
  157. );
  158. return $id;
  159. }
  160. /**
  161. * removes a feed
  162. *
  163. * @return array|bool true or error messages
  164. * @param int id of the feed
  165. */
  166. public function remove($id) {
  167. // get feed and category
  168. $feed = $this->find($id);
  169. if($feed->count()==0)
  170. return Zend_Registry::get('language')->translate("feed doesn't exists");
  171. $feed = $feed->current();
  172. $category = $feed->category;
  173. // delete all items
  174. $itemsModel = new application_models_items();
  175. $itemsModel->delete('feed='.$feed->id);
  176. // delete icon
  177. $this->deleteIcon($feed);
  178. // delete messages
  179. $messagesModel = new application_models_messages();
  180. $messagesModel->delete('feed='.$feed->id);
  181. // delete feed
  182. $this->delete('id='.$feed->id);
  183. // reorder feeds in parent category
  184. if($category!=0) {
  185. $categoryModel = new application_models_categories();
  186. $categoryModel->fixPositions($categoryModel->find($category)->current());
  187. }
  188. // success
  189. return true;
  190. }
  191. /**
  192. * saves the new icon
  193. *
  194. * @return void
  195. * @param Zend_Db_Table_Row $feed the current feed
  196. */
  197. public function saveIcon($feed) {
  198. $icon = false;
  199. $iconLoader = Zend_Controller_Action_HelperBroker::getStaticHelper('icon');
  200. // use favicon url (if given)
  201. if(strlen(trim($feed->favicon))!=0) {
  202. if(@file_get_contents($feed->favicon)!==false)
  203. $icon = $iconLoader->loadIconFile($feed->favicon, Zend_Registry::get('config')->favicons->path);
  204. }
  205. // try url of the htmlurl rss feed
  206. if($icon===false && strlen($feed->htmlurl)>0)
  207. $icon = $iconLoader->load($feed->htmlurl,Zend_Registry::get('config')->favicons->path);
  208. // use datasource icon
  209. if($icon===false)
  210. $icon = Zend_Controller_Action_HelperBroker::getStaticHelper('pluginloader')->getPlugin($feed->source)->icon;
  211. // save icon url
  212. $feed->icon = $icon;
  213. $feed->dirtyicon = 0;
  214. $feed->save();
  215. }
  216. /**
  217. * deletes the icon file
  218. *
  219. * @return void
  220. * @param Zend_Db_Table_Row $feed the current feed
  221. */
  222. public function deleteIcon($feed) {
  223. // only delete if no other feed uses this icon
  224. $res = $this->fetchAll(
  225. $this->select()
  226. ->from($this, array('amount' => 'Count(*)'))
  227. ->where('icon=?', $feed->icon)
  228. );
  229. if($res[0]['amount']==1) {
  230. @unlink(Zend_Registry::get('config')->favicons->path . $feed->icon); // fails on plugin feeds
  231. }
  232. }
  233. /**
  234. * validates feed input
  235. *
  236. * @return Zend_Filter_Input|array validator or error message array
  237. * @param array $data for validating
  238. * @param int $validateid (optional) indicates whether id has to be validated
  239. */
  240. protected function validate($data, $validateId = false) {
  241. // define filter
  242. $filterTrim = new Zend_Filter_StringTrim();
  243. $filter = array(
  244. 'name' => $filterTrim,
  245. 'url' => $filterTrim,
  246. 'category' => $filterTrim,
  247. 'priority' => $filterTrim,
  248. 'favicon' => $filterTrim,
  249. 'filter' => $filterTrim,
  250. 'source' => $filterTrim
  251. );
  252. if(!isset($data['source']))
  253. $data['source'] = '';
  254. // define validators
  255. $validatorNotEmpty = new Zend_Validate_NotEmpty();
  256. $validatorNotEmpty->setMessage(Zend_Registry::get('language')->translate("Value is required and can't be empty"), Zend_Validate_NotEmpty::IS_EMPTY);
  257. $validatorCategoryId = new application_validate_categoryid();
  258. $validatorCategoryId->setMessage(Zend_Registry::get('language')->translate("category doesn't exists"), application_validate_categoryid::NOT_EXISTS);
  259. $validatorSource = new application_validate_source();
  260. $validatorSource->setMessage(Zend_Registry::get('language')->translate("source doesn't exists"), application_validate_source::NOT_EXISTS);
  261. $validatorNum = new Zend_Validate_Int(Zend_Registry::get('session')->language);
  262. $validatorNum->setLocale(Zend_Registry::get('session')->language);
  263. $validatorNum->setMessage(Zend_Registry::get('language')->translate('Only digits allowed'), Zend_Validate_Int::NOT_INT);
  264. $validatorNum->setMessage(Zend_Registry::get('language')->translate('Only digits allowed'), Zend_Validate_Int::INVALID);
  265. $validatorDuplicateFeed = new application_validate_duplicatefeed($data['source'],$validateId ? $data['id'] : false);
  266. $validatorDuplicateFeed->setMessage(Zend_Registry::get('language')->translate("feed already exists"), application_validate_duplicatefeed::ALREADY_EXISTS);
  267. $validators = array(
  268. 'name' => array(
  269. $validatorNotEmpty,
  270. Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED
  271. ),
  272. 'url' => array(
  273. $validatorDuplicateFeed,
  274. Zend_Filter_Input::ALLOW_EMPTY => true,
  275. Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL
  276. ),
  277. 'category' => array(
  278. $validatorNum,
  279. $validatorCategoryId,
  280. Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL
  281. ),
  282. 'priority' => array(
  283. $validatorNum,
  284. Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED
  285. ),
  286. 'favicon' => array(
  287. Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL,
  288. Zend_Filter_Input::ALLOW_EMPTY => true
  289. ),
  290. 'filter' => array(
  291. Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_OPTIONAL,
  292. Zend_Filter_Input::ALLOW_EMPTY => true
  293. ),
  294. 'source' => array(
  295. $validatorNotEmpty,
  296. $validatorSource,
  297. Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED,
  298. )
  299. );
  300. // read from source whether url is optional or not
  301. if($validatorSource->isValid($data['source'])) {
  302. $plugin = Zend_Controller_Action_HelperBroker::getStaticHelper('pluginloader')->getPlugin($data['source']);
  303. if(!$plugin->sourceOptional && $plugin->source!==false) {
  304. $validators['url'] = array(
  305. $validatorNotEmpty,
  306. $validatorDuplicateFeed,
  307. Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED
  308. );
  309. }
  310. }
  311. // insert filter and validate rules for id
  312. if($validateId!==false) {
  313. $validatorFeedId = new application_validate_feedid();
  314. $validatorFeedId->setMessage(Zend_Registry::get('language')->translate("feed doesn't exists"), application_validate_feedid::NOT_EXISTS);
  315. $filter['id'] = $filterTrim;
  316. $validators['id'] = array(
  317. $validatorNum,
  318. $validatorFeedId,
  319. Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED
  320. );
  321. }
  322. // create validation main object
  323. $validator = new Zend_Filter_Input(
  324. $filter,
  325. $validators,
  326. $data,
  327. array(
  328. Zend_Filter_Input::NOT_EMPTY_MESSAGE => Zend_Registry::get('language')->translate("Value is required and can't be empty"),
  329. Zend_Filter_Input::BREAK_CHAIN => false
  330. )
  331. );
  332. // return filter input object
  333. return parent::validate($validator);
  334. }
  335. }
  336. ?>