Example #1
0
 /**
  * set the location of a member
  */
 public function setLocation($IdMember, $geonameid = false)
 {
     // Address IdCity address must only consider Populated palces (definition of cities), it also must consider the address checking process
     $Rank = 0;
     // Rank=0 means the main address, todo when we will deal with several addresses we will need to consider the other rank Values ;
     $IdMember = (int) $IdMember;
     $geonameid = (int) $geonameid;
     $errors = array();
     if (empty($IdMember)) {
         // name is not set:
         $errors['Name'] = 'Name not set';
     }
     if (empty($geonameid)) {
         // name is not set:
         $errors['Geonameid'] = 'Geoname not set';
     }
     // get Member's current Location
     $result = $this->singleLookup("\nSELECT  a.IdCity\nFROM    addresses AS a\nWHERE   a.IdMember = '{$IdMember}'\n    AND a.rank = 0\n            ");
     if (!isset($result) || $result->IdCity != $geonameid) {
         // Check Geo and maybe add location
         $geomodel = new GeoModel();
         if (!$geomodel->getDataById($geonameid)) {
             // if the geonameid is not in our DB, let's add it
             if (!$geomodel->addGeonameId($geonameid, 'member_primary')) {
                 $vars['errors'] = array('geoinserterror');
                 return false;
             }
         } else {
             // the geonameid is in our DB, so just update the counters
             //get id for usagetype:
             $usagetypeId = $geomodel->getUsagetypeId('member_primary')->id;
             $update = $geomodel->updateUsageCounter($geonameid, $usagetypeId, 'add');
         }
         $result = $this->singleLookup("\nUPDATE  addresses\nSET     IdCity = {$geonameid}\nWHERE   IdMember = {$IdMember} and Rank=" . $Rank);
         // name is not set:
         if (!empty($result)) {
             $errors['Geonameid'] = 'Geoname not set';
         }
         $result = $this->singleLookup("\nUPDATE  members\nSET     IdCity = {$geonameid}\nWHERE   id = {$IdMember}\n                ");
         if (!empty($result)) {
             $errors['Geonameid'] = 'Member IdCity not set';
         } else {
             $this->logWrite("The Member with the Id: " . $IdMember . " changed his location to Geo-Id: " . $geonameid, "Members");
         }
         if (empty($errors) && ($m = $this->createEntity('Member')->findById($IdMember))) {
             // if a member with status NeedMore updates her/his profile, moving them back to pending
             if ($m->Status == 'NeedMore') {
                 $m->Status = 'Pending';
                 $m->update();
             }
         }
         return array('errors' => $errors, 'IdMember' => $result);
     } else {
         // geonameid hasn't changed
         return false;
     }
 }