summaryrefslogtreecommitdiffstats
path: root/db/foldermapper.php
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-03-20 19:40:17 +0100
committerBernhard Posselt <nukeawhale@gmail.com>2013-03-20 19:40:17 +0100
commitb9d98e336c29d15cbf669d7d735eff7c80b02414 (patch)
tree5373f6fa6c585c495f52fff6299cf125394a30fa /db/foldermapper.php
parentc26baa1eb7803f2291612c866d7610679f51f6d6 (diff)
finished foldermapper
Diffstat (limited to 'db/foldermapper.php')
-rw-r--r--db/foldermapper.php225
1 files changed, 43 insertions, 182 deletions
diff --git a/db/foldermapper.php b/db/foldermapper.php
index d9040b50b..9cfa905cb 100644
--- a/db/foldermapper.php
+++ b/db/foldermapper.php
@@ -1,210 +1,71 @@
<?php
+
/**
-* ownCloud - News app
+* ownCloud - News
*
* @author Alessandro Cosentino
-* Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com>
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
-namespace OCA\News;
-
-/**
- * This class maps a feed to an entry in the feeds table of the database.
- */
-class FolderMapper {
-
- const tableName = '*PREFIX*news_folders';
-
- private $userid;
-
- public function __construct($userid = null) {
- if ($userid !== null) {
- $this->userid = $userid;
- }
- else {
- $this->userid = \OCP\USER::getUser();
- }
- }
-
-
- /**
- * @brief Returns the forest (list of trees) of folders children of $parentid
- * @param
- * @returns
- */
- public function childrenOf($parentid) {
- $folderlist = array();
- $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName .
- ' WHERE user_id = ? AND parent_id = ?');
- $result = $stmt->execute(array($this->userid, $parentid));
-
- while( $row = $result->fetchRow()) {
- $folderid = $row['id'];
- $folder = new Folder($row['name'], $folderid);
- $folder->setOpened($row['opened']);
- $children = self::childrenOf($folderid);
- $folder->addChildren($children);
- $folderlist[] = $folder;
- }
-
- return $folderlist;
- }
-
- /**
- * @brief Returns the forest (list of trees) of folders children of $parentid
- * @param
- * @returns
- */
- public function getAll() {
- return $this->childrenOf(0);
- }
-
- /**
- * @brief Returns the forest (list of trees) of folders children of $parentid,
- * including the feeds that they contain
- * @param
- * @returns
- */
- public function childrenOfWithFeeds($parentid) {
+namespace OCA\News\Db;
- $feedmapper = new FeedMapper();
- $collectionlist = $feedmapper->findByFolderId($parentid);
+use \OCA\AppFramework\Core\API;
- $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName .
- ' WHERE user_id = ? AND parent_id = ?');
- $result = $stmt->execute(array($this->userid, $parentid));
- while( $row = $result->fetchRow()) {
- $folderid = $row['id'];
- $folder = new Folder($row['name'], $folderid);
- $folder->setOpened($row['opened']);
- $children = self::childrenOfWithFeeds($folderid);
- $folder->addChildren($children);
- $collectionlist[] = $folder;
- }
+class FolderMapper extends NewsMapper {
- return $collectionlist;
+ public function __construct(API $api) {
+ parent::__construct($api, 'news_folders');
}
+ public function find($id, $userId){
+ $sql = 'SELECT * FROM `*dbprefix*news_folders` ' .
+ 'WHERE `id` = ? ' .
+ 'AND `user_id` = ?';
- /**
- * This is being used for consistency
- */
- public function findById($id){
- return $this->find($id);
- }
-
-
- /**
- * @brief Retrieve a folder from the database
- * @param id The id of the folder in the database table.
- * @returns an instance of OC_News_Folder
- */
- public function find($id) {
- $stmt = \OCP\DB::prepare('SELECT *
- FROM ' . self::tableName .
- ' WHERE user_id = ? AND id = ?');
- $result = $stmt->execute(array($this->userid, $id));
-
- $row = $result->fetchRow();
- if(!$row)
- return null;
-
- $folder = new Folder($row['name'], $row['id']);
- $folder->setOpened($row['opened']);
+ $row = $this->findRow($sql, $id, $userId);
+ $folder = new Folder();
+ $folder->fromRow($row);
return $folder;
}
- /**
- * @brief Store the folder and all its feeds into the database
- * @param folder the folder to be saved
- * @returns The id of the folder in the database table.
- */
- public function save(Folder $folder) {
- $query = \OCP\DB::prepare('
- INSERT INTO ' . self::tableName .
- '(name, parent_id, user_id, opened)
- VALUES (?, ?, ?, ?)
- ');
-
- $name = $folder->getName();
- if(empty($name)) {
- $l = \OC_L10N::get('news');
- $name = $l->t('no name');
+ private function findAllRows($sql, $params=array()){
+ $result = $this->execute($sql, $params);
+
+ $folders = array();
+ while($row = $result->fetchRow()){
+ $folder = new Folder();
+ $folder->fromRow($row);
+ array_push($folders, $folder);
}
- $parentid = $folder->getParentId();
-
- $params=array(
- $name,
- $parentid,
- $this->userid,
- $folder->getOpened()
- );
- $query->execute($params);
- $folderid = \OCP\DB::insertid(self::tableName);
-
- $folder->setId($folderid);
- return $folderid;
- }
-
-
- /**
- * @brief Updates the folder
- * @param folder the folder to be updated
- */
- public function update(Folder $folder) {
- $query = \OCP\DB::prepare('UPDATE ' . self::tableName
- . ' SET name = ?, opened = ?' . ' WHERE id = ?');
-
- $params = array($folder->getName(), $folder->getOpened(), $folder->getId());
- $query->execute($params);
- return true;
- }
-
- /**
- * @brief Delete the folder and all its feeds from the database
- * @param folder the folder to be deleted (an instance of OCA\News\Folder)
- * @returns true if the folder has been deleted, false if an error occurred
- */
- public function delete(Folder $folder) {
- $folderid = $folder->getId();
- return deleteById(folderid);
+ return $folders;
}
- /**
- * @brief Delete the folder and all its feeds from the database
- * @param folder the folder to be deleted (an instance of OCA\News\Folder)
- * @returns true if the folder has been deleted, false if an error occurred
- */
- public function deleteById($folderid) {
- if ($folderid == null) {
- return false;
- }
-
- // delete child folders
- $stmt = \OCP\DB::prepare('SELECT id FROM ' . self::tableName .' WHERE parent_id = ? AND user_id = ?');
- $result = $stmt->execute(array($folderid, $this->userid));
- while ($row = $result->fetchRow()) {
- if (!self::deleteById($row['id']))
- return false;
- }
- $stmt = \OCP\DB::prepare('DELETE FROM ' . self::tableName .' WHERE id = ? AND user_id = ?');
- $result = $stmt->execute(array($folderid, $this->userid));
+ public function findAllFromUser($userId){
+ $sql = 'SELECT * FROM `*dbprefix*news_folders` ' .
+ 'AND `user_id` = ?';
+ $params = array($userId);
- $feedMapper = new FeedMapper($this->userid);
- //TODO: handle the value that the execute returns
- if(!$feedMapper->deleteAll($folderid))
- return false;
-
- return true;
+ return $this->findAllRows($sql, $params);
}
-
} \ No newline at end of file