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