Skip to content

Commit

Permalink
[jan] Add API to backup and restore user data.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed May 25, 2017
1 parent 86a8f33 commit 0fdb12f
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 2 deletions.
1 change: 1 addition & 0 deletions nag/docs/CHANGES
Expand Up @@ -2,6 +2,7 @@
v5.0.0-git
----------

[jan] Add API to backup and restore user data.
[jan] Add support for monthly recurrence on last weekday (Request #1922).
[jan] Keep original file names when PUTing tasks via WebDAV.
[mjr] Add an "actual" field for tracking actual time spent.
Expand Down
109 changes: 109 additions & 0 deletions nag/lib/Application.php
Expand Up @@ -11,6 +11,8 @@
* @package Nag
*/

use Horde\Backup;

/* Determine the base directories. */
if (!defined('NAG_BASE')) {
define('NAG_BASE', realpath(__DIR__ . '/..'));
Expand Down Expand Up @@ -356,6 +358,113 @@ public function topbarCreate(Horde_Tree_Renderer_Base $tree, $parent = null,
}
}

/* Backup/restore */

/**
*/
public function backup(array $users = array())
{
global $injector, $nag_shares;

$factory = $injector->getInstance('Nag_Factory_Driver');

if (!$users) {
foreach ($nag_shares->listAllShares() as $share) {
$users[$share->get('owner')] = true;
}
$users = array_keys($users);
}

$getUser = function($user) use ($factory, $nag_shares)
{
global $registry;

$backup = new Backup\User($user);

$shares = $nag_shares->listShares(
$user, array('attributes' => $user)
);
if (!$shares) {
return $backup;
}

// Need to pushApp() here because this method is called delayed,
// but we need Nag's $conf.
$pushed = $registry->pushApp('nag', array('check_perms' => false));
$tasklists = array();
foreach ($shares as $share) {
$tasklists[$share->getId()] = $share->toHash();
$backup->collections[] = new Backup\Collection(
new Nag\Backup\Tasks($factory->create($share->getName())),
'user',
'tasks'
);
}
$backup->collections[] = new Backup\Collection(
new ArrayIterator($tasklists),
'user',
'tasklists'
);
if ($pushed === true) {
$registry->popApp();
}

return $backup;
};

return new Backup\Users(new ArrayIterator($users), $getUser);
}

/**
*/
public function restore(Backup\Collection $data)
{
global $injector, $nag_shares;

switch ($data->getType()) {
case 'tasklists':
foreach ($data as $tasklist) {
$tasklist['owner'] = $data->getUser();
$tasklist['attributes'] = array_intersect_key(
$tasklist['attributes'],
array(
'name' => true,
'desc' => true,
'color' => true,
'issmart' => true,
'search' => true)
);
$nag_shares->fromHash($tasklist);
}
break;
case 'tasks':
$factory = $injector->getInstance('Nag_Factory_Driver');
$map = array();
foreach ($data as $task) {
if (!empty($task['recurrence'])) {
$task['recurrence'] = Horde_Date_Recurrence::fromHash(
$task['recurrence']
);
}
if (!empty($task['parent']) &&
isset($map[$task['parent']])) {
$task['parent'] = $map[$task['parent']];
}
$driver = $factory->create($task['tasklist_id']);
$ids = $driver->add($task);
$map[$task['task_id']] = $ids[0];
}
break;
}
}

/**
*/
public function restoreDependencies()
{
return array('tasks' => array('tasklists'));
}

/* Download data. */

/**
Expand Down
99 changes: 99 additions & 0 deletions nag/lib/Backup/Tasks.php
@@ -0,0 +1,99 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @author Jan Schneider <jan@horde.org>
* @license http://www.horde.org/licenses/gpl GPL
* @package Nag
*/

namespace Nag\Backup;

use Iterator;
use Nag;
use Nag_Driver;

/**
* Backup iterator for tasks.
*
* @author Jan Schneider <jan@horde.org>
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package Nag
*/
class Tasks implements Iterator
{
/**
* The driver instance.
*
* @var Nag_Driver
*/
protected $_driver;

/**
* Tasks iterator.
*
* @var Nag_Task
*/
protected $_task;

/**
* Constructor.
*
* @param Nag_Driver $driver A driver instance.
*/
public function __construct(Nag_Driver $driver)
{
$this->_driver = $driver;
}

// Iterator methods.

/**
*/
public function current()
{
if (!$this->_current) {
return false;
}
$hash = $this->_current->toHash();
if ($hash['recurrence']) {
$hash['recurrence'] = $hash['recurrence']->toHash();
}
return $hash;
}

/**
*/
public function key()
{
return $this->_current ? $this->_current->id : false;
}

/**
*/
public function next()
{
$this->_current = $this->_task->each();
}

/**
*/
public function rewind()
{
$this->_driver->retrieve(Nag::VIEW_ALL, false);
$this->_task = $this->_driver->tasks;
$this->_task->reset();
$this->_current = $this->_task->each();
}

/**
*/
public function valid()
{
return $this->_current !== false;
}
}
10 changes: 8 additions & 2 deletions nag/package.xml
Expand Up @@ -22,7 +22,7 @@
<email>chuck@horde.org</email>
<active>false</active>
</lead>
<date>2016-12-13</date>
<date>2017-05-25</date>
<version>
<release>5.0.0</release>
<api>5.0.0</api>
Expand All @@ -33,6 +33,7 @@
</stability>
<license uri="http://www.horde.org/licenses/gpl">GPL-2.0</license>
<notes>
* [jan] Add API to backup and restore user data.
* [jan] Add support for monthly recurrence on last weekday (Request #1922).
* [jan] Keep original file names when PUTing tasks via WebDAV.
* [mjr] Add an "actual" field for tracking actual time spent.
Expand Down Expand Up @@ -109,6 +110,9 @@
</dir> <!-- /lib/Ajax/Imple -->
<file name="Application.php" role="horde" />
</dir> <!-- /lib/Ajax -->
<dir name="Backup">
<file name="Tasks.php" role="horde" />
</dir> <!-- /lib/Backup -->
<dir name="Block">
<file name="Summary.php" role="horde" />
</dir> <!-- /lib/Block -->
Expand Down Expand Up @@ -837,6 +841,7 @@
<install as="nag/lib/Ajax/Application/Handler/Smartmobile.php" name="lib/Ajax/Application/Handler/Smartmobile.php" />
<install as="nag/lib/Ajax/Imple/ContactAutoCompleter.php" name="lib/Ajax/Imple/ContactAutoCompleter.php" />
<install as="nag/lib/Ajax/Imple/TagAutoCompleter.php" name="lib/Ajax/Imple/TagAutoCompleter.php" />
<install as="nag/lib/Backup/Tasks.php" name="lib/Backup/Tasks.php" />
<install as="nag/lib/Block/Summary.php" name="lib/Block/Summary.php" />
<install as="nag/lib/Driver/Kolab.php" name="lib/Driver/Kolab.php" />
<install as="nag/lib/Driver/Smartlist.php" name="lib/Driver/Smartlist.php" />
Expand Down Expand Up @@ -1811,9 +1816,10 @@
<stability>
<release>beta</release>
<api>beta</api></stability>
<date>2016-12-13</date>
<date>2017-05-25</date>
<license uri="http://www.horde.org/licenses/gpl">GPL-2.0</license>
<notes>
* [jan] Add API to backup and restore user data.
* [jan] Add support for monthly recurrence on last weekday (Request #1922).
* [jan] Keep original file names when PUTing tasks via WebDAV.
* [mjr] Add an "actual" field for tracking actual time spent.
Expand Down

0 comments on commit 0fdb12f

Please sign in to comment.