cinecturlink2/src/ManageLink.php

320 lines
14 KiB
PHP
Raw Permalink Normal View History

2023-08-24 11:55:44 +00:00
<?php
2023-10-15 18:49:39 +00:00
2023-08-24 11:55:44 +00:00
declare(strict_types=1);
namespace Dotclear\Plugin\cinecturlink2;
2023-10-15 18:49:39 +00:00
use Dotclear\App;
2023-08-24 11:55:44 +00:00
use Dotclear\Core\Backend\{
Notices,
Page
};
use Dotclear\Core\Process;
use Dotclear\Helper\Html\Form\{
Div,
Form,
Input,
Label,
Link,
Note,
Number,
Para,
Select,
Submit
};
use Dotclear\Helper\Html\Html;
use Dotclear\Helper\Network\Http;
use Exception;
2023-10-15 18:49:39 +00:00
/**
* @brief cinecturlink2 manage link class.
* @ingroup cinecturlink2
*
* @author Jean-Christian Denis (author)
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-08-24 11:55:44 +00:00
class ManageLink extends Process
{
private static string $module_redir = '';
2023-10-23 20:54:27 +00:00
private static RecordLinksRow $row;
2023-08-24 11:55:44 +00:00
public static function init(): bool
{
return self::status(My::checkContext(My::MANAGE) && ($_REQUEST['part'] ?? 'links') == 'link');
}
public static function process(): bool
{
if (!self::status() || ($_REQUEST['part'] ?? 'links') != 'link') {
return false;
}
self::$module_redir = $_REQUEST['redir'] ?? '';
2023-10-23 20:54:27 +00:00
self::$row = new RecordLinksRow();
$utils = new Utils();
2023-08-24 11:55:44 +00:00
if (!empty($_POST['save'])) {
try {
Utils::makePublicDir(
2023-10-15 18:49:39 +00:00
App::config()->dotclearRoot() . '/' . App::blog()->settings()->system->get('public_path'),
2023-08-24 11:55:44 +00:00
My::settings()->folder
);
2023-10-23 20:54:27 +00:00
if (empty(self::$row->link_title)) {
2023-08-24 11:55:44 +00:00
throw new Exception(__('You must provide a title.'));
}
2023-10-23 20:54:27 +00:00
if (empty(self::$row->link_author)) {
2023-08-24 11:55:44 +00:00
throw new Exception(__('You must provide an author.'));
}
2023-10-23 20:54:27 +00:00
if (!preg_match('/https?:\/\/.+/', self::$row->link_img)) {
2023-08-24 11:55:44 +00:00
//throw new Exception(__('You must provide a link to an image.'));
}
// create a link
2023-10-23 20:54:27 +00:00
if (!self::$row->link_id) {
$exists = $utils->getLinks(['link_title' => self::$row->link_title], true)->f(0);
2023-08-24 11:55:44 +00:00
if ($exists) {
throw new Exception(__('Link with same name already exists.'));
}
2023-10-23 20:54:27 +00:00
$link_id = $utils->addLink(self::$row->getCursor());
2023-08-24 11:55:44 +00:00
Notices::addSuccessNotice(
__('Link successfully created.')
);
// update a link
} else {
2023-10-23 20:54:27 +00:00
$exists = $utils->getLinks(['link_id' => self::$row->link_id], true)->f(0);
2023-08-24 11:55:44 +00:00
if (!$exists) {
throw new Exception(__('Unknown link.'));
}
2023-10-23 20:54:27 +00:00
$link_id = $utils->updLink(self::$row->link_id, self::$row->getCursor());
2023-08-24 11:55:44 +00:00
Notices::addSuccessNotice(
__('Link successfully updated.')
);
}
My::redirect(
[
2023-10-23 20:54:27 +00:00
'part' => 'link',
'link_id' => $link_id,
'redir' => self::$module_redir,
2023-08-24 11:55:44 +00:00
]
);
} catch (Exception $e) {
2023-10-15 18:49:39 +00:00
App::error()->add($e->getMessage());
2023-08-24 11:55:44 +00:00
}
}
2023-10-23 20:54:27 +00:00
if (!empty($_POST['delete']) && self::$row->link_id) {
2023-08-24 11:55:44 +00:00
try {
2023-10-23 20:54:27 +00:00
$utils->delLink(self::$row->link_id);
2023-08-24 11:55:44 +00:00
Notices::addSuccessNotice(
__('Link successfully deleted.')
);
if (!empty($_POST['redir'])) {
Http::redirect(self::$module_redir);
} else {
My::redirect(['part' => 'links']);
}
} catch (Exception $e) {
2023-10-15 18:49:39 +00:00
App::error()->add($e->getMessage());
2023-08-24 11:55:44 +00:00
}
}
2023-10-23 20:54:27 +00:00
if (self::$row->link_id) {
self::$row = new RecordLinksRow(
$utils->getLinks(['link_id' => self::$row->link_id])
);
2023-08-24 11:55:44 +00:00
}
return true;
}
public static function render(): void
{
if (!self::status() || ($_REQUEST['part'] ?? 'links') != 'link') {
return;
}
$mc = Combo::mediaCombo();
Page::openModule(
My::name(),
2023-10-15 18:49:39 +00:00
Page::jsVars(['dotclear.c2_lang' => App::auth()->getInfo('user_lang')]) .
2023-08-24 11:55:44 +00:00
My::jsLoad('c2link')
);
echo
Page::breadcrumb([
2023-10-23 20:54:27 +00:00
__('Plugins') => '',
My::name() => My::manageUrl(),
(empty(self::$row->link_id) ? __('New link') : __('Edit link')) => '',
2023-08-24 11:55:44 +00:00
]) .
Notices::getNotices();
if (!empty(self::$module_redir)) {
echo (new Para())
->items([
(new Link())
->class('back')
->href(self::$module_redir)
->text(__('Back')),
])
->render();
}
echo (new Div())
->items([
(new Form('newlinkform'))
->action(My::manageUrl())
->method('post')
->fields([
(new Div())
->class('two-cols clearfix')
->items([
(new Div())
->class('col70')
->items([
(new Para())
->items([
(new Label(__('Title:'), Label::OUTSIDE_LABEL_BEFORE))
2023-10-23 20:54:27 +00:00
->for('link_title'),
(new Input('link_title'))
2023-08-24 11:55:44 +00:00
->size(65)
2023-10-23 09:28:28 +00:00
->maxlength(255)
2023-10-23 20:54:27 +00:00
->value(Html::escapeHTML(self::$row->link_title)),
2023-08-24 11:55:44 +00:00
]),
(new Para())
->items([
(new Label(__('Description:'), Label::OUTSIDE_LABEL_BEFORE))
2023-10-23 20:54:27 +00:00
->for('link_desc'),
(new Input('link_desc'))
2023-08-24 11:55:44 +00:00
->size(65)
2023-10-23 09:28:28 +00:00
->maxlength(255)
2023-10-23 20:54:27 +00:00
->value(Html::escapeHTML(self::$row->link_desc)),
2023-08-24 11:55:44 +00:00
]),
(new Para())
->items([
(new Label(__('Author:'), Label::OUTSIDE_LABEL_BEFORE))
2023-10-23 20:54:27 +00:00
->for('link_author'),
(new Input('link_author'))
2023-08-24 11:55:44 +00:00
->size(65)
2023-10-23 09:28:28 +00:00
->maxlength(255)
2023-10-23 20:54:27 +00:00
->value(Html::escapeHTML(self::$row->link_author)),
2023-08-24 11:55:44 +00:00
]),
(new Para())
->items([
(new Label(__('Details URL:'), Label::OUTSIDE_LABEL_BEFORE))
2023-10-23 20:54:27 +00:00
->for('link_url'),
(new Input('link_url'))
2023-08-24 11:55:44 +00:00
->size(65)
2023-10-23 09:28:28 +00:00
->maxlength(255)
2023-10-23 20:54:27 +00:00
->value(Html::escapeHTML(self::$row->link_url)),
2023-08-24 11:55:44 +00:00
(new Link('newlinksearch'))
->class('modal hidden-if-no-js')
->href('http://google.com')
->title(__('Search with Google'))
->text(__('Search with Google')),
]),
(new Para())
->items([
(new Label(__('Image URL:'), Label::OUTSIDE_LABEL_BEFORE))
2023-10-23 20:54:27 +00:00
->for('link_img'),
(new Input('link_img'))
2023-08-24 11:55:44 +00:00
->size(65)
2023-10-23 09:28:28 +00:00
->maxlength(255)
2023-10-23 20:54:27 +00:00
->value(Html::escapeHTML(self::$row->link_img)),
2023-08-24 11:55:44 +00:00
(new Link('newimagesearch'))
->class('modal hidden-if-no-js')
->href('http://amazon.com')
->title(__('Search with Amazon'))
->text(__('Search with Amazon')),
]),
...(
empty($mc) ?
[(new Note())
->class('form-note')
->text(__('There is no image in cinecturlink media path.'))] :
[(new Para())
->items([
(new Label(__('or select from repository:'), Label::OUTSIDE_LABEL_BEFORE))
->for('newimageselect'),
(new Select('newimageselect'))
->items($mc)
->default(''),
]),
(new Para())
->class('form-note')
->items([
(new Link())
->class('modal hidden-if-no-js')
2023-10-15 18:49:39 +00:00
->href(App::backend()->url()->get('admin.media', ['d' => (string) My::settings()->folder]))
2023-08-24 11:55:44 +00:00
->title(__('Media manager'))
->text(__('Go to media manager to add image to cinecturlink path.')),
]),
]
),
]),
(new Div())
->class('col30')
->items([
(new Para())
->items([
(new Label(__('Category:'), Label::OUTSIDE_LABEL_BEFORE))
2023-10-23 20:54:27 +00:00
->for('cat_id'),
(new Select('cat_id'))
2023-08-24 11:55:44 +00:00
->items(Combo::categoriesCombo())
2023-10-23 20:54:27 +00:00
->default((string) self::$row->cat_id),
2023-08-24 11:55:44 +00:00
]),
(new Para())
->items([
(new Label(__('Lang:'), Label::OUTSIDE_LABEL_BEFORE))
2023-10-23 20:54:27 +00:00
->for('link_lang'),
(new Select('link_lang'))
2023-08-24 11:55:44 +00:00
->items(Combo::langsCombo())
2023-10-23 20:54:27 +00:00
->default(self::$row->link_lang),
2023-08-24 11:55:44 +00:00
]),
(new Para())
->items([
(new Label(__('Rating:'), Label::OUTSIDE_LABEL_BEFORE))
2023-10-23 20:54:27 +00:00
->for('link_note'),
(new Number('link_note'))
2023-08-24 11:55:44 +00:00
->min(0)
->max(20)
2023-10-23 20:54:27 +00:00
->value(self::$row->link_note),
2023-08-24 11:55:44 +00:00
]),
]),
]),
(new Para())
->class('border-top')
->separator(' ')
->items([
(new Submit('save'))
->value(__('Save') . ' (s)')
->accesskey('s'),
(new Link())
->class('button')
->href(My::manageUrl(['part' => 'links']))
->title(__('Cancel'))
->text(__('Cancel') . ' (c)')
->accesskey('c'),
(new Submit('delete'))
->class('delete')
->value(__('Delete') . ' (d)')
->accesskey('d'),
... My::hiddenFields([
2023-10-23 20:54:27 +00:00
'link_id' => self::$row->link_id,
'part' => 'link',
'redir' => self::$module_redir,
2023-08-24 11:55:44 +00:00
]),
]),
]),
])
->render();
Page::helpBlock(My::id());
Page::closeModule();
}
}