From: Sébastien CHAZALLET Date: Sun, 2 Dec 2012 14:36:38 +0000 (+0100) Subject: Rajout d'un fil RSS X-Git-Url: http://git.inspyration.org/?p=zf2.biz%2Fgalerie.git;a=commitdiff_plain;h=b9de62fff679629641c3b50ad3f3ff52df9abd60 Rajout d'un fil RSS --- diff --git a/module/Galerie/Module.php b/module/Galerie/Module.php index 4d12cd3..b6a7411 100644 --- a/module/Galerie/Module.php +++ b/module/Galerie/Module.php @@ -22,6 +22,7 @@ use \Zend\Log\Writer\FirePhp\FirePhpBridge; use Galerie\Model\GalerieTable; use Galerie\Model\GalerieInfoTable; +use Galerie\Model\GalerieInfoRssTable; use Galerie\Model\Contact; use Galerie\Form\GalerieForm; @@ -81,6 +82,11 @@ class Module implements $sm->get('Zend\Db\Adapter\Adapter') ); }, + 'Galerie\Model\GalerieInfoRssTable' => function($sm) { + return new GalerieInfoRssTable( + $sm->get('Zend\Db\Adapter\Adapter') + ); + }, 'Galerie\Form\GalerieForm' => function($sm) { $result = new GalerieForm; $result->setTranslator($sm->get('translator'), 'galerie'); diff --git a/module/Galerie/config/module.config.php b/module/Galerie/config/module.config.php index 4704435..d2e661e 100644 --- a/module/Galerie/config/module.config.php +++ b/module/Galerie/config/module.config.php @@ -160,6 +160,16 @@ return array( ), 'verb' => 'get', ), + 'rss' => array( + 'type' => 'Literal', + 'options' => array( + 'route' => '/rss', + 'defaults' => array( + 'action' => 'rss', + ), + ), + 'verb' => 'get', + ), /* 'default' => array( 'type' => 'Segment', @@ -204,4 +214,18 @@ return array( 'host' => 'smtp.free.fr', 'port' => 25, ), + 'rss' => array( + 'title' => 'Galeries', + 'description' => 'Liste des galeries disponibles', + 'link' => 'http://zf2.biz/galeries', + 'setfeedlink' => array( + 'link' => 'http://zf2.biz/galeries/rss', + 'type' => 'rss' + ), + 'author' => array( + 'name' => 'Sébastien CHAZALLET', + 'email' => 'contact@zf2.biz', + 'uri' => 'http://zf2.biz', + ) + ), ); diff --git a/module/Galerie/src/Galerie/Controller/IndexController.php b/module/Galerie/src/Galerie/Controller/IndexController.php index 86d4a10..fef7968 100644 --- a/module/Galerie/src/Galerie/Controller/IndexController.php +++ b/module/Galerie/src/Galerie/Controller/IndexController.php @@ -11,6 +11,8 @@ use Zend\View\Renderer\PhpRenderer; use Zend\Session\Container; +use Zend\Feed\Writer\FeedFactory; + use Galerie\Model\Galerie; use Galerie\Graph\Test as TestPie; @@ -27,6 +29,7 @@ class IndexController extends AbstractActionController private $_translator; private $_log; + private $_rss; private function _getGalerieTable() @@ -47,6 +50,15 @@ class IndexController extends AbstractActionController return $this->_galerieInfoTable; } + private function _getGalerieInfoRssTable() + { + if (!$this->_galerieInfoTable) { + $sm = $this->getServiceLocator(); + $this->_galerieInfoTable = $sm->get('Galerie\Model\GalerieInfoRssTable'); + } + return $this->_galerieInfoTable; + } + private function _getTranslator() { if (!$this->_translator) { @@ -110,6 +122,16 @@ class IndexController extends AbstractActionController return $this->_log; } + private function _getRss() + { + if (!$this->_rss) { + $sm = $this->getServiceLocator(); + $config = $sm->get('Config'); + $this->_rss = FeedFactory::factory($config['rss']); + } + return $this->_rss; + } + @@ -123,7 +145,50 @@ class IndexController extends AbstractActionController $last = null; } return new ViewModel(array('last' => $last)); - } + } + + public function rssAction() { + // Récupération des informations brutes + $modelManager = $this->_getGalerieInfoRssTable(); + $datas = $modelManager->all(); + + // Création du fil RSS + $rss = $this->_getRss(); + + foreach($datas as $d) { + $entry = $rss->createEntry(); + $entry->setTitle($d->name); + + $entry->setLink($this->url()->fromRoute('galerie/view', array('id' => $d->id))); + $entry->addAuthor(array( + 'name' => $d->username, + )); + $date = new \DateTime(); + $entry->setDateModified( + $date->setTimestamp(intval($d->updated)) + ); + $entry->setDateCreated( + $date->setTimestamp(intval($d->created)) + ); + $entry->setDescription($d->description); + $entry->setContent("{$d->nb} photos."); + $rss->addEntry($entry); + } + + //echo '
'; print_r($rss->export('rss')); die('
'); + + // Création de la réponse + $response = $this->getResponse(); + $response->setStatusCode(200); + + // Modification des entêtes + $headers = $this->getResponse()->getHeaders(); + $headers->addHeaderLine('Content-Type', 'application/rss+xml; charset=utf-8'); + + $response->setContent($rss->export('rss')); + + return $response; + } public function csvAction() { // Récupération des informations brutes diff --git a/module/Galerie/src/Galerie/Model/GalerieInfoRss.php b/module/Galerie/src/Galerie/Model/GalerieInfoRss.php new file mode 100644 index 0000000..4a9ef6c --- /dev/null +++ b/module/Galerie/src/Galerie/Model/GalerieInfoRss.php @@ -0,0 +1,46 @@ +id + . ';' . $this->name + . ';' . $this->description + . ';' . $this->username + . ';' . $this->nb + . ';' . $this->created + . ';' . $this->updated; + } + +} + diff --git a/module/Galerie/src/Galerie/Model/GalerieInfoRssTable.php b/module/Galerie/src/Galerie/Model/GalerieInfoRssTable.php new file mode 100644 index 0000000..d804830 --- /dev/null +++ b/module/Galerie/src/Galerie/Model/GalerieInfoRssTable.php @@ -0,0 +1,167 @@ +adapter = $adapter; + + // Utilisation du patron de conception Prototype + // pour la création des objets ResultSet + $this->resultSetPrototype = new ResultSet(); + $this->resultSetPrototype->setArrayObjectPrototype( + new GalerieInfoRss() + ); + + // Initialisation de l'outil de création de requête + $this->sql = new Sql($this->adapter, $this->getTable()); + } + + + public function getTable() + { + return 'gallery'; // Table centrale de la requête + } + + + public function select($where = null, $order = null, $limit = null, $offset = null) + { + $select = $this->sql->select() + ->columns(array('id', 'name', 'description', 'created', 'updated')) + ->join('user', 'gallery.id_user = user.id', array( + 'username' => new \Zend\Db\Sql\Expression("user.firstname || ' ' || user.lastname") + )) + ->join('photo', 'gallery.id = photo.id_gallery', array( + 'nb' => new \Zend\Db\Sql\Expression('count(photo.id)') + ), Select::JOIN_LEFT) + ->group(array( + 'user.lastname', + 'user.firstname', + 'gallery.name' + )) + ->order(array( + 'user.lastname', + 'user.firstname', + 'gallery.name' + )); + if ($where) { + $select->where($where); + } + if ($order) { + $select->order($order); + } + if ($limit) { + $select->limit($limit); + } + if ($offset) { + $select->offset($offset); + } + + // prepare and execute + $statement = $this->sql->prepareStatementForSqlObject($select); + $result = $statement->execute(); + + // build result set + $resultSet = clone $this->resultSetPrototype; + $resultSet->initialize($result); + + return $resultSet; + } + + public function insert($set) { + throw new \Exception('insert is not allowed'); + } + + public function update($set, $where = null) { + throw new \Exception('update is not allowed'); + } + + public function delete($where) { + throw new \Exception('delete is not allowed'); + } + + public function all() + { + return $this->select(); + } + + public function one($id) + { + if ($id === null) { + $row = null; + } else { + $row = $this->select(array('gallery.id' => (int) $id))->current(); + } + if (!$row) { + throw new \Exception("cannot get row {id: {$id}} in table 'galerie'"); + } + return $row; + } + + public function any($id) + { + if ($id === null) { + $row = null; + } else { + $row = $this->select(array('gallery.id' => (int) $id))->current(); + } + return $row; + } + + public function all_by_user($id_user) + { + return $this->select(array('gallery.id_user' => (int) $id_user)); + } + + + + + public function count_all() + { + $select = $this->sql->select()->columns(array( + 'nb' => new \Zend\Db\Sql\Expression('count(gallery.id)') + )); + + // prepare and execute + $statement = $this->sql->prepareStatementForSqlObject($select); + $result = $statement->execute()->current(); + + return $result['nb']; + } + + public function getPartial($start, $length, $tri, $senstri, $filtre) + { + $where = new Where; + $where->like('gallery.name', "%{$filtre}%"); + $where->or; + $where->like('gallery.description', "%{$filtre}%"); + + return $this->select($where, "{$tri} {$senstri}", $length, $start); + } + + + public function csvHeader() + { + return "Id;Nom;Description;Propriétaire;Nombre;Creation;MAJ"; + } + + +}