Typo Fix
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourFour.php
index bcad6fcf03c1d071d6f0ae55d55b6b761c5719e6..0a887b1258789ee0297f28e9553bf9a06d02a5c3 100644 (file)
@@ -359,36 +359,42 @@ ALTER TABLE civicrm_dashboard
    *
    * @return bool
    */
+  function upgrade_4_4_7($rev, $originalVer, $latestVer) {
+    // For WordPress/Joomla(?), cleanup broken image_URL from 4.4.6 upgrades - https://issues.civicrm.org/jira/browse/CRM-14971
+    $exBackendUrl = CRM_Utils_System::url('civicrm/contact/imagefile', 'photo=XXX', TRUE); // URL formula from 4.4.6 upgrade
+    $exFrontendUrl = CRM_Utils_System::url('civicrm/contact/imagefile', 'photo=XXX', TRUE, NULL, TRUE, TRUE);
+    if ($originalVer == '4.4.6' && $exBackendUrl != $exFrontendUrl) {
+      $minId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_contact WHERE image_URL IS NOT NULL');
+      $maxId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_contact WHERE image_URL IS NOT NULL');
+      for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) {
+        $endId = $startId + self::BATCH_SIZE - 1;
+        $title = ts('Upgrade image_urls (%1 => %2)', array(1 => $startId, 2 => $endId));
+        $this->addTask($title, 'cleanupBackendImageUrls', $startId, $endId);
+      }
+    }
+    $this->addTask(ts('Update saved search information'), 'changeSavedSearch');
+  }
+
   static function upgradeImageUrls(CRM_Queue_TaskContext $ctx, $startId, $endId){
-    $sql = "
-SELECT id, image_url
-FROM civicrm_contact
-WHERE 1
-AND id BETWEEN %1 AND %2
-AND image_URL IS NOT NULL
-";
-    $params = array(
-      1 => array($startId, 'Integer'),
-      2 => array($endId, 'Integer'),
-    );
-    $dao = CRM_Core_DAO::executeQuery($sql, $params, TRUE, NULL, FALSE, FALSE);
+    $dao = self::findContactImageUrls($startId, $endId);
     $failures = array();
+    $config = CRM_Core_Config::singleton();
     while ($dao->fetch()){
       $imageURL = $dao->image_url;
       $baseurl = CIVICRM_UF_BASEURL;
+      //CRM-15897 - gross hack for joomla to remove the administrator/
+      if ($config->userFramework == 'Joomla') {
+        $baseurl = str_replace("/administrator/", "/", $baseurl);
+      }
       $baselen = strlen($baseurl);
       if (substr($imageURL, 0, $baselen)==$baseurl){
         $photo = basename($dao->image_url);
-        $config = CRM_Core_Config::singleton();
         $fullpath = $config->customFileUploadDir.$photo;
           if (file_exists($fullpath)){
-            $newimageurl =  CRM_Utils_System::url('civicrm/contact/imagefile', 'photo='.$photo, TRUE);
-            $sql = 'UPDATE civicrm_contact SET image_url=%1 WHERE id=%2';
-            $params = array(
-              1 => array($newimageurl, 'String'),
-              2 => array($dao->id, 'Integer'),
-            );
-            $updatedao = CRM_Core_DAO::executeQuery($sql, $params);
+            // For anyone who upgraded 4.4.6 release (eg 4.4.0=>4.4.6), the $newImageUrl incorrectly used backend URLs.
+            // For anyone who skipped 4.4.6 (eg 4.4.0=>4.4.7), the $newImageUrl correctly uses frontend URLs
+            self::setContactImageUrl($dao->id,
+              CRM_Utils_System::url('civicrm/contact/imagefile', 'photo='.$photo, TRUE, NULL, TRUE, TRUE));
           }
           else{
             $failures[$dao->id] = $dao->image_url;
@@ -402,6 +408,112 @@ AND image_URL IS NOT NULL
     return TRUE;
   }
 
+  static function changeSavedSearch(CRM_Queue_TaskContext $ctx) {
+    $membershipStatuses = array_flip(CRM_Member_PseudoConstant::membershipStatus());
+
+    $dao = new CRM_Contact_DAO_SavedSearch();
+    $dao->find();
+    while ($dao->fetch()) {
+      $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($dao->id);
+      if (!empty($formValues['mapper'])) {
+        foreach ($formValues['mapper'] as $key => $value) {
+          foreach ($value as $k => $v) {
+            if ($v[0] == 'Membership' && in_array($v[1], array('membership_status', 'membership_status_id'))) {
+              $value = $formValues['value'][$key][$k];
+              $op = $formValues['operator'][$key][$k];
+              if ($op == 'IN') {
+                $value = trim($value);
+                $value = str_replace('(', '', $value);
+                $value = str_replace(')', '', $value);
+
+                $v = explode(',', $value);
+                $value = array();
+                foreach ($v as $k1 => $v2) {
+                  if (is_numeric($v2)) {
+                    break 2;
+                  }
+                  $value[$k1] = $membershipStatuses[$v2];
+                }
+                $formValues['value'][$key][$k] = "(" . implode(',', $value) . ")";
+              }
+              elseif (in_array($op, array('=', '!='))) {
+                if (is_numeric($value)) {
+                  break;
+                }
+                $formValues['value'][$key][$k] = $membershipStatuses[$value];
+              }
+            }
+          }
+        }
+        $dao->form_values = serialize($formValues);
+        $dao->save();
+      }
+    }
+
+    return TRUE;
+  }
+
+  /**
+   * For WordPress/Joomla(?) sites which upgraded to 4.4.6, find back-end image_URLs
+   * (e.g. "http://example.com/wp-admin/admin.php?page=CiviCRM&amp;q=civicrm/contact/imagefile&amp;photo=123.jpg")
+   * and convert them to front-end URLs
+   * (e.g. "http://example.com/?page=CiviCRM&amp;q=civicrm/contact/imagefile&amp;photo=123.jpg").
+   *
+   * @param CRM_Queue_TaskContext $ctx
+   * @param $startId
+   * @param $endId
+   * @return bool
+   */
+  static function cleanupBackendImageUrls(CRM_Queue_TaskContext $ctx, $startId, $endId) {
+    $dao = self::findContactImageUrls($startId, $endId);
+    while ($dao->fetch()) {
+      $imageUrl = str_replace('&amp;', '&', $dao->image_url);
+      if (preg_match(":civicrm/contact/imagefile.*photo=:", $imageUrl)) {
+        // looks like one of ours
+        $imageUrlParts = parse_url($imageUrl);
+        parse_str($imageUrlParts['query'], $imageUrlQuery);
+        self::setContactImageUrl($dao->id,
+          CRM_Utils_System::url('civicrm/contact/imagefile', 'photo='.$imageUrlQuery['photo'], TRUE, NULL, TRUE, TRUE));
+      }
+    }
+    return TRUE;
+  }
+
+  /**
+   * @param int $startId
+   * @param int $endId
+   * @return CRM_Core_DAO columns include "id" and "image_URL"
+   */
+  public static function findContactImageUrls($startId, $endId) {
+    $sql = "
+SELECT id, image_url
+FROM civicrm_contact
+WHERE 1
+AND id BETWEEN %1 AND %2
+AND image_URL IS NOT NULL
+";
+
+    $params = array(
+      1 => array($startId, 'Integer'),
+      2 => array($endId, 'Integer'),
+    );
+    $dao = CRM_Core_DAO::executeQuery($sql, $params, TRUE, NULL, FALSE, FALSE);
+    return $dao;
+  }
+
+  /**
+   * @param int $cid
+   * @param string $newImageUrl
+   */
+  public static function setContactImageUrl($cid, $newImageUrl) {
+    $sql = 'UPDATE civicrm_contact SET image_url=%1 WHERE id=%2';
+    $params = array(
+      1 => array($newImageUrl, 'String'),
+      2 => array($cid, 'Integer'),
+    );
+    $updatedao = CRM_Core_DAO::executeQuery($sql, $params);
+  }
+
   /**
    * Update activity contacts CRM-12274
    *
@@ -708,7 +820,7 @@ CREATE TABLE IF NOT EXISTS `civicrm_word_replacement` (
    * @return bool TRUE if $params is valid
    */
   static function isValidWordReplacement($params) {
-    $result = strlen($params['find_word']) <= self::MAX_WORD_REPLACEMENT_SIZE && strlen($params['replace_word']) > self::MAX_WORD_REPLACEMENT_SIZE;
+    $result = strlen($params['find_word']) <= self::MAX_WORD_REPLACEMENT_SIZE && strlen($params['replace_word']) <= self::MAX_WORD_REPLACEMENT_SIZE;
     if (!$result) {
       CRM_Core_Error::debug_var('invalidWordReplacement', $params);
     }