Selaa lähdekoodia

Fix bug #24 : les flux ont désormais une catégorie par défaut

Marien Fressinaud 13 vuotta sitten
vanhempi
commit
59b2ae54ca
2 muutettua tiedostoa jossa 89 lisäystä ja 35 poistoa
  1. 53 35
      app/controllers/feedController.php
  2. 36 0
      app/models/Category.php

+ 53 - 35
app/controllers/feedController.php

@@ -16,43 +16,56 @@ class feedController extends ActionController {
 					$feed = new Feed ($url);
 					$feed->load ();
 
+					$catDAO = new CategoryDAO ();
+					$cat = $feed->category ();
+					if ($cat == '') {
+						$cat = $catDAO->getDefault ()->id ();
+					}
+
 					$feedDAO = new FeedDAO ();
 					$values = array (
 						'id' => $feed->id (),
 						'url' => $feed->url (),
-						'category' => null,
+						'category' => $cat,
 						'name' => $feed->name (),
 						'website' => $feed->website (),
 						'description' => $feed->description (),
 						'lastUpdate' => time ()
 					);
-					$feedDAO->addFeed ($values);
+					if ($feedDAO->addFeed ($values)) {
+						$entryDAO = new EntryDAO ();
+						$entries = $feed->entries ();
+						foreach ($entries as $entry) {
+							$values = array (
+								'id' => $entry->id (),
+								'guid' => $entry->guid (),
+								'title' => $entry->title (),
+								'author' => $entry->author (),
+								'content' => $entry->content (),
+								'link' => $entry->link (),
+								'date' => $entry->date (true),
+								'is_read' => $entry->isRead (),
+								'is_favorite' => $entry->isFavorite (),
+								'id_feed' => $feed->id ()
+							);
+							$entryDAO->addEntry ($values);
+						}
 
-					$entryDAO = new EntryDAO ();
-					$entries = $feed->entries ();
-					foreach ($entries as $entry) {
-						$values = array (
-							'id' => $entry->id (),
-							'guid' => $entry->guid (),
-							'title' => $entry->title (),
-							'author' => $entry->author (),
-							'content' => $entry->content (),
-							'link' => $entry->link (),
-							'date' => $entry->date (true),
-							'is_read' => $entry->isRead (),
-							'is_favorite' => $entry->isFavorite (),
-							'id_feed' => $feed->id ()
+						// notif
+						$notif = array (
+							'type' => 'good',
+							'content' => 'Le flux <em>' . $feed->name () . '</em> a bien été ajouté'
+						);
+						Session::_param ('notification', $notif);
+						$params['id'] = $feed->id ();
+					} else {
+						// notif
+						$notif = array (
+							'type' => 'bad',
+							'content' => '<em>' . $feed->name () . '</em> n\' a pas pu être ajouté'
 						);
-						$entryDAO->addEntry ($values);
+						Session::_param ('notification', $notif);
 					}
-
-					// notif
-					$notif = array (
-						'type' => 'good',
-						'content' => 'Le flux <em>' . $feed->url () . '</em> a bien été ajouté'
-					);
-					Session::_param ('notification', $notif);
-					$params['id'] = $feed->id ();
 				} catch (FileNotExistException $e) {
 					Log::record ($e->getMessage (), Log::ERROR);
 					// notif
@@ -137,19 +150,11 @@ class feedController extends ActionController {
 		} else {
 			$entryDAO = new EntryDAO ();
 			$feedDAO = new FeedDAO ();
-			$catDAO = new CategoryDAO ();
 		
 			$categories = Request::param ('categories', array ());
 			$feeds = Request::param ('feeds', array ());
-		
-			foreach ($categories as $cat) {
-				$values = array (
-					'id' => $cat->id (),
-					'name' => $cat->name (),
-					'color' => $cat->color ()
-				);
-				$catDAO->addCategory ($values);
-			}
+
+			$this->addCategories ($categories);
 			
 			$nb_month_old = $this->view->conf->oldEntries ();
 			$date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
@@ -230,4 +235,17 @@ class feedController extends ActionController {
 			Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
 		}
 	}
+
+	private function addCategories ($categories) {
+		$catDAO = new CategoryDAO ();
+
+		foreach ($categories as $cat) {
+			$values = array (
+				'id' => $cat->id (),
+				'name' => $cat->name (),
+				'color' => $cat->color ()
+			);
+			$catDAO->addCategory ($values);
+		}
+	}
 }

+ 36 - 0
app/models/Category.php

@@ -127,6 +127,22 @@ class CategoryDAO extends Model_pdo {
 			return false;
 		}
 	}
+	public function searchByName ($name) {
+		$sql = 'SELECT * FROM category WHERE name=?';
+		$stm = $this->bd->prepare ($sql);
+
+		$values = array ($name);
+
+		$stm->execute ($values);
+		$res = $stm->fetchAll (PDO::FETCH_ASSOC);
+		$cat = HelperCategory::daoToCategory ($res);
+
+		if (isset ($cat[0])) {
+			return $cat[0];
+		} else {
+			return false;
+		}
+	}
 
 	public function listCategories () {
 		$sql = 'SELECT * FROM category ORDER BY name';
@@ -136,6 +152,26 @@ class CategoryDAO extends Model_pdo {
 		return HelperCategory::daoToCategory ($stm->fetchAll (PDO::FETCH_ASSOC));
 	}
 
+	public function getDefault () {
+		$def_cat = $this->searchByName ('Sans catégorie');
+
+		if (!$def_cat) {
+			$cat = new Category ('Sans catégorie');
+
+			$values = array (
+				'id' => $cat->id (),
+				'name' => $cat->name (),
+				'color' => $cat->color ()
+			);
+
+			$this->addCategory ($values);
+
+			$def_cat = $cat;
+		}
+
+		return $def_cat;
+	}
+
 	public function count () {
 		$sql = 'SELECT COUNT(*) AS count FROM category';
 		$stm = $this->bd->prepare ($sql);