2010-11-08 23:37:58 +01:00
|
|
|
<?php
|
2018-01-21 19:33:59 +01:00
|
|
|
/**
|
|
|
|
* @file mod/tagrm.php
|
|
|
|
*/
|
2018-02-15 03:33:55 +01:00
|
|
|
|
2017-04-30 06:07:00 +02:00
|
|
|
use Friendica\App;
|
2018-02-15 03:33:55 +01:00
|
|
|
use Friendica\Content\Text\BBCode;
|
2018-01-21 19:33:59 +01:00
|
|
|
use Friendica\Core\L10n;
|
2017-08-26 08:04:21 +02:00
|
|
|
use Friendica\Core\System;
|
2018-07-21 14:40:21 +02:00
|
|
|
use Friendica\Database\DBA;
|
2018-02-06 13:40:22 +01:00
|
|
|
use Friendica\Model\Item;
|
2018-10-23 19:29:59 +02:00
|
|
|
use Friendica\Model\Term;
|
2017-04-30 06:07:00 +02:00
|
|
|
|
2018-06-18 22:36:34 +02:00
|
|
|
function tagrm_post(App $a)
|
|
|
|
{
|
2018-02-06 13:40:22 +01:00
|
|
|
if (!local_user()) {
|
2018-10-19 20:11:27 +02:00
|
|
|
$a->internalRedirect($_SESSION['photo_return']);
|
2016-12-20 12:35:18 +01:00
|
|
|
}
|
2010-11-08 23:37:58 +01:00
|
|
|
|
2018-06-18 22:36:34 +02:00
|
|
|
if (x($_POST,'submit') && ($_POST['submit'] === L10n::t('Cancel'))) {
|
2018-10-19 20:11:27 +02:00
|
|
|
$a->internalRedirect($_SESSION['photo_return']);
|
2016-12-20 12:35:18 +01:00
|
|
|
}
|
2010-11-08 23:37:58 +01:00
|
|
|
|
2018-10-23 11:40:20 +02:00
|
|
|
$tags = [];
|
2018-10-23 19:29:59 +02:00
|
|
|
foreach (defaults($_POST, 'tag', []) as $tag) {
|
|
|
|
array_push($tags, hex2bin(notags(trim($tag))));
|
2018-10-23 11:40:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$item_id = defaults($_POST,'item', 0);
|
|
|
|
update_tags($item_id, $tags);
|
|
|
|
|
|
|
|
$a->internalRedirect($_SESSION['photo_return']);
|
|
|
|
|
|
|
|
// NOTREACHED
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:32:32 +02:00
|
|
|
/**
|
|
|
|
* Updates tags from an item
|
|
|
|
* @param $item_id
|
|
|
|
* @param $tags array
|
|
|
|
*/
|
2018-10-23 11:40:20 +02:00
|
|
|
function update_tags($item_id, $tags){
|
|
|
|
if (empty($item_id) || empty($tags)){
|
2018-10-23 21:18:07 +02:00
|
|
|
return;
|
2018-10-23 11:40:20 +02:00
|
|
|
}
|
2010-11-08 23:37:58 +01:00
|
|
|
|
2018-06-18 22:36:34 +02:00
|
|
|
$item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
|
2018-07-21 14:46:04 +02:00
|
|
|
if (!DBA::isResult($item)) {
|
2018-10-23 21:18:07 +02:00
|
|
|
return;
|
2016-12-20 10:10:33 +01:00
|
|
|
}
|
2010-11-08 23:37:58 +01:00
|
|
|
|
2018-10-23 19:29:59 +02:00
|
|
|
$old_tags = explode(',', $item['tag']);
|
2018-10-23 11:40:20 +02:00
|
|
|
|
2018-10-23 19:29:59 +02:00
|
|
|
foreach ($tags as $new_tag) {
|
|
|
|
foreach ($old_tags as $count => $old_tag) {
|
|
|
|
if (strcmp($old_tag, $new_tag) == 0) {
|
|
|
|
unset($old_tags[$count]);
|
2018-10-23 11:40:20 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-11-08 23:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 19:29:59 +02:00
|
|
|
$tag_str = implode(',',$old_tags);
|
2018-10-23 21:18:07 +02:00
|
|
|
Term::insertFromTagFieldByItemId($item_id, $tag_str);
|
2010-11-08 23:37:58 +01:00
|
|
|
|
2018-10-23 11:40:20 +02:00
|
|
|
info(L10n::t('Tag(s) removed') . EOL );
|
2010-11-08 23:37:58 +01:00
|
|
|
}
|
|
|
|
|
2018-06-18 22:36:34 +02:00
|
|
|
function tagrm_content(App $a)
|
|
|
|
{
|
2010-11-08 23:37:58 +01:00
|
|
|
$o = '';
|
|
|
|
|
2018-02-06 13:40:22 +01:00
|
|
|
if (!local_user()) {
|
2018-10-19 20:11:27 +02:00
|
|
|
$a->internalRedirect($_SESSION['photo_return']);
|
2010-11-08 23:37:58 +01:00
|
|
|
// NOTREACHED
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:40:20 +02:00
|
|
|
if ($a->argc == 3){
|
|
|
|
update_tags($a->argv[1], [hex2bin(notags(trim($a->argv[2])))]);
|
2018-10-23 11:44:31 +02:00
|
|
|
$a->internalRedirect($_SESSION['photo_return']);
|
2018-10-23 11:40:20 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 22:36:34 +02:00
|
|
|
$item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
|
|
|
|
if (!$item_id) {
|
2018-10-19 20:11:27 +02:00
|
|
|
$a->internalRedirect($_SESSION['photo_return']);
|
2010-11-08 23:37:58 +01:00
|
|
|
// NOTREACHED
|
|
|
|
}
|
|
|
|
|
2018-06-18 22:36:34 +02:00
|
|
|
$item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
|
2018-07-21 14:46:04 +02:00
|
|
|
if (!DBA::isResult($item)) {
|
2018-10-19 20:11:27 +02:00
|
|
|
$a->internalRedirect($_SESSION['photo_return']);
|
2016-12-20 10:10:33 +01:00
|
|
|
}
|
2010-11-08 23:37:58 +01:00
|
|
|
|
2018-06-18 22:36:34 +02:00
|
|
|
$arr = explode(',', $item['tag']);
|
2010-11-08 23:37:58 +01:00
|
|
|
|
2018-10-23 11:40:20 +02:00
|
|
|
|
|
|
|
if (empty($item['tag'])) {
|
2018-10-19 20:11:27 +02:00
|
|
|
$a->internalRedirect($_SESSION['photo_return']);
|
2016-12-20 11:56:34 +01:00
|
|
|
}
|
2010-11-08 23:37:58 +01:00
|
|
|
|
2018-01-22 15:16:25 +01:00
|
|
|
$o .= '<h3>' . L10n::t('Remove Item Tag') . '</h3>';
|
2010-11-08 23:37:58 +01:00
|
|
|
|
2018-01-22 15:16:25 +01:00
|
|
|
$o .= '<p id="tag-remove-desc">' . L10n::t('Select a tag to remove: ') . '</p>';
|
2010-11-08 23:37:58 +01:00
|
|
|
|
|
|
|
$o .= '<form id="tagrm" action="tagrm" method="post" >';
|
2018-06-18 22:36:34 +02:00
|
|
|
$o .= '<input type="hidden" name="item" value="' . $item_id . '" />';
|
2010-11-08 23:37:58 +01:00
|
|
|
$o .= '<ul>';
|
|
|
|
|
2016-12-20 21:31:05 +01:00
|
|
|
foreach ($arr as $x) {
|
2018-10-23 11:40:20 +02:00
|
|
|
$o .= '<li><input type="checkbox" name="tag[]" value="' . bin2hex($x) . '" >' . BBCode::convert($x) . '</input></li>';
|
2010-11-08 23:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$o .= '</ul>';
|
2018-01-22 15:16:25 +01:00
|
|
|
$o .= '<input id="tagrm-submit" type="submit" name="submit" value="' . L10n::t('Remove') .'" />';
|
|
|
|
$o .= '<input id="tagrm-cancel" type="submit" name="submit" value="' . L10n::t('Cancel') .'" />';
|
2010-11-08 23:37:58 +01:00
|
|
|
$o .= '</form>';
|
|
|
|
|
|
|
|
return $o;
|
2011-05-23 11:39:57 +02:00
|
|
|
}
|