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/Publish/Publish.php

298 lines
8.4 KiB
PHP
Raw Normal View History

2016-03-15 16:28:38 +01:00
<?php
/**
* @package Widgets
*
* @file Post.php
* This file is part of Movim.
*
* @brief The Post visualisation widget
*
* @author Jaussoin Timothée <edhelas_at_movim_dot_com>
*
* @version 1.0
* @date 1 december 2014
*
* Copyright (C)2014 MOVIM project
*
* See COPYING for licensing information.
*/
use Moxl\Xec\Action\Pubsub\PostPublish;
use Moxl\Xec\Action\Pubsub\TestPostPublish;
use Moxl\Xec\Action\Microblog\CommentCreateNode;
use \Michelf\Markdown;
use Respect\Validation\Validator;
class Publish extends WidgetBase
{
function load()
{
$this->addjs('publish.js');
$this->addcss('publish.css');
$this->registerEvent('pubsub_postpublish_handle', 'onPublish');
$this->registerEvent('pubsub_testpostpublish_handle', 'onTestPublish');
$this->registerEvent('pubsub_testpostpublish_error', 'onTestPublishError');
}
function onPublish($packet)
{
list($to, $node, $id) = array_values($packet->content);
RPC::call('Publish.enableSend');
// Only for the microblog for the moment
//if($node == 'urn:xmpp:microblog:0') {
$this->ajaxCreateComments($to, $id);
//}
}
function onTestPublish($packet)
{
list($server, $node) = array_values($packet->content);
$this->ajaxCreate($server, $node);
}
function onTestPublishError($packet)
{
Notification::append(null, $this->__('publish.no_publication'));
}
function ajaxCreateBlog()
{
$this->ajaxCreate($this->user->getLogin(), 'urn:xmpp:microblog:0');
}
function ajaxCreate($server, $node, $id = false)
{
if(!$this->validateServerNode($server, $node)) return;
$post = false;
if($id) {
$pd = new \modl\PostnDAO();
$p = $pd->getItem($id);
if($p->isEditable()) {
$post = $p;
}
}
$view = $this->tpl();
$view->assign('to', $server);
$view->assign('node', $node);
$view->assign('item', $post);
RPC::call('MovimTpl.fill', 'main section > div:nth-child(2)', $view->draw('_publish_create', true));
$pd = new \Modl\ItemDAO;
$item = $pd->getItem($server, $node);
$view = $this->tpl();
$view->assign('server', $server);
$view->assign('node', $node);
$view->assign('post', $post);
$view->assign('item', $item);
Header::fill($view->draw('_publish_header', true));
if($id) {
RPC::call('Publish.initEdit');
}
RPC::call('Publish.setEmbed');
}
function ajaxCreateComments($server, $id)
{
if(!$this->validateServerNode($server, $id)) return;
$cn = new CommentCreateNode;
$cn->setTo($server)
->setParentId($id)
->request();
}
function ajaxFormFilled($server, $node)
{
$view = $this->tpl();
$view->assign('server', $server);
$view->assign('node', $node);
Dialog::fill($view->draw('_publish_back_confirm', true));
}
function ajaxPreview($form)
{
if($form->content->value != '') {
$view = $this->tpl();
$view->assign('content', addHFR(Markdown::defaultTransform($form->content->value)));
Dialog::fill($view->draw('_publish_preview', true), true);
} else {
Notification::append(false, $this->__('publish.no_content_preview'));
}
}
function ajaxHelp()
{
$view = $this->tpl();
Dialog::fill($view->draw('_publish_help', true), true);
}
/*
* Sic, doing this hack and wait to have a proper way to test it in the standard
*/
function ajaxTestPublish($server, $node)
{
if(!$this->validateServerNode($server, $node)) return;
$t = new TestPostPublish;
$t->setTo($server)
->setNode($node)
->request();
}
function ajaxPublish($form)
{
RPC::call('Publish.disableSend');
if($form->title->value != '') {
$p = new PostPublish;
$p->setFrom($this->user->getLogin())
->setTo($form->to->value)
->setTitle(htmlspecialchars($form->title->value))
->setNode($form->node->value);
//->setLocation($geo)
//->enableComments()
// Still usefull ? Check line 44
if($form->node->value == 'urn:xmpp:microblog:0') {
$p->enableComments();
}
$content = $content_xhtml = '';
if($form->content->value != '') {
$content = $form->content->value;
$content_xhtml = addHFR(Markdown::defaultTransform($content));
}
if($form->id->value != '') {
$p->setId($form->id->value);
$pd = new \modl\PostnDAO();
$post = $pd->getItem($form->id->value);
if(isset($post)) {
$p->setPublished(strtotime($post->published));
}
}
if(Validator::stringType()->notEmpty()->validate($form->tags->value)) {
$p->setTags(array_unique(
array_filter(
array_map(
function($value) {
if(Validator::stringType()->notEmpty()->validate($value)) {
preg_match('/([^\s[:punct:]]|_|-){3,30}/', trim($value), $matches);
if(isset($matches[0])) return $matches[0];
}
},
explode(',', $form->tags->value)
)
)
));
}
if($form->embed->value != '' && filter_var($form->embed->value, FILTER_VALIDATE_URL)) {
try {
$embed = Embed\Embed::create($form->embed->value);
$p->setLink($form->embed->value);
if(in_array($embed->type, array('photo', 'rich'))) {
$key = key($embed->images);
$p->setImage($embed->images[0]['value'], $embed->title, $embed->images[0]['mime']);
}
if($embed->type !== 'photo') {
$content_xhtml .= $this->prepareEmbed($embed);
}
} catch(Exception $e) {
error_log($e->getMessage());
}
}
if($content != '') {
$p->setContent(htmlspecialchars($content));
}
if($content_xhtml != '') {
$p->setContentXhtml($content_xhtml);
}
$p->request();
} else {
RPC::call('Publish.enableSend');
Notification::append(false, $this->__('publish.no_title'));
}
}
function ajaxEmbedTest($url)
{
if($url == '') {
return;
} elseif(!filter_var($url, FILTER_VALIDATE_URL)) {
Notification::append(false, $this->__('publish.valid_url'));
return;
}
try {
$embed = Embed\Embed::create($url);
$html = $this->prepareEmbed($embed);
RPC::call('movim_fill', 'preview', '');
RPC::call('movim_fill', 'gallery', '');
if(in_array($embed->type, array('photo', 'rich'))) {
RPC::call('movim_fill', 'gallery', $this->prepareGallery($embed));
}
if($embed->type !== 'photo') {
RPC::call('movim_fill', 'preview', $html);
}
} catch(Exception $e) {
error_log($e->getMessage());
}
}
function prepareEmbed($embed)
{
$view = $this->tpl();
$view->assign('embed', $embed);
return $view->draw('_publish_embed', true);
}
function prepareGallery($embed)
{
$view = $this->tpl();
$view->assign('embed', $embed);
return $view->draw('_publish_gallery', true);
}
private function validateServerNode($server, $node)
{
$validate_server = Validator::stringType()->noWhitespace()->length(6, 40);
$validate_node = Validator::stringType()->length(3, 100);
if(!$validate_server->validate($server)
|| !$validate_node->validate($node)
) return false;
else return true;
}
function display()
{
}
}