From e7c4f7b13f5bad87529eb460ce1461bce46fa92f Mon Sep 17 00:00:00 2001 From: vinnl Date: Fri, 2 May 2008 22:45:30 +0200 Subject: [PATCH] * [Planet] Planets can now be edited. * [Planet] Feeds can now be edited. * [Planet] Feeds can now be deleted from planets. * Some tweaks to the "choicy" style. --- .../imagestream/controllers/StreamController.php | 3 +- project/modules/page/views/scripts/index/add.phtml | 2 +- project/modules/planet/Tables.yml | 3 + .../modules/planet/controllers/FeedController.php | 102 ++++++++++++++++----- .../planet/controllers/PlanetController.php | 100 +++++++++++--------- .../modules/planet/views/scripts/planet/edit.phtml | 23 +++++ .../modules/planet/views/scripts/planet/main.phtml | 16 +++- project/public/styles/choicy/screen.css | 2 +- 8 files changed, 179 insertions(+), 72 deletions(-) create mode 100644 project/modules/planet/views/scripts/planet/edit.phtml diff --git a/project/modules/imagestream/controllers/StreamController.php b/project/modules/imagestream/controllers/StreamController.php index 1c08086..415f7aa 100644 --- a/project/modules/imagestream/controllers/StreamController.php +++ b/project/modules/imagestream/controllers/StreamController.php @@ -271,7 +271,8 @@ class Imagestream_StreamController extends Page_Abstract $cache = Page_Module::loadCache(); $cache->remove(Page_Module::urlToCacheId($location)); } catch(Exception $e) { - $this->_flashMessenger->setNamespace('error')->addMessage($translate->_( + $this->_flashMessenger->setNamespace('error') + ->addMessage($translate->_( 'An error occurred while deleting the block, please try again.')); } $this->_redirect($location); diff --git a/project/modules/page/views/scripts/index/add.phtml b/project/modules/page/views/scripts/index/add.phtml index 5b9c76e..189fa75 100644 --- a/project/modules/page/views/scripts/index/add.phtml +++ b/project/modules/page/views/scripts/index/add.phtml @@ -1,4 +1,4 @@ -
+

translate('Add block'); ?>

    links as $section => $links): ?> diff --git a/project/modules/planet/Tables.yml b/project/modules/planet/Tables.yml index 951b1a1..a680aad 100644 --- a/project/modules/planet/Tables.yml +++ b/project/modules/planet/Tables.yml @@ -10,6 +10,9 @@ PlanetFeed: link: type: string(255) notblank: true + title: + type: string(255) + notblank: true relations: Block: local: block_id diff --git a/project/modules/planet/controllers/FeedController.php b/project/modules/planet/controllers/FeedController.php index 487ab7e..b1325d5 100644 --- a/project/modules/planet/controllers/FeedController.php +++ b/project/modules/planet/controllers/FeedController.php @@ -66,17 +66,24 @@ class Planet_FeedController extends Page_Abstract } /** - * Add a stream + * Add a feed */ public function addAction() { + $translate = Zend_Registry::get('Zend_Translate'); if(!$this->_isAllowed('add')) { $this->_flashMessenger->addMessage($translate->_( 'You are not allowed to add a feed to a planet.')); - $this->_redirect($location); + $this->_redirect(''); } $planetId = $this->_request->getParam('planet_id'); + if(null === $planetId) + { + $this->_flashMessenger->addMessage($translate->_( + 'No planet to add a feed to specified.')); + $this->_redirect(''); + } $defaultRequest = Zend_Registry::get('defaultRequest'); $this->_helper->actionStack($defaultRequest); @@ -92,10 +99,18 @@ class Planet_FeedController extends Page_Abstract $feed = new PlanetFeed(); $feed->block_id = $planetId; $feed->link = $form->getValue('link'); + try + { + $imported = Zend_Feed::import($feed->link); + $feed->title = $imported->title(); + } catch(Exception $e) { + } $feed->save(); - $cache = Page_Module::loadCache(); + $cache = Planet_Module::loadCache(); $cache->remove($planetId); - $this->_refresh('Feed added.'); + $this->_helper->flashMessenger + ->addMessage($translate->_('Feed added.')); + $this->_redirect($feed->Block->location); } catch(Exception $e) { $logger = Zend_Registry::get('logger'); $logger->err($e->getMessage()); @@ -104,31 +119,30 @@ class Planet_FeedController extends Page_Abstract } /** - * Edit a stream - * - * @todo Adapt this function to the Planet module + * Edit a feed */ public function editAction() { $translate = Zend_Registry::get('Zend_Translate'); - $streamId = $this->_request->getParam('stream_id'); + $feedId = $this->_request->getParam('feed_id'); - if(null === $streamId) + if(null === $feedId) { $this->_flashMessenger->addMessage($translate->_( - 'No image stream specified.')); + 'No feed specified.')); $this->_redirect(); } - $streamTable = Doctrine::getTable('Imagestream'); - $stream = $streamTable->find($streamId); + $feedTable = Doctrine::getTable('PlanetFeed'); + $feed = $feedTable->find($feedId); + $planetId = $feed->block_id; if(!$this->_isAllowed('edit')) { $this->_flashMessenger->addMessage($translate->_( - 'You are not allowed to edit image stream.')); - $this->_redirect($html->Block->location); + 'You are not allowed to edit feeds.')); + $this->_redirect($feed->Block->location); } $this->_helper->viewRenderer->setScriptAction('add'); @@ -137,13 +151,37 @@ class Planet_FeedController extends Page_Abstract $form = $this->_getForm(); $form->submit->setLabel('Edit'); - $form->populate($stream->toArray()); + $form->populate($feed->toArray()); $this->view->form = $form->render(); if(!$this->_request->isPost() || !$form->isValid($this->_request->getPost())) { return; } + + try + { + $feed->link = $form->getValue('link'); + try + { + $imported = Zend_Feed::import($feed->link); + $feed->title = $imported->title(); + } catch(Exception $e) { + } + $feed->save(); + $cache = Planet_Module::loadCache(); + $cache->remove($planetId); + $cache = Planet_Module::loadCache('feed'); + $cache->remove($feedId); + $this->_helper->flashMessenger + ->addMessage($translate->_('Feed edited.')); + $this->_redirect($feed->Block->location); + } catch(Exception $e) { + $logger = Zend_Registry::get('logger'); + $logger->err($e->getMessage()); + $this->_refresh('Failed to edit the feed.', 'error'); + } + try { $stream->service = $form->getValue('service'); @@ -164,25 +202,43 @@ class Planet_FeedController extends Page_Abstract } /** - * Delete a stream + * Delete a feed * * @todo Allow the user to undo this - * @todo Finish this function (i.e. delete all feeds and entries) */ public function deleteAction() { - $planetId = $this->_request->getParam('planet_id'); + $feedId = $this->_request->getParam('feed_id'); $translate = Zend_Registry::get('Zend_Translate'); - if(null === $planetId) + if(null === $feedId) { $this->_flashMessenger->addMessage($translate->_( - 'No Planet specified.')); + 'No feed specified.')); $this->_redirect(); } - - - + $feedsTable = Doctrine::getTable('PlanetFeed'); + $feed = $feedsTable->find($feedId); + $location = $feed->Block->location; + $planetId = $feed->block_id; + $planetCache = Planet_Module::loadCache(); + $feedCache = Planet_Module::loadCache('feed'); + try + { + $feed->delete(); + $planetCache->remove($planetId); + $feedCache->remove($feedId); + } catch(Exception $e) { + $logger = Zend_Registry::get('logger'); + $logger->err($e->getMessage()); + $this->_flashMessenger->setNamespace('error') + ->addMessage($translate->_( + 'An error occurred while deleting the feed, please try again.')); + $this->_redirect($location); + } + $this->_flashMessenger->resetNamespace()->addMessage($translate->_( + 'The feed was successfully deleted.')); + $this->_redirect($location); } /** diff --git a/project/modules/planet/controllers/PlanetController.php b/project/modules/planet/controllers/PlanetController.php index 44576a3..e0879e6 100644 --- a/project/modules/planet/controllers/PlanetController.php +++ b/project/modules/planet/controllers/PlanetController.php @@ -38,6 +38,7 @@ class Planet_PlanetController extends Page_Abstract * feed controller. * * @todo Allow changing the Planet settings such as the title + * @todo Refactor */ public function mainAction() { @@ -64,7 +65,7 @@ class Planet_PlanetController extends Page_Abstract ->getRoute('Planet_ViewEntry'); foreach($rows as $row) { - $cacheId = Pivip_Utility_Cache::toCacheId($row->link); + $cacheId = $row->feed_id; if(!$feed = $feedCache->load($cacheId)) { try @@ -83,7 +84,8 @@ class Planet_PlanetController extends Page_Abstract $planetEntry = array('title' => $entry->title(), 'link' => $entry->link(), 'description' => $entry->description(), - 'content' => $entry->content()); + 'content' => $entry->content(), + 'lastUpdate' => strtotime($entry->date())); $feed[] = $guid; $entryCache->save($planetEntry, $guid, array('planet', 'entry')); $planetFeed['entries'][] = $planetEntry; @@ -100,6 +102,18 @@ class Planet_PlanetController extends Page_Abstract } } } + usort($planetFeed['entries'], array($this, 'compareEntriesByDate')); + $i = 0; + $entries = array(); + foreach($planetFeed['entries'] as $k => $v) + { + if(10 <= $i++) + { + break; + } + $entries[$k] = $v; + } + $planetFeed['entries'] = $entries; $translate = Zend_Registry::get('Zend_Translate'); $planetFeed['title'] = $translate->_('Planet'); $route = Zend_Controller_Front::getInstance()->getRouter() @@ -109,6 +123,34 @@ class Planet_PlanetController extends Page_Abstract $planetFeed['charset'] = 'utf-8'; $planetFeed = Zend_Feed::importArray($planetFeed); $this->view->feed = $planetFeed; + $this->view->urlParams = array('planet_id' => $planetId); + if($this->_isAllowed('edit')) + { + $this->view->allowEdit = true; + } + if($this->_isAllowed('delete')) + { + $this->view->allowDelete = true; + } + } + + /** + * Sort entries by date + */ + public function compareEntriesByDate($a, $b) + { + if(!isset($b['lastUpdate'])) + { + return -1; + } else if(!isset($a['lastUpdate'])) { + return 1; + } + if($a['lastUpdate'] > $b['lastUpdate']) { + return -1; + } else if($a['lastUpdate'] < $b['lastUpdate']) { + return 1; + } + return 0; } /** @@ -155,63 +197,33 @@ class Planet_PlanetController extends Page_Abstract } /** - * Edit a stream - * - * @todo Adapt this function to the Planet module + * Edit a Planet */ public function editAction() { - $translate = Zend_Registry::get('Zend_Translate'); - - $streamId = $this->_request->getParam('stream_id'); + $planetId = $this->_request->getParam('planet_id'); - if(null === $streamId) + if(null === $planetId) { $this->_flashMessenger->addMessage($translate->_( - 'No image stream specified.')); - $this->_redirect(); + 'No planet specified.')); + $this->_redirect(''); } - $streamTable = Doctrine::getTable('Imagestream'); - $stream = $streamTable->find($streamId); - if(!$this->_isAllowed('edit')) { $this->_flashMessenger->addMessage($translate->_( - 'You are not allowed to edit image stream.')); + 'You are not allowed to edit a planet.')); $this->_redirect($html->Block->location); } - $this->_helper->viewRenderer->setScriptAction('add'); - $defaultRequest = Zend_Registry::get('defaultRequest'); - $this->_helper->actionStack($defaultRequest); + $feedsTable = Doctrine::getTable('PlanetFeed'); + $query = Doctrine_Query::create(); + $query->from('PlanetFeed f') + ->where('block_id=?', $planetId); + $this->view->feeds = $query->execute(); - $form = $this->_getForm(); - $form->submit->setLabel('Edit'); - $form->populate($stream->toArray()); - $this->view->form = $form->render(); - if(!$this->_request->isPost() || - !$form->isValid($this->_request->getPost())) - { - return; - } - try - { - $stream->service = $form->getValue('service'); - $stream->keywords = $form->getValue('keywords'); - $stream->amount = intval($form->getValue('amount')); - $stream->type = $form->getValue('type'); - $stream->size = $form->getValue('size'); - $stream->save(); - $this->_flashMessenger->addMessage($translate->_('Block updated.')); - } catch(Exception $e) { - $this->_flashMessenger->setNamespace('error')->addMessage($translate->_( - 'An error occured while updating the block, please try again.')); - $this->_redirect($this->_request->getRequestUri()); - } - $cache = Imagestream_Module::loadCache(); - $cache->remove($streamId); - $this->_redirect($html->Block->location); + $this->view->planetId = $planetId; } /** diff --git a/project/modules/planet/views/scripts/planet/edit.phtml b/project/modules/planet/views/scripts/planet/edit.phtml new file mode 100644 index 0000000..c8e95a0 --- /dev/null +++ b/project/modules/planet/views/scripts/planet/edit.phtml @@ -0,0 +1,23 @@ +
    +feeds as $feed): ?> +
    +
    title; ?> +
    + translate('Edit'); ?> +
    +
    + translate('Delete'); ?> +
    +
    + +action('add', 'feed', 'planet', + array('planet_id' => $this->planetId)); ?> +
    \ No newline at end of file diff --git a/project/modules/planet/views/scripts/planet/main.phtml b/project/modules/planet/views/scripts/planet/main.phtml index 89cae4b..4f76834 100644 --- a/project/modules/planet/views/scripts/planet/main.phtml +++ b/project/modules/planet/views/scripts/planet/main.phtml @@ -1,4 +1,16 @@
    - action('feed', 'feed', 'planet', array('feed' => $this->feed)); ?> - + action('feed', 'feed', 'planet', array('feed' => $this->feed)); + if($this->allowEdit || $this->allowDelete): ?> + + allowEdit): ?> + translate('Edit'); ?> + allowDelete): ?> + translate('Delete'); ?> + + +
    \ No newline at end of file diff --git a/project/public/styles/choicy/screen.css b/project/public/styles/choicy/screen.css index 599e7e1..10a05e0 100644 --- a/project/public/styles/choicy/screen.css +++ b/project/public/styles/choicy/screen.css @@ -105,7 +105,7 @@ h1 a, h2 a, h3 a, h4 a, h5 a, h6 a .notifications .error { - background-color: #DA2B31; + background-color: #FF4207; border-color: #7A0707; } -- 2.11.4.GIT