Delete profile_addresses_components rows where deleting profile_addresses ones
[platal.git] / classes / address.php
index 2ebe8b5..62e7350 100644 (file)
@@ -650,7 +650,7 @@ class Address
     {
         $address = $this->text;
         if ($this->type == self::LINK_PROFILE || $this->type == self::LINK_JOB) {
-            static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privĂ©');
+            static $pubs = array('public' => 'publique', 'ax' => 'annuaire papier', 'private' => 'privĂ©', 'hidden' => 'administrateurs');
             $address .= ' (affichage ' . $pubs[$this->pub];
         }
         if ($this->type == self::LINK_PROFILE) {
@@ -704,6 +704,11 @@ class Address
                          $this->types, $this->formatted_address, $this->location_type, $this->partial_match, $this->latitude, $this->longitude,
                          $this->southwest_latitude, $this->southwest_longitude, $this->northeast_latitude, $this->northeast_longitude, $this->geocoding_calls, $this->postal_code_fr);
 
+            // In an ideal world there would not be any components to clean up before insertion.
+            // In real world, it happens that some addresses are badly removed and this query removes the leaked trash.
+            XDB::execute('DELETE FROM  profile_addresses_components
+                                WHERE  pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?} AND id = {?}',
+                         $this->pid, $this->jobid, $this->groupid, $this->type, $this->id);
             if ($this->componentsIds) {
                 foreach (explode(',', $this->componentsIds) as $component_id) {
                     XDB::execute('INSERT IGNORE INTO  profile_addresses_components (pid, jobid, groupid, type, id, component_id)
@@ -776,7 +781,7 @@ class Address
         return $best_mail;
     }
 
-    public function updateGeocoding($text)
+    public function updateGeocoding()
     {
         XDB::execute('UPDATE  profile_addresses
                          SET  text = {?}, postalText = {?}, types = {?}, formatted_address = {?},
@@ -817,16 +822,38 @@ class Address
     {
         $where = '';
         if (!is_null($pid)) {
-            $where = XDB::format(' AND pid = {?}', $pid);
+            $where .= XDB::format(' AND pid = {?}', $pid);
         }
         if (!is_null($jobid)) {
-            $where = XDB::format(' AND jobid = {?}', $jobid);
+            $where .= XDB::format(' AND jobid = {?}', $jobid);
         }
         if (!is_null($groupid)) {
-            $where = XDB::format(' AND groupid = {?}', $groupid);
+            $where .= XDB::format(' AND groupid = {?}', $groupid);
+        }
+        // Prevent buggy callers from accidentally dropping profile_addresses
+        if (!$where) {
+            throw new Exception("Unable to delete all addresses of the given type, the request was too generic");
+        }
+        $where_pub = $where . (($deletePrivate) ? '' : ' AND pub IN (\'public\', \'ax\')');
+
+        if ($deletePrivate) {
+            XDB::execute('DELETE FROM  profile_addresses_components
+                                WHERE  type = {?}' . $where,
+                         $type);
+        } else {
+            // Delete address components one by one if $deletePrivate is not requested
+            $res = XDB::iterator('SELECT  pid, jobid, groupid, type, id
+                                    FROM  profile_addresses
+                                   WHERE  type = {?}' . $where_pub,
+                                 $type);
+            while (($values = $res->next())) {
+                XDB::execute('DELETE FROM  profile_addresses_components
+                                    WHERE  pid = {?} AND jobid = {?} AND groupid = {?} AND type = {?} AND id = {?}',
+                             $values['pid'], $values['jobid'], $values['groupid'], $values['type'], $values['id']);
+            }
         }
         XDB::execute('DELETE FROM  profile_addresses
-                            WHERE  type = {?}' . $where . (($deletePrivate) ? '' : ' AND pub IN (\'public\', \'ax\')'),
+                            WHERE  type = {?}' . $where_pub,
                      $type);
         if ($type == self::LINK_PROFILE) {
             Phone::deletePhones($pid, Phone::LINK_ADDRESS, null, $deletePrivate);