1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12: class Ansel_Faces
13: {
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: static public function delete(Ansel_Image $image, $face = null)
26: {
27: if ($image->facesCount == 0) {
28: return true;
29: }
30:
31: $path = self::getVFSPath($image->id) . '/faces';
32: $ext = self::getExtension();
33:
34: if ($face === null) {
35: $sql = 'SELECT face_id FROM ansel_faces WHERE image_id = ' . $image->id;
36: try {
37: $faces = $GLOBALS['ansel_db']->selectValues($sql);
38: } catch (Horde_Db_Exception $e) {
39: throw new Ansel_Exception($e);
40: }
41: try {
42: foreach ($faces as $id) {
43: $GLOBALS['injector']
44: ->getInstance('Horde_Core_Factory_Vfs')
45: ->create('images')
46: ->deleteFile($path, $id . $ext);
47: }
48: } catch (Horde_Vfs_Exception $e) {}
49: try {
50: $GLOBALS['ansel_db']->delete('DELETE FROM ansel_faces WHERE '
51: . 'image_id = ' . $image->id);
52: $GLOBALS['ansel_db']->update('UPDATE ansel_images SET '
53: . 'image_faces = 0 WHERE image_id = ' . $image->id
54: . ' AND image_faces > 0 ');
55: } catch (Horde_Db_Exception $e) {
56: throw new Ansel_Exception($e);
57: }
58: $gallery = $GLOBALS['injector']
59: ->getInstance('Ansel_Storage')
60: ->getGallery($image->gallery);
61: $gallery->set('faces', $gallery->get('faces') - count($faces), true);
62: } else {
63: try {
64: $GLOBALS['injector']
65: ->getInstance('Horde_Core_Factory_Vfs')
66: ->create('images')
67: ->deleteFile($path, (int)$face . $ext);
68: } catch (Horde_Vfs_Exception $e) {}
69: try {
70: $GLOBALS['ansel_db']->delete('DELETE FROM ansel_faces WHERE'
71: . ' face_id = ' . (int)$face);
72: $GLOBALS['ansel_db']->update('UPDATE ansel_images SET '
73: . 'image_faces = image_faces - 1 WHERE image_id = '
74: . $image->id . ' AND image_faces > 0 ');
75: } catch (Horde_Db_Exception $e) {
76: throw new Ansel_Exception($e);
77: }
78: $gallery = $GLOBALS['injector']
79: ->getInstance('Ansel_Storage')
80: ->getGallery($image->gallery);
81: $gallery->set('faces', $gallery->get('faces') - 1, true);
82: }
83: }
84:
85: 86: 87: 88: 89:
90: static public function getVFSPath($image)
91: {
92: return '.horde/ansel/' . substr(str_pad($image, 2, 0, STR_PAD_LEFT), -2) . '/';
93: }
94:
95: 96: 97: 98:
99: static public function getExtension()
100: {
101: if ($GLOBALS['conf']['image']['type'] == 'jpeg') {
102: return '.jpg';
103: } else {
104: return '.png';
105: }
106: }
107:
108: 109: 110: 111: 112: 113: 114:
115: static public function getLink(array $face)
116: {
117: return Ansel::getUrlFor(
118: 'view',
119: array('view' => 'Image',
120: 'gallery' => $face['gallery_id'],
121: 'image' => $face['image_id']));
122: }
123:
124: 125: 126: 127: 128: 129: 130:
131: static public function getFaceTile($face)
132: {
133: $faces = $GLOBALS['injector']->getInstance('Ansel_Faces');
134: if (!is_array($face)) {
135: $face = $faces->getFaceById($face, true);
136: }
137: $face_id = $face['face_id'];
138:
139:
140: $imghtml = sprintf("<img src=\"%s\" class=\"bordered-facethumb\" id=\"%s\" alt=\"%s\" />",
141: $faces->getFaceUrl($face['image_id'], $face_id),
142: 'facethumb' . $face_id,
143: htmlspecialchars($face['face_name']));
144:
145: $img_view_url = Ansel::getUrlFor('view',
146: array('gallery' => $face['gallery_id'],
147: 'view' => 'Image',
148: 'image'=> $face['image_id'],
149: 'havesearch' => false));
150:
151:
152: $html = '<div id="face' . $face_id . '"><table><tr><td>' . $img_view_url->link() . $imghtml . '</a></td><td>';
153: if (!empty($face['face_name'])) {
154: $html .= Horde::url('faces/face.php')->add('face', $face['face_id'])->link() . $face['face_name'] . '</a><br />';
155: }
156:
157:
158: if (empty($face['face_name']) && $GLOBALS['conf']['report_content']['driver']) {
159: $html .= Horde::url('faces/claim.php')->add('face', $face_id)->link(array('title' => _("Do you know someone in this photo?"))) . _("Claim") . '</a>';
160: }
161:
162:
163: if (Horde_Util::loadExtension('libpuzzle') !== false) {
164: $html .= Horde::url('faces/search/image_search.php')->add('face_id', $face_id)->link() . _("Find similar") . '</a>';
165: }
166: $html .= '</div></td></tr></table>';
167:
168: return $html;
169: }
170:
171: }
172: