2015-08-05 01:01:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Model\Group;
|
|
|
|
|
|
|
|
use PicoDb\Database;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all groups
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function get_all()
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
return Database::getInstance('db')
|
2015-08-05 01:01:21 +02:00
|
|
|
->table('groups')
|
|
|
|
->orderBy('title')
|
|
|
|
->findAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get assoc array of group ids with assigned feeds ids
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function get_map()
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
$result = Database::getInstance('db')
|
2015-08-05 01:01:21 +02:00
|
|
|
->table('feeds_groups')
|
|
|
|
->findAll();
|
|
|
|
|
|
|
|
// TODO: add PDO::FETCH_COLUMN|PDO::FETCH_GROUP to picodb and use it instead
|
|
|
|
// of the following lines
|
|
|
|
$map = array();
|
|
|
|
|
|
|
|
foreach ($result as $row) {
|
|
|
|
$group_id = $row['group_id'];
|
|
|
|
$feed_id = $row['feed_id'];
|
|
|
|
|
|
|
|
if (isset($map[$group_id])) {
|
|
|
|
$map[$group_id][] = $feed_id;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$map[$group_id] = array($feed_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $map;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all groups assigned to feed
|
|
|
|
*
|
|
|
|
* @param integer $feed_id id of the feed
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function get_feed_group_ids($feed_id)
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
return Database::getInstance('db')
|
2015-08-05 01:01:21 +02:00
|
|
|
->table('groups')
|
|
|
|
->join('feeds_groups', 'group_id', 'id')
|
|
|
|
->eq('feed_id', $feed_id)
|
|
|
|
->findAllByColumn('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the id of a group
|
|
|
|
*
|
|
|
|
* @param string $title group name
|
|
|
|
* @return mixed group id or false if not found
|
|
|
|
*/
|
|
|
|
function get_group_id($title)
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
return Database::getInstance('db')
|
2015-08-05 01:01:21 +02:00
|
|
|
->table('groups')
|
|
|
|
->eq('title', $title)
|
|
|
|
->findOneColumn('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all feed ids assigned to a group
|
|
|
|
*
|
2015-08-15 04:08:04 +02:00
|
|
|
* @param integer $group_id
|
2015-08-05 01:01:21 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function get_feeds_by_group($group_id)
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
return Database::getInstance('db')
|
2015-08-05 01:01:21 +02:00
|
|
|
->table('feeds_groups')
|
|
|
|
->eq('group_id', $group_id)
|
|
|
|
->findAllByColumn('feed_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a group to the Database
|
|
|
|
*
|
|
|
|
* Returns either the id of the new group or the id of an existing group with
|
|
|
|
* the same name
|
|
|
|
*
|
|
|
|
* @param string $title group name
|
|
|
|
* @return mixed id of the created group or false on error
|
|
|
|
*/
|
|
|
|
function create($title)
|
|
|
|
{
|
|
|
|
$data = array('title' => $title);
|
|
|
|
|
|
|
|
// check if the group already exists
|
|
|
|
$group_id = get_group_id($title);
|
|
|
|
|
|
|
|
// create group if missing
|
|
|
|
if ($group_id === false) {
|
2015-08-15 03:33:39 +02:00
|
|
|
Database::getInstance('db')
|
2015-08-05 01:01:21 +02:00
|
|
|
->table('groups')
|
|
|
|
->insert($data);
|
|
|
|
|
|
|
|
$group_id = get_group_id($title);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $group_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add groups to feed
|
|
|
|
*
|
|
|
|
* @param integer $feed_id feed id
|
|
|
|
* @param array $group_ids array of group ids
|
|
|
|
* @return boolean true on success, false on error
|
|
|
|
*/
|
|
|
|
function add($feed_id, $group_ids)
|
|
|
|
{
|
|
|
|
foreach ($group_ids as $group_id){
|
|
|
|
$data = array('feed_id' => $feed_id, 'group_id' => $group_id);
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
$result = Database::getInstance('db')
|
2015-08-05 01:01:21 +02:00
|
|
|
->table('feeds_groups')
|
|
|
|
->insert($data);
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove groups from feed
|
|
|
|
*
|
|
|
|
* @param integer $feed_id id of the feed
|
|
|
|
* @param array $group_ids array of group ids
|
|
|
|
* @return boolean true on success, false on error
|
|
|
|
*/
|
|
|
|
function remove($feed_id, $group_ids)
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
return Database::getInstance('db')
|
2015-08-05 01:01:21 +02:00
|
|
|
->table('feeds_groups')
|
|
|
|
->eq('feed_id', $feed_id)
|
|
|
|
->in('group_id', $group_ids)
|
|
|
|
->remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purge orphaned groups from database
|
|
|
|
*/
|
|
|
|
function purge_groups()
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
$groups = Database::getInstance('db')
|
2015-08-05 01:01:21 +02:00
|
|
|
->table('groups')
|
|
|
|
->join('feeds_groups', 'group_id', 'id')
|
|
|
|
->isnull('feed_id')
|
|
|
|
->findAllByColumn('id');
|
|
|
|
|
|
|
|
if (! empty($groups)) {
|
2015-08-15 03:33:39 +02:00
|
|
|
Database::getInstance('db')
|
2015-08-05 01:01:21 +02:00
|
|
|
->table('groups')
|
|
|
|
->in('id', $groups)
|
|
|
|
->remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update feed group associations
|
|
|
|
*
|
|
|
|
* @param integer $feed_id id of the feed to update
|
|
|
|
* @param array $group_ids valid groups ids for feed
|
|
|
|
* @param string $create_group group to create and assign to feed
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function update_feed_groups($feed_id, $group_ids, $create_group = '')
|
|
|
|
{
|
|
|
|
if ($create_group !== '') {
|
|
|
|
$id = create($create_group);
|
|
|
|
|
|
|
|
if ($id === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! in_array($id, $group_ids)) {
|
|
|
|
$group_ids[] = $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$assigned = get_feed_group_ids($feed_id);
|
|
|
|
$superfluous = array_diff($assigned, $group_ids);
|
|
|
|
$missing = array_diff($group_ids, $assigned);
|
|
|
|
|
|
|
|
// remove no longer assigned groups from feed
|
|
|
|
if (! empty($superfluous) && ! remove($feed_id, $superfluous)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add requested groups to feed
|
|
|
|
if (! empty($missing) && ! add($feed_id, $missing)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
purge_groups();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|