From: x2003bruneau Date: Wed, 28 Feb 2007 15:07:52 +0000 (+0000) Subject: Should close #611: Support of big (> 1000) vcards X-Git-Tag: xorg/0.9.14~250 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=29b12e6ef20d42ac06842bf65fb47277bcc91a4d;hp=a582725ed8b8197e7fa55c119768cd9812d59603;p=platal.git Should close #611: Support of big (> 1000) vcards git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1530 839d8a87-29fc-0310-9880-83ba4fa771e5 --- diff --git a/include/vcard.inc.php b/include/vcard.inc.php index 01ec339..bec45bb 100644 --- a/include/vcard.inc.php +++ b/include/vcard.inc.php @@ -22,69 +22,57 @@ require_once('xorg.misc.inc.php'); require_once('user.func.inc.php'); -class VCard +class VCardIterator { - var $users = array(); - var $photos; + private $user_list = array(); + private $count = 0; + private $freetext = null; + private $photos = true; - function VCard($users, $photos = true, $freetext = null) + public function __construct($photos, $freetext) { - $this->photos = $photos; - if (is_array($users)) { - foreach ($users as $user) { - $this->add_user($user, $freetext); - } - } else { - $this->add_user($users, $freetext); - } + $this->freetext = $freetext; + $this->photos = $photos; } - function escape($text) + public function add_user($user) { - return preg_replace('/[,;]/', '\\\\$0', $text); + $this->user_list[] = get_user_forlife($user); + $this->count++; } - function format_adr($params, &$smarty) + public function first() { - // $adr1, $adr2, $adr3, $postcode, $city, $region, $country - extract($params['adr']); - $adr = trim($adr1); - $adr = trim("$adr\n$adr2"); - $adr = trim("$adr\n$adr3"); - return $this->text_encode(';;' - . $this->escape($adr) . ';' - . $this->escape($city) . ';' - . $this->escape($region) . ';' - . $this->escape($postcode) . ';' - . $this->escape($country), false); + return count($this->user_list) == $this->count - 1; } - function text_encode($text, $escape = true) + public function last() { - if (is_array($text)) { - return implode(',', array_map(array($this, 'text_encode'), $text)); - } - if ($escape) { - $text = $this->escape($text); - } - return preg_replace("/(\r\n|\n|\r)/", '\n', $text); + return count($this->user_list) == 0; } - function add_user($x, $freetext) + public function total() { - global $globals; + return $this->count; + } - $login = get_user_forlife($x); + public function next() + { + if (!$this->user_list) { + return null; + } + global $globals; + $login = array_shift($this->user_list); $user = get_user_details($login); if (strlen(trim($user['freetext']))) { - $user['freetext'] = html_entity_decode($user['freetext']); + $user['freetext'] = pl_entity_decode($user['freetext']); } - if (!is_null($freetext)) { + if ($this->freetext) { if (strlen(trim($user['freetext']))) { - $user['freetext'] = $freetext . "\n" . $user['freetext']; + $user['freetext'] = $this->freetext . "\n" . $user['freetext']; } else { - $user['freetext'] = $freetext; + $user['freetext'] = $this->freetext; } } @@ -99,10 +87,10 @@ class VCard S::v('uid'), $user['forlife'].'@'.$globals->mail->domain, $user['forlife'].'@'.$globals->mail->domain2); - + $user['virtualalias'] = $res->fetchOneCell(); - $user['gpxs_vcardjoin'] = join(',', array_map(array($this, 'text_encode'), $user['gpxs_name'])); - $user['binets_vcardjoin'] = join(',', array_map(array($this, 'text_encode'), $user['binets'])); + $user['gpxs_vcardjoin'] = join(',', array_map(array('VCard', 'text_encode'), $user['gpxs_name'])); + $user['binets_vcardjoin'] = join(',', array_map(array('VCard', 'text_encode'), $user['binets'])); // get photo if ($this->photos) { $res = XDB::query( @@ -114,21 +102,69 @@ class VCard $user['photo'] = $res->fetchOneAssoc(); } } - $this->users[] = $user; + return $user; + } +} + +class VCard +{ + private $iterator = null; + + public function __construct($users, $photos = true, $freetext = null) + { + $this->iterator = new VCardIterator($photos, $freetext); + if (is_array($users)) { + foreach ($users as $user) { + $this->iterator->add_user($user); + } + } else { + $this->iterator->add_user($users); + } + } + + public static function escape($text) + { + return preg_replace('/[,;]/', '\\\\$0', $text); + } + + public static function format_adr($params, &$smarty) + { + // $adr1, $adr2, $adr3, $postcode, $city, $region, $country + extract($params['adr']); + $adr = trim($adr1); + $adr = trim("$adr\n$adr2"); + $adr = trim("$adr\n$adr3"); + return VCard::text_encode(';;' + . VCard::escape($adr) . ';' + . VCard::escape($city) . ';' + . VCard::escape($region) . ';' + . VCard::escape($postcode) . ';' + . VCard::escape($country), false); + } + + public static function text_encode($text, $escape = true) + { + if (is_array($text)) { + return implode(',', array_map(array('VCard', 'text_encode'), $text)); + } + if ($escape) { + $text = VCard::escape($text); + } + return preg_replace("/(\r\n|\n|\r)/", '\n', $text); } - function do_page(&$page) + public function do_page(&$page) { $page->changeTpl('core/vcard.tpl', NO_SKIN); $page->register_modifier('vcard_enc', array($this, 'text_encode')); $page->register_function('format_adr', array($this, 'format_adr')); - $page->assign_by_ref('users', $this->users); + $page->assign_by_ref('users', $this->iterator); header("Pragma: "); header("Cache-Control: "); header("Content-type: text/x-vcard; charset=UTF-8"); header("Content-Transfer-Encoding: 8bit"); - } + } } // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: diff --git a/templates/core/vcard.tpl b/templates/core/vcard.tpl index 378e540..7fbcbe9 100644 --- a/templates/core/vcard.tpl +++ b/templates/core/vcard.tpl @@ -19,7 +19,7 @@ {* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *} {* *} {**************************************************************************} -{foreach from="$users" item=vcard} +{iterate from=$users item=vcard} BEGIN:VCARD VERSION:3.0 {if $vcard.nom_usage} @@ -87,10 +87,10 @@ X-BINETS:{$vcard.binets_vcardjoin} X-GROUPS:{$vcard.gpxs_vcardjoin} {/if} {if $vcard.photo} -PHOTO;ENCODING=b;TYPE={$vcard.photo.attachmime}:{$vcard.photo.attach|base64_encode} +PHOTO;ENCODING=b;TYPE={$vcard.photo.attachmime}:{$vcard.photo.attach|base64_encode|vcard_enc} {/if} SORT-STRING:{$vcard.nom|vcard_enc} REV:{$vcard.date|date_format:"%Y%m%dT000000Z"} END:VCARD{"\n"} -{/foreach} +{/iterate} {* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}