2012-05-28 22:27:18 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2014-04-19 20:16:55 +04:00
|
|
|
* ownCloud - News
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Alessandro Cosentino <cosenal@gmail.com>
|
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
|
|
|
* @copyright Alessandro Cosentino 2012
|
|
|
|
* @copyright Bernhard Posselt 2012, 2014
|
|
|
|
*/
|
2012-05-28 22:27:18 +04:00
|
|
|
|
2013-03-20 22:40:17 +04:00
|
|
|
namespace OCA\News\Db;
|
2012-10-14 23:15:16 +04:00
|
|
|
|
2014-04-19 15:20:54 +04:00
|
|
|
use \OCA\News\Core\Db;
|
2012-10-14 23:15:16 +04:00
|
|
|
|
|
|
|
|
2013-03-22 16:20:24 +04:00
|
|
|
class FolderMapper extends Mapper implements IMapper {
|
2012-10-14 23:15:16 +04:00
|
|
|
|
2014-04-19 15:20:54 +04:00
|
|
|
public function __construct(Db $db) {
|
|
|
|
parent::__construct($db, 'news_folders', '\OCA\News\Db\Folder');
|
2012-06-08 00:19:25 +04:00
|
|
|
}
|
2012-07-08 19:37:35 +04:00
|
|
|
|
2013-03-20 22:40:17 +04:00
|
|
|
public function find($id, $userId){
|
2013-03-23 00:39:39 +04:00
|
|
|
$sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
|
2013-03-20 22:40:17 +04:00
|
|
|
'WHERE `id` = ? ' .
|
|
|
|
'AND `user_id` = ?';
|
2012-10-14 23:15:16 +04:00
|
|
|
|
2014-04-05 21:11:09 +04:00
|
|
|
return $this->findEntity($sql, array($id, $userId));
|
2012-07-06 01:22:24 +04:00
|
|
|
}
|
2012-07-08 19:37:35 +04:00
|
|
|
|
2012-06-06 21:34:19 +04:00
|
|
|
|
2013-03-20 22:40:17 +04:00
|
|
|
public function findAllFromUser($userId){
|
2013-03-23 00:39:39 +04:00
|
|
|
$sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
|
2013-05-10 15:30:00 +04:00
|
|
|
'WHERE `user_id` = ? ' .
|
|
|
|
'AND `deleted_at` = 0';
|
2013-03-20 22:40:17 +04:00
|
|
|
$params = array($userId);
|
2012-07-08 19:37:35 +04:00
|
|
|
|
2014-04-05 21:11:09 +04:00
|
|
|
return $this->findEntities($sql, $params);
|
2012-06-06 21:34:19 +04:00
|
|
|
}
|
2013-03-22 02:29:54 +04:00
|
|
|
|
|
|
|
|
|
|
|
public function findByName($folderName, $userId){
|
2013-03-23 00:39:39 +04:00
|
|
|
$sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
|
2013-05-10 15:30:00 +04:00
|
|
|
'WHERE `name` = ? ' .
|
2013-03-22 02:29:54 +04:00
|
|
|
'AND `user_id` = ?';
|
|
|
|
$params = array($folderName, $userId);
|
|
|
|
|
2014-04-05 21:11:09 +04:00
|
|
|
return $this->findEntities($sql, $params);
|
2013-03-22 02:29:54 +04:00
|
|
|
}
|
2013-03-26 14:44:36 +04:00
|
|
|
|
|
|
|
|
|
|
|
public function delete(Entity $entity){
|
|
|
|
parent::delete($entity);
|
|
|
|
|
|
|
|
// someone please slap me for doing this manually :P
|
|
|
|
// we needz CASCADE + FKs please
|
2013-05-24 19:15:51 +04:00
|
|
|
$sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `folder_id` = ?';
|
2013-03-26 14:44:36 +04:00
|
|
|
$params = array($entity->getId());
|
|
|
|
$this->execute($sql, $params);
|
2013-05-24 19:15:51 +04:00
|
|
|
|
2014-04-07 00:16:39 +04:00
|
|
|
$sql = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN '.
|
|
|
|
'(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)';
|
2013-05-24 19:15:51 +04:00
|
|
|
|
|
|
|
$this->execute($sql);
|
2013-03-26 14:44:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-21 23:10:45 +04:00
|
|
|
/**
|
|
|
|
* @param int $deleteOlderThan if given gets all entries with a delete date
|
|
|
|
* older than that timestamp
|
|
|
|
* @param string $userId if given returns only entries from the given user
|
|
|
|
* @return array with the database rows
|
|
|
|
*/
|
|
|
|
public function getToDelete($deleteOlderThan=null, $userId=null) {
|
2013-05-10 15:30:00 +04:00
|
|
|
$sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
|
2013-05-21 23:10:45 +04:00
|
|
|
'WHERE `deleted_at` > 0 ';
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
// sometimes we want to delete all entries
|
|
|
|
if ($deleteOlderThan !== null) {
|
|
|
|
$sql .= 'AND `deleted_at` < ? ';
|
|
|
|
array_push($params, $deleteOlderThan);
|
|
|
|
}
|
2013-05-10 15:30:00 +04:00
|
|
|
|
|
|
|
// we need to sometimes only delete feeds of a user
|
|
|
|
if($userId !== null) {
|
2013-05-21 23:10:45 +04:00
|
|
|
$sql .= 'AND `user_id` = ?';
|
2013-05-10 15:30:00 +04:00
|
|
|
array_push($params, $userId);
|
|
|
|
}
|
|
|
|
|
2014-04-05 21:11:09 +04:00
|
|
|
return $this->findEntities($sql, $params);
|
2013-05-10 15:30:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-04 05:09:17 +04:00
|
|
|
/**
|
|
|
|
* Deletes all folders of a user
|
|
|
|
* @param string $userId the name of the user
|
|
|
|
*/
|
|
|
|
public function deleteUser($userId) {
|
|
|
|
$sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?';
|
|
|
|
$this->execute($sql, array($userId));
|
|
|
|
}
|
|
|
|
|
2013-05-10 15:30:00 +04:00
|
|
|
|
2012-05-29 07:40:18 +04:00
|
|
|
}
|