1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: class Ansel_Ajax_Imple_TagActions extends Horde_Core_Ajax_Imple
12: {
13: public function attach()
14: {
15:
16: Horde::addScriptFile('tagactions.js');
17:
18: $url = $this->_getUrl('TagActions', 'ansel', array('gallery' => $this->_params['gallery'],
19: 'image' => (isset($this->_params['image']) ? $this->_params['image'] : 0)));
20: $params = array('url' => (string)$url,
21: 'gallery' => $this->_params['gallery'],
22: 'image' => (isset($this->_params['image']) ? $this->_params['image'] : 0),
23: 'bindTo' => $this->_params['bindTo'],
24: 'input' => 'tags');
25: $js = array();
26: $js[] = "Ansel.ajax['tagActions'] = " . Horde_Serialize::serialize($params, Horde_Serialize::JSON) . ";";
27: $js[] = "Event.observe(Ansel.ajax.tagActions.bindTo.add, 'click', function(event) {addTag(); Event.stop(event)});";
28:
29: Horde::addInlineScript($js, 'dom');
30: }
31:
32: public function handle($args, $post)
33: {
34: $action = $args['action'];
35: $tags = $post['tags'];
36: if (empty($action) || empty($tags)) {
37: return array('response' => '0');
38: }
39:
40: $gallery = $args['gallery'];
41: $image = isset($args['image']) ? $args['image'] : null;
42: if ($image) {
43: $id = $image;
44: $type = 'image';
45: } else {
46: $id = $gallery;
47: $type = 'gallery';
48: }
49:
50: if (!is_numeric($id)) {
51: return array('response' => 0,
52: 'message' => sprintf(_("Invalid input %s"), htmlspecialchars($id)));
53: }
54:
55:
56: if ($type == 'gallery') {
57: $resource = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($id);
58: $parent = $resource;
59: } else {
60: $resource = $GLOBALS['injector']->getInstance('Ansel_Storage')->getImage($id);
61: $parent = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($resource->gallery);
62: }
63:
64: switch ($action) {
65: case 'add':
66: if (!empty($tags)) {
67: $tags = rawurldecode($post['tags']);
68: $tags = explode(',', $tags);
69: $GLOBALS['injector']->getInstance('Ansel_Tagger')->tag($id, $tags, $GLOBALS['registry']->getAuth(), $type);
70:
71:
72: $newTags = $GLOBALS['injector']->getInstance('Ansel_Tagger')->getTags($id, $type);
73: if (count($newTags)) {
74: $newTags = $GLOBALS['injector']->getInstance('Ansel_Tagger')->getTagInfo(array_keys($newTags));
75: }
76:
77: return array('response' => 1,
78: 'message' => $this->_getTagHtml($newTags,
79: $parent->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)));
80: }
81: break;
82:
83: case 'remove':
84: $GLOBALS['injector']->getInstance('Ansel_Tagger')->untag($resource->id, (int)$tags, $type);
85: $existingTags = $GLOBALS['injector']->getInstance('Ansel_Tagger')->getTags($resource->id, $type);
86: if (count($existingTags)) {
87: $newTags = $GLOBALS['injector']->getInstance('Ansel_Tagger')->getTagInfo(array_keys($existingTags));
88: } else {
89: $newTags = array();
90: }
91: return array('response' => 1,
92: 'message' => $this->_getTagHtml($newTags,
93: $parent->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)));
94: break;
95: }
96:
97: }
98:
99: private function _getTagHtml($tags, $hasEdit)
100: {
101: global $registry;
102: $links = Ansel::getTagLinks($tags, 'add');
103: $html = '<ul>';
104: foreach ($tags as $taginfo) {
105: $tag_id = $taginfo['tag_id'];
106: $html .= '<li>' . $links[$tag_id]->link(array('title' => sprintf(ngettext("%d photo", "%d photos", $taginfo['count']), $taginfo['count']))) . htmlspecialchars($taginfo['tag_name']) . '</a>' . ($hasEdit ? '<a href="#" onclick="removeTag(' . $tag_id . ');">' . Horde::img('delete-small.png', _("Remove Tag")) . '</a>' : '') . '</li>';
107: }
108: $html .= '</ul>';
109: return $html;
110: }
111:
112: }
113: