1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/movim_ynh.git synced 2024-09-03 19:46:19 +02:00
movim_ynh/sources/app/widgets/Groups/Groups.php

205 lines
5.1 KiB
PHP
Raw Normal View History

<?php
use Moxl\Xec\Action\Pubsub\GetItems;
2015-08-19 11:28:28 +02:00
use Moxl\Xec\Action\Disco\Items;
use Respect\Validation\Validator;
use Moxl\Xec\Action\Pubsub\Create;
use Moxl\Xec\Action\Pubsub\TestCreate;
class Groups extends WidgetBase
{
private $_list_server;
function load()
{
2015-08-19 11:28:28 +02:00
$this->registerEvent('disco_items_handle', 'onDisco');
$this->registerEvent('disco_items_error', 'onDiscoError');
$this->registerEvent('pubsub_create_handle', 'onCreate');
$this->registerEvent('pubsub_testcreate_handle', 'onTestCreate');
$this->registerEvent('pubsub_testcreate_error', 'onTestCreateError');
$this->registerEvent('pubsub_delete_handle', 'onDelete');
$this->registerEvent('pubsub_delete_error', 'onDeleteError');
$this->addjs('groups.js');
}
function onDisco($packet)
{
$server = $packet->content;
$this->displayServer($server);
}
function onCreate($packet)
{
Notification::append(null, $this->__('groups.created'));
list($server, $node) = array_values($packet->content);
$this->ajaxDisco($server);
}
function onDelete($packet)
{
Notification::append(null, $this->__('groups.deleted'));
list($server, $node) = array_values($packet->content);
$this->displayServer($server);
}
function onDeleteError($packet)
{
Notification::append(null, $this->__('groups.deleted'));
$m = new Rooms;
$m->setBookmark();
list($server, $node) = array_values($packet->content);
$this->ajaxSubscriptions();
}
function onDiscoError($packet)
{
Notification::append(null, $this->__('groups.disco_error'));
}
function onTestCreate($packet)
{
$server = $packet->content;
$view = $this->tpl();
$view->assign('server', $server);
Dialog::fill($view->draw('_groups_add', true));
}
function onTestCreateError($packet)
{
Notification::append(null, $this->__('groups.no_creation'));
}
function ajaxHeader()
{
$id = new \modl\ItemDAO();
$view = $this->tpl();
$view->assign('servers', $id->getGroupServers());
$header = $view->draw('_groups_header', true);
Header::fill($header);
}
function ajaxSubscriptions()
{
$html = $this->prepareSubscriptions();
RPC::call('movim_fill', 'groups_widget', $html);
RPC::call('Groups.refresh');
}
function ajaxDisco($server)
{
2016-03-06 20:46:54 +01:00
if(!$this->validateServer($server)) {
Notification::append(null, $this->__('groups.disco_error'));
return;
}
2015-08-19 11:28:28 +02:00
$r = new Items;
$r->setTo($server)->request();
}
/*
* Seriously ? We need to put this hack because of buggy XEP-0060...
*/
function ajaxTestAdd($server)
{
if(!$this->validateServer($server)) return;
$t = new TestCreate;
$t->setTo($server)
->request();
}
function ajaxAddConfirm($server, $form)
{
if(!$this->validateServer($server)) return;
2015-12-15 11:54:49 +01:00
$validate_name = Validator::stringType()->length(4, 80);
if(!$validate_name->validate($form->name->value)) {
Notification::append(null, $this->__('groups.name_error'));
return;
}
$uri = stringToUri($form->name->value);
$c = new Create;
$c->setTo($server)->setNode($uri)->setData($form->name->value)
->request();
}
private function displayServer($server)
{
if(!$this->validateServer($server)) return;
$html = $this->prepareServer($server);
RPC::call('movim_fill', 'groups_widget', $html);
RPC::call('Groups.refresh');
}
function checkNewServer($node) {
$r = false;
if($this->_list_server != $node->server)
$r = true;
$this->_list_server = $node->server;
return $r;
}
function prepareSubscriptions() {
$sd = new \modl\SubscriptionDAO();
$view = $this->tpl();
$view->assign('subscriptions', $sd->getSubscribed());
$html = $view->draw('_groups_subscriptions', true);
return $html;
}
private function prepareServer($server) {
$id = new \modl\ItemDAO();
$view = $this->tpl();
$view->assign('nodes', $id->getItems($server));
$view->assign('server', $server);
$html = $view->draw('_groups_server', true);
return $html;
}
private function cleanServers($servers) {
$i = 0;
foreach($servers as $c) {
if(filter_var($c->server, FILTER_VALIDATE_EMAIL)) {
unset($servers[$i]);
} elseif(count(explode('.', $c->server))<3) {
unset($servers[$i]);
}
$i++;
}
return $servers;
}
/**
* @brief Validate the server
*
* @param string $server
*/
private function validateServer($server)
{
2016-03-06 20:46:54 +01:00
$validate_server = Validator::noWhitespace()->alnum('.')->length(6, 40);
return ($validate_server->validate($server));
}
function display()
{
}
}