]> git.mxchange.org Git - friendica.git/blob - mod/tagrm.php
Rewrite Proxy module
[friendica.git] / mod / tagrm.php
1 <?php
2 /**
3  * @file mod/tagrm.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\L10n;
9 use Friendica\Database\DBA;
10 use Friendica\Model\Item;
11 use Friendica\Model\Term;
12 use Friendica\Util\Strings;
13
14 function tagrm_post(App $a)
15 {
16         if (!local_user()) {
17                 $a->internalRedirect($_SESSION['photo_return']);
18         }
19
20         if (!empty($_POST['submit']) && ($_POST['submit'] === L10n::t('Cancel'))) {
21                 $a->internalRedirect($_SESSION['photo_return']);
22         }
23
24         $tags = [];
25         foreach (defaults($_POST, 'tag', []) as $tag) {
26                 $tags[] = hex2bin(Strings::escapeTags(trim($tag)));
27         }
28
29         $item_id = defaults($_POST,'item', 0);
30         update_tags($item_id, $tags);
31         info(L10n::t('Tag(s) removed') . EOL);
32
33         $a->internalRedirect($_SESSION['photo_return']);
34         // NOTREACHED
35 }
36
37 /**
38  * Updates tags from an item
39  * @param $item_id
40  * @param $tags array
41  */
42 function update_tags($item_id, $tags){
43         if (empty($item_id) || empty($tags)){
44                 return;
45         }
46
47         $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
48         if (!DBA::isResult($item)) {
49                 return;
50         }
51
52         $old_tags = explode(',', $item['tag']);
53
54         foreach ($tags as $new_tag) {
55                 foreach ($old_tags as $index => $old_tag) {
56                         if (strcmp($old_tag, $new_tag) == 0) {
57                                 unset($old_tags[$index]);
58                                 break;
59                         }
60                 }
61         }
62
63         $tag_str = implode(',', $old_tags);
64         Term::insertFromTagFieldByItemId($item_id, $tag_str);
65 }
66
67 function tagrm_content(App $a)
68 {
69         $o = '';
70
71         if (!local_user()) {
72                 $a->internalRedirect($_SESSION['photo_return']);
73                 // NOTREACHED
74         }
75
76         if ($a->argc == 3) {
77                 update_tags($a->argv[1], [Strings::escapeTags(trim(hex2bin($a->argv[2])))]);
78                 $a->internalRedirect($_SESSION['photo_return']);
79         }
80
81         $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
82         if (!$item_id) {
83                 $a->internalRedirect($_SESSION['photo_return']);
84                 // NOTREACHED
85         }
86
87         $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
88         if (!DBA::isResult($item)) {
89                 $a->internalRedirect($_SESSION['photo_return']);
90         }
91
92         $arr = explode(',', $item['tag']);
93
94
95         if (empty($item['tag'])) {
96                 $a->internalRedirect($_SESSION['photo_return']);
97         }
98
99         $o .= '<h3>' . L10n::t('Remove Item Tag') . '</h3>';
100
101         $o .= '<p id="tag-remove-desc">' . L10n::t('Select a tag to remove: ') . '</p>';
102
103         $o .= '<form id="tagrm" action="tagrm" method="post" >';
104         $o .= '<input type="hidden" name="item" value="' . $item_id . '" />';
105         $o .= '<ul>';
106
107         foreach ($arr as $x) {
108                 $o .= '<li><input type="checkbox" name="tag[]" value="' . bin2hex($x) . '" >' . BBCode::convert($x) . '</input></li>';
109         }
110
111         $o .= '</ul>';
112         $o .= '<input id="tagrm-submit" type="submit" name="submit" value="' . L10n::t('Remove') .'" />';
113         $o .= '<input id="tagrm-cancel" type="submit" name="submit" value="' . L10n::t('Cancel') .'" />';
114         $o .= '</form>';
115
116         return $o;
117 }