/[drupal]/contributions/modules/ldap_addressbook/Format.inc
ViewVC logotype

Contents of /contributions/modules/ldap_addressbook/Format.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Tue Mar 28 20:15:19 2006 UTC (3 years, 7 months ago) by rabello
Branch: MAIN
CVS Tags: DRUPAL-4-7--1-0, HEAD
Branch point for: DRUPAL-5, DRUPAL-4-7
File MIME type: text/x-php
Import of revamped ldap_addressbook module, compatible with dp-47+
1 <?php //Dear emacs, please make this buffer -*- php -*-
2
3 // $Id$
4
5 /**
6 * @file
7 * This file contains a set of constructions that help us formatting the
8 * output of LDAP searches and views.
9 *
10 * Copyright (C) 2006 Andre dos Anjos
11 *
12 * This file is part of the ldap_addresbook module for Drupal.
13 *
14 * The ldap_addressbook module is free software; you can redistribute it
15 * and/or modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of the License,
17 * or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * more details.
23 *
24 * You should have received a copy of the GNU General Public License along with
25 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
26 * Place, Suite 330, Boston, MA 02111-1307 USA
27 **/
28
29 /**
30 * Returns the name of a temporary file which contains the image given as
31 * parameter rescaled to fit the given maximum size parameter. The rescaling
32 * executed by this function will have optimal resampling.
33 *
34 * @param $original_imagefilename The input jpeg image file
35 * @param $size_hint The maximum size hint for this image
36 * @param $tmp_path The temporary directory where to put the resulting file
37 * @param $prefix A string prefix, so I can generate a filename that is easy
38 * to identify
39 **/
40 function _lab_rescale_file($original_imagefilename, $size_hint,
41 $tmp_path, $prefix) {
42 $value = getimagesize($original_imagefilename);
43 if (!$value) return FALSE;
44 list($width_orig, $height_orig, $type_orig, $attr_orig) = $value;
45
46 $width = $width_orig;
47 $height = $height_orig;
48 if ($width > $height) {
49 if ($width > 0) $height = ((float)$size_hint/$width) * $height_orig;
50 $width = $size_hint;
51 }
52 else {
53 if ($height > 0) $width = ((float)$size_hint/$height) * $width_orig;
54 $height = $size_hint;
55 }
56 //trigger_error('Resizing image to (w x h) = ('.$width.' x '.$height.')');
57
58 // Resample
59 $resampled_image = imagecreatetruecolor($width, $height);
60 $image = imagecreatefromjpeg($original_imagefilename);
61 imagecopyresampled($resampled_image, $image, 0, 0, 0, 0, $width, $height,
62 $width_orig, $height_orig);
63
64 // Save again
65 $resampled_imagefilename = tempnam($tmp_path, $prefix);
66 imagejpeg($resampled_image, $resampled_imagefilename, 100);
67
68 // Destroy the existing images
69 imagedestroy($image);
70 imagedestroy($resampled_image);
71
72 // Return happily
73 chmod($resampled_imagefilename, 0644);
74 return $resampled_imagefilename;
75 }
76
77 /**
78 * Returns the name of a temporary file which contains the image given as
79 * parameter rescaled to fit the given maximum size parameter. The rescaling
80 * executed by this function will have optimal resampling.
81 *
82 * @param $jpeg The input jpeg image
83 * @param $size_hint The maximum size hint for this image
84 * @param $tmp_path The temporary directory where to put the resulting file
85 * @param $prefix A string prefix, so I can generate a filename that is easy
86 * to identify
87 **/
88 function _lab_rescale($jpeg, $size_hint, $tmp_path, $prefix) {
89 //save the jpeg initially
90 $original_imagefilename = tempnam($tmp_path, $prefix);
91 $original_imagefile = fopen($original_imagefilename, 'wb');
92 fwrite($original_imagefile, $jpeg);
93 fclose($original_imagefile);
94 $resampled_imagefilename =
95 _lab_rescale_file($original_imagefilename, $size_hint,
96 $tmp_path, $prefix);
97 // Remove the original file
98 unlink($original_imagefilename);
99 return $resampled_imagefilename; //this may be false
100 }
101
102 /**
103 * This function translates the names and meanings of most of the LDAP fields
104 * of interest. If you have any strings wishing translation, just place them
105 * here.
106 *
107 * @param k The name of the string needing translation
108 * @return The human-readable format of the string, if available, or itself.
109 **/
110 function _lat($k) {
111 $dict = array('cn' => t('Name'),
112 'givenname' => t('First name'),
113 'sn' => t('Last name'),
114 'jpegphoto' => t('Photo'),
115 'mail' => t('E-mail'),
116 'homepostaladdress' => t('Home address'),
117 'postaladdress' => t('Work address'),
118 'postalcode' => t('Work postal code'),
119 'postalcode' => t('Work zip'),
120 'initials' => t('Initials'),
121 'title' => t('Title'),
122 'telephonenumber' => t('Telephone'),
123 'homephone' => t('Private phone number'),
124 'mobile' => t('Mobile'),
125 'o' => t('Organization'),
126 'labeleduri' => t('Web address'));
127 $lk = strtolower($k);
128 if (array_key_exists($lk, $dict)) {
129 return $dict[$lk];
130 }
131 return $k;
132 }
133
134 /**
135 * This function can output a Drupal themed table that contains 1 addressbook
136 * contact in compact form. This is the reasoning behind this view:
137 * # The user photo, if there is any, is reduced
138 * # The phone numbers are all cluttered together
139 * # The mail and URL are grouped together
140 * # We use graphical icons to show the view/edit/delete urls, since they are
141 * more compact than text.
142 *
143 * @param $info The entry from the LDAP server
144 * @param $summary The summary fields I'm supposed to work with
145 * @param $path The path to the module
146 * @param $tmp_path The temporary directory where I should be able to create
147 * temporary files.
148 * @param $edit TRUE if the user has edit permissions (this will make me
149 * generate links for the edit and delete panes.
150 * @param $privacy The privacy keyword to fill in to the links (either
151 * 'global', for global addressbook entries or 'private').
152 *
153 * @param $info An LDAP entry for which you want to create a compact view.
154 */
155 function _lab_compact_view($info, $summary, $path, $tmp_path, $edit, $privacy) {
156 $cn = $info["cn"][0];
157 $row = array();
158
159 $imgdir = "modules/$path/image";
160
161
162 $images = l(theme_image($imgdir.'/view.png', t('view'), t('view')),
163 $path."/node/$privacy/$cn/view", array(), NULL, NULL, FALSE, TRUE);
164 if ($edit) {
165 $images .= '<br />'.
166 l(theme_image($imgdir.'/edit.png', t('edit'), t('edit')),
167 $path."/node/$privacy/$cn/edit", array(), NULL, NULL, FALSE, TRUE).
168 '<br />'.
169 l(theme_image($imgdir.'/delete.png', t('delete'), t('delete')),
170 $path."/node/$privacy/$cn/delete", array(), NULL, NULL, FALSE, TRUE);
171 }
172 $row[] = $images;
173
174 //jpeg photo comes first
175 if (in_array('jpegphoto', $summary)) {
176 if(array_key_exists('jpegphoto', $info)) {
177 $imagefilename = _lab_rescale($info['jpegphoto'][0], 70,
178 $tmp_path, $path);
179 if (!$imagefilename) {
180 drupal_set_message(t("JPEG image for contact '$cn' seems corrupted. Fix it whenever possible!"));
181 $row[] = l(t("[Corrupted image]"),
182 $path."/node/$privacy/$cn/view", array(), NULL, NULL, FALSE, TRUE);
183 }
184 else {
185 $row[] = l(theme_image($imagefilename, '['.$info['cn'][0].'\'s photo]',
186 '['.$info['cn'][0].'\'s photo]'),
187 $path."/node/$privacy/$cn/view", array(), NULL, NULL, FALSE, TRUE);
188 }
189 }
190 else $row[] = l(t("[No photo]"),
191 $path."/node/$privacy/$cn/view", array(), NULL, NULL, FALSE, TRUE);
192 ;
193 }
194
195 //CN gets the e-mail link and the URL under it
196 $newcolumn = "<b>$cn</b><br /><br />";
197 if (array_key_exists('mail', $info)) {
198 $mail = $info['mail'][0];
199 $newcolumn .= '<a href="mailto:'.$mail.'">'.
200 theme_image($imgdir.'/mail.png', t("e-mail to $cn"), t("e-mail to $cn"),
201 array("style" => " vertical-align:middle")).$mail.'</a>';
202 }
203 if (array_key_exists('labeleduri', $info)) {
204 $url = $info['labeleduri'][0];
205 $newcolumn .= '<br /><a href="'.$url.'">'.
206 theme_image($imgdir.'/home.png', t("visit $cn's homepage"),
207 t("visit $cn's homepage"),
208 array("style" => " vertical-align:middle")).$url.'</a>';
209 }
210 $row[] = $newcolumn;
211
212 //Phone numbers
213 $newcolumn = '';
214 if (array_key_exists('telephonenumber', $info))
215 $newcolumn .= '<b>'.t('Main').'</b>:'.$info['telephonenumber'][0];
216 if (array_key_exists('homephone', $info))
217 $newcolumn .= '<br />'.
218 theme_image($imgdir.'/home.png', t("visit $cn's homepage"),
219 t("visit $cn's homepage"),
220 array("style" => " vertical-align:middle")).
221 $info['homephone'][0];
222 if (array_key_exists('mobile', $info))
223 $newcolumn .= '<br /><b>'._lat('mobile').':</b>'.$info['mobile'][0];
224 $row[] = $newcolumn;
225
226 $exclude = array('cn', 'jpegphoto', 'mail', 'labeleduri',
227 'telephonenumber', 'homephone', 'mobile');
228 foreach ($summary as $field) {
229 if (in_array($field, $exclude)) continue;
230 $value = $info[$field][0];
231 if ($field == 'mail')
232 $row[] = "<a href=\"mailto:$value\">$value</a>";
233 else if ($field == 'labeleduri')
234 $row[] = "<a href=\"$value\">$value</a>";
235 else
236 $row[] = $value;
237 }
238 return $row;
239 }
240
241 /**
242 * This function can output a Drupal themed table that contains many
243 * addressbook contacts in compact format.
244 *
245 * @param $info The entries from the LDAP server
246 * @param $fields The fields I'm supposed to work with
247 * @param $path The path to the module
248 * @param $tmp_path The temporary directory where I should be able to create
249 * temporary files.
250 * @param $edit TRUE if the user has edit permissions (this will make me
251 * generate links for the edit and delete panes.
252 * @param $privacy The privacy keyword to fill in to the links (either
253 * 'global', for global addressbook entries or 'private').
254 *
255 * @param $info An LDAP entry for which you want to create a compact view.
256 */
257 function _lab_multi_view($info, $summary, $path, $tmp_path, $edit, $privacy) {
258 $table_head = array();
259 $table_row = array();
260 $table_head[] = '';
261 //jpeg photo comes first
262 if (in_array('jpegphoto', $summary)) $table_head[] = _lat('jpegphoto');
263
264 $table_head[] = _lat('cn');
265
266 if (in_array('telephonenumber', $summary) ||
267 in_array('homephone', $summary) ||
268 in_array('mobile', $summary));
269 $table_head[] = _lat('telephonenumber');
270
271 $exclude = array('cn', 'jpegphoto', 'mail', 'labeleduri',
272 'telephonenumber', 'homephone', 'mobile');
273
274 foreach ($summary as $field) {
275 if (in_array($field, $exclude)) continue;
276 $table_head[] = l(_lat($field), $path."/search/$privacy/$what/$field");
277 }
278 for ($i=0; $i<count($info); $i++)
279 $table_row[] = _lab_compact_view($info[$i], $summary, $path, $tmp_path,
280 $edit, $privacy);
281 $table_attrs = array("width" => "100%");
282 return theme_table($table_head, $table_row, $table_attrs);
283 }
284
285 /**
286 * This function can output a Drupal themed table that contains 1 addressbook
287 * contact in extended form.
288 *
289 * @param $info The entry from the LDAP server
290 * @param $fields The fields I'm supposed to work with
291 * @param $path The path to the module
292 * @param $tmp_path The temporary directory where I should be able to create
293 * temporary files.
294 *
295 * @param $info An LDAP entry for which you want to create a compact view.
296 */
297 function _lab_view($info, $fields, $path, $tmp_path) {
298 $table_head = array();
299 $table_row = array();
300 $cn = $info['cn'][0];
301 if(array_key_exists('jpegphoto', $info)) {
302 $name_entry = array('data' => '<h2>'.$cn.'</h2>');
303 $imagefilename =
304 _lab_rescale($info['jpegphoto'][0], 100, $tmp_path, $path);
305 if (!$imagefilename) {
306 drupal_set_message(t("JPEG image for contact '$cn' seems corrupted. Fix it whenever possible!"));
307 $image_entry = '[Corrupted image]';
308 }
309 else {
310 $image_entry = theme_image($imagefilename, '['.$cn.'\'s photo]',
311 '['.$cn.'\'s photo]');
312 }
313 $table_row[0] = array($image_entry, $name_entry);
314 }
315 else {
316 $name_entry = array('data' => '<h2>'.$cn.'</h2>',
317 'colspan' => 2);
318 $table_row[0] = array($name_entry);
319 }
320
321 $special = array('jpegphoto', 'cn');
322 $i = 1;
323 foreach ($fields as $field) {
324 $lfield = strtolower($field);
325 if (in_array($field, $special)) continue;
326 $value = '';
327 if (array_key_exists($field, $info))
328 $value = $info[$field][0];
329 if (array_key_exists($lfield, $info))
330 $value = $info[$lfield][0];
331 if ($lfield == 'mail' && !empty($value))
332 $value = "<a href=\"mailto:$value\">$value</a>";
333 else if ($lfield == 'labeleduri' && !empty($value))
334 $value = "<a href=\"$value\">$value</a>";
335 if (!empty($value)) {
336 $value_entry = array('data' => $value);
337 $table_row[$i] = array('<b>'._lat($field).':</b>', $value_entry);
338 $i++;
339 }
340 }
341 //$table_attrs = array("width" => "100%");
342 return theme_table($table_head, $table_row, $table_attrs);
343 }
344
345 ?>

  ViewVC Help
Powered by ViewVC 1.1.2