Modify ProfileFields retrieval API : no more iterators
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Mon, 22 Mar 2010 10:13:37 +0000 (11:13 +0100)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Mon, 22 Mar 2010 13:07:56 +0000 (14:07 +0100)
$profile->getAddresses now returns an array, use
$profile->iterAddresses for a PlIterator

Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
classes/profile.php
include/profilefields.inc.php
include/vcard.inc.php
modules/carnet/contacts.pdf.inc.php
templates/include/minifiche.tpl

index a09f3eb..f55b0a2 100644 (file)
@@ -376,18 +376,23 @@ class Profile
         }
 
         if ($this->addresses == null) {
-            return PlIteratorUtils::emptyIterator();
+            return array();
         }
         return $this->addresses->get($flags, $limit);
     }
 
+    public function iterAddresses($flags, $limit = null)
+    {
+        return PlIteratorUtils::fromArray($this->getAddresses($flags, $limit), 1, true);
+    }
+
     public function getMainAddress()
     {
-        $it = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN);
-        if ($it->total() == 0) {
+        $addr = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN);
+        if (count($addr) == 0) {
             return null;
         } else {
-            return $it->next();
+            return array_pop($addr);
         }
     }
 
@@ -407,7 +412,7 @@ class Profile
         }
 
         if ($this->phones == null) {
-            return PlIteratorUtils::emptyIterator();
+            return array();
         }
         return $this->phones->get($flags, $limit);
     }
@@ -427,7 +432,7 @@ class Profile
         }
 
         if ($this->educations == null) {
-            return PlIteratorUtils::emptyIterator();
+            return array();
         }
         return $this->educations->get($flags, $limit);
     }
@@ -467,7 +472,7 @@ class Profile
             $this->setNetworking($this->getProfileField('ProfileNetworking'));
         }
         if ($this->networks == null) {
-            return PlIteratorUtils::emptyIterator();
+            return array();
         }
         return $this->networks->get($flags, $limit);
     }
@@ -475,10 +480,10 @@ class Profile
     public function getWebSite()
     {
         $site = $this->getNetworking(self::NETWORKING_WEB, 1);
-        if ($site->total() != 1) {
+        if (count($site) != 1) {
             return null;
         }
-        $site = $site->next();
+        $site = array_pop($site);
         return $site['address'];
     }
 
@@ -499,7 +504,7 @@ class Profile
         }
 
         if ($this->jobs == null) {
-            return PlIteratorUtils::emptyIterator();
+            return array();
         }
         return $this->jobs->get($flags, $limit);
     }
@@ -507,10 +512,10 @@ class Profile
     public function getMainJob()
     {
         $job = $this->getJobs(self::JOBS_MAIN, 1);
-        if ($job->total() != 1) {
+        if (count($job) != 1) {
             return null;
         }
-        return $job->next();
+        return array_pop($job);
     }
 
     /* Binets
index b0b8490..0bb11ba 100644 (file)
@@ -348,7 +348,7 @@ class ProfileEducation extends ProfileField
                 break;
             }
         }
-        return PlIteratorUtils::fromArray($educations, 1, true);
+        return $educations;
     }
 
     public static function fetchData(array $pids, $visibility)
@@ -442,7 +442,7 @@ class ProfileNetworking extends ProfileField
                 break;
             }
         }
-        return PlIteratorUtils::fromArray($nws, 1, true);
+        return $nws;
     }
 }
 // }}}
@@ -556,7 +556,7 @@ class ProfileAddresses extends ProfileField
                 break;
             }
         }
-        return PlIteratorUtils::fromArray($res, 1, true);
+        return $res;
     }
 
     public static function fetchData(array $pids, $visibility)
@@ -581,7 +581,7 @@ class ProfileAddresses extends ProfileField
     public function addPhones(ProfilePhones $phones)
     {
         $p = $phones->get(0);
-        while ($phone = $p->next()) {
+        foreach ($p as $phone) {
             if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) {
                 $this->addresses[$phone->link_id]->addPhone($phone);
             }
@@ -613,7 +613,7 @@ class ProfilePhones extends ProfileField
                 break;
             }
         }
-        return PlIteratorUtils::fromArray($phones, 1, true);
+        return $phones;
     }
 
     public static function fetchData(array $pids, $visibility)
@@ -669,13 +669,13 @@ class ProfileJobs extends ProfileField
                 break;
             }
         }
-        return PlIteratorUtils::fromArray($jobs, 1, true);
+        return $jobs;
     }
 
     public function addPhones(ProfilePhones $phones)
     {
         $p = $phones->get(0);
-        while ($phone = $p->next()) {
+        foreach ($p as $phone) {
             if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
                 $this->jobs[$phone->link_id]->addPhones($phone);
             }
@@ -685,7 +685,7 @@ class ProfileJobs extends ProfileField
     public static function addAddresses(ProfileAddresses $addresses)
     {
         $a = $addresses->get(Profile::ADDRESS_PRO);
-        while ($address = $a->next()) {
+        foreach ($a as $address) {
             if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) {
                 $this->jobs[$address->link_id]->setAddress($address);
             }
index 9cbd7c0..2a1402a 100644 (file)
@@ -83,7 +83,7 @@ class VCard extends PlVCard
         }
 
         // Homes
-        $adrs = $pf->getAddresses(Profile::ADDRESS_PERSO);
+        $adrs = $pf->iterAddresses(Profile::ADDRESS_PERSO);
         while ($adr = $adrs->next()) {
             // TODO : find a way to fetch only the "street" part of the address
             $group = $entry->addHome($adr['text'], null, null, $adr['postalCode'],
@@ -98,7 +98,7 @@ class VCard extends PlVCard
         }
 
         // Pro
-        $adrs = $pf->getAddresses(Profile::ADDRESS_PRO);
+        $adrs = $pf->iterAddresses(Profile::ADDRESS_PRO);
         while ($adr = $adrs->next()) {
             // TODO : link address to company
             $group = $entry->addWork(null, null, null, null,
index ff5aff9..175cbc7 100644 (file)
@@ -315,7 +315,7 @@ class ContactsPDF extends FPDF
             $self->TableRow('mobile', utf8_decode($profile->mobile), 'Mono');
         }
 
-        $it = $profile->getAddresses(Profile::ADDRESS_ALL);
+        $it = $profile->iterAddresses(Profile::ADDRESS_ALL);
         while ($a = $it->next()) {
             foreach ($a as &$value) {
                 $value = utf8_decode($value);
@@ -323,7 +323,7 @@ class ContactsPDF extends FPDF
             $self->Space();
             $self->Address($a);
         }
-        $it = $profile->getAddresses(Profile::ADDRESS_PRO);
+        $it = $profile->iterAddresses(Profile::ADDRESS_PRO);
         while ($a = $it->next()) {
             foreach ($a as &$value) {
                 $value = utf8_decode($value);
index c4f0dfa..33add8f 100644 (file)
@@ -64,7 +64,7 @@
       <img src='images/flags/{$nat}.gif' alt='{$nat}' height='11' title='{$nat}' />&nbsp;
       {/foreach}
       {$profile->promo()}{*
-      *}{iterate from=$profile->getExtraEducations(4) item=edu}, {display_education edu=$edu profile=$profile}{/iterate}{*
+      *}{foreach from=$profile->getExtraEducations(4) item=edu}, {display_education edu=$edu profile=$profile}{/foreach}{*
       *}{if $dead}, {"décédé"|sex:"décédée":$profile} le {$profile->deathdate|date_format}{/if}
     </div>
   </div>