streams/Code/Module/Impel.php

210 lines
7.2 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
2021-12-03 03:01:39 +00:00
2022-02-16 04:08:28 +00:00
namespace Code\Module;
2021-12-03 03:01:39 +00:00
use App;
2021-12-02 22:33:36 +00:00
use URLify;
2022-02-16 04:08:28 +00:00
use Code\Lib\IConfig;
use Code\Web\Controller;
use Code\Lib\Menu;
use Code\Lib\MenuItem;
2022-01-26 05:43:05 +00:00
2016-04-19 03:38:38 +00:00
// import page design element
2021-12-02 23:02:31 +00:00
class Impel extends Controller
{
public function init()
{
2022-09-04 01:35:50 +00:00
$ret = ['success' => false];
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
if (!local_channel()) {
2021-12-02 23:02:31 +00:00
json_return_and_die($ret);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
logger('impel: ' . print_r($_REQUEST, true), LOGGER_DATA);
$elm = $_REQUEST['element'];
$x = base64url_decode($elm);
2021-12-03 03:01:39 +00:00
if (!$x) {
2021-12-02 23:02:31 +00:00
json_return_and_die($ret);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$j = json_decode($x, true);
2021-12-03 03:01:39 +00:00
if (!$j) {
2021-12-02 23:02:31 +00:00
json_return_and_die($ret);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
// logger('element: ' . print_r($j,true));
$channel = App::get_channel();
$arr = [];
$is_menu = false;
// a portable menu has its links rewritten with the local baseurl
$portable_menu = false;
switch ($j['type']) {
case 'webpage':
$arr['item_type'] = ITEM_TYPE_WEBPAGE;
$namespace = 'WEBPAGE';
$installed_type = t('webpage');
break;
case 'block':
$arr['item_type'] = ITEM_TYPE_BLOCK;
$namespace = 'BUILDBLOCK';
$installed_type = t('block');
break;
case 'layout':
$arr['item_type'] = ITEM_TYPE_PDL;
$namespace = 'PDL';
$installed_type = t('layout');
break;
case 'portable-menu':
$portable_menu = true;
// fall through
case 'menu':
$is_menu = true;
$installed_type = t('menu');
break;
default:
logger('mod_impel: unrecognised element type' . print_r($j, true));
break;
}
if ($is_menu) {
$m = [];
$m['menu_channel_id'] = local_channel();
$m['menu_name'] = $j['pagetitle'];
$m['menu_desc'] = $j['desc'];
2021-12-03 03:01:39 +00:00
if ($j['created']) {
2021-12-02 23:02:31 +00:00
$m['menu_created'] = datetime_convert($j['created']);
2021-12-03 03:01:39 +00:00
}
if ($j['edited']) {
2021-12-02 23:02:31 +00:00
$m['menu_edited'] = datetime_convert($j['edited']);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$m['menu_flags'] = 0;
if ($j['flags']) {
2021-12-03 03:01:39 +00:00
if (in_array('bookmark', $j['flags'])) {
2021-12-02 23:02:31 +00:00
$m['menu_flags'] |= MENU_BOOKMARK;
2021-12-03 03:01:39 +00:00
}
if (in_array('system', $j['flags'])) {
2021-12-02 23:02:31 +00:00
$m['menu_flags'] |= MENU_SYSTEM;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
2022-01-26 05:43:05 +00:00
$menu_id = Menu::create($m);
2021-12-02 23:02:31 +00:00
if ($menu_id) {
if (is_array($j['items'])) {
foreach ($j['items'] as $it) {
$mitem = [];
$mitem['mitem_link'] = str_replace('[channelurl]', z_root() . '/channel/' . $channel['channel_address'], $it['link']);
$mitem['mitem_link'] = str_replace('[pageurl]', z_root() . '/page/' . $channel['channel_address'], $it['link']);
$mitem['mitem_link'] = str_replace('[cloudurl]', z_root() . '/cloud/' . $channel['channel_address'], $it['link']);
$mitem['mitem_link'] = str_replace('[baseurl]', z_root(), $it['link']);
$mitem['mitem_desc'] = escape_tags($it['desc']);
$mitem['mitem_order'] = intval($it['order']);
if (is_array($it['flags'])) {
$mitem['mitem_flags'] = 0;
2021-12-03 03:01:39 +00:00
if (in_array('zid', $it['flags'])) {
2021-12-02 23:02:31 +00:00
$mitem['mitem_flags'] |= MENU_ITEM_ZID;
2021-12-03 03:01:39 +00:00
}
if (in_array('new-window', $it['flags'])) {
2021-12-02 23:02:31 +00:00
$mitem['mitem_flags'] |= MENU_ITEM_NEWWIN;
2021-12-03 03:01:39 +00:00
}
if (in_array('chatroom', $it['flags'])) {
2021-12-02 23:02:31 +00:00
$mitem['mitem_flags'] |= MENU_ITEM_CHATROOM;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
2022-01-26 05:43:05 +00:00
MenuItem::add($menu_id, local_channel(), $mitem);
2021-12-02 23:02:31 +00:00
}
if ($j['edited']) {
2021-12-03 03:01:39 +00:00
$x = q(
"update menu set menu_edited = '%s' where menu_id = %d and menu_channel_id = %d",
2021-12-02 23:02:31 +00:00
dbesc(datetime_convert('UTC', 'UTC', $j['edited'])),
intval($menu_id),
intval(local_channel())
);
}
}
$ret['success'] = true;
}
$x = $ret;
} else {
$arr['uid'] = local_channel();
$arr['aid'] = $channel['channel_account_id'];
$arr['title'] = $j['title'];
$arr['body'] = $j['body'];
$arr['term'] = $j['term'];
$arr['layout_mid'] = $j['layout_mid'];
$arr['created'] = datetime_convert('UTC', 'UTC', $j['created']);
$arr['edited'] = datetime_convert('UTC', 'UTC', $j['edited']);
$arr['owner_xchan'] = get_observer_hash();
$arr['author_xchan'] = (($j['author_xchan']) ? $j['author_xchan'] : get_observer_hash());
$arr['mimetype'] = (($j['mimetype']) ? $j['mimetype'] : 'text/x-multicode');
2021-12-02 23:02:31 +00:00
if (!$j['mid']) {
$j['uuid'] = new_uuid();
$j['mid'] = z_root() . '/item/' . $j['uuid'];
}
$arr['uuid'] = $j['uuid'];
$arr['mid'] = $arr['parent_mid'] = $j['mid'];
if ($j['pagetitle']) {
$pagetitle = strtolower(URLify::transliterate($j['pagetitle']));
}
2021-12-03 03:01:39 +00:00
$i = q(
"select id, edited, item_deleted from item where mid = '%s' and uid = %d limit 1",
2021-12-02 23:02:31 +00:00
dbesc($arr['mid']),
intval(local_channel())
);
IConfig::Set($arr, 'system', $namespace, (($pagetitle) ? $pagetitle : substr($arr['mid'], 0, 16)), true);
if ($i) {
$arr['id'] = $i[0]['id'];
// don't update if it has the same timestamp as the original
2021-12-03 03:01:39 +00:00
if ($arr['edited'] > $i[0]['edited']) {
2022-12-21 20:20:55 +00:00
$x = item_store_update($arr);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
} else {
if (($i) && (intval($i[0]['item_deleted']))) {
// was partially deleted already, finish it off
2021-12-03 03:01:39 +00:00
q(
"delete from item where mid = '%s' and uid = %d",
2021-12-02 23:02:31 +00:00
dbesc($arr['mid']),
intval(local_channel())
);
2021-12-03 03:01:39 +00:00
} else {
2022-12-21 20:20:55 +00:00
$x = item_store($arr);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
if ($x && $x['success']) {
$item_id = $x['item_id'];
}
}
if ($x['success']) {
$ret['success'] = true;
info(sprintf(t('%s element installed'), $installed_type));
} else {
notice(sprintf(t('%s element installation failed'), $installed_type));
}
//??? should perhaps return ret?
json_return_and_die(true);
}
2016-04-19 03:38:38 +00:00
}