Fix spelling and capitalization of Contact ID and External ID
[civicrm-core.git] / CRM / Profile / Form / Edit.php
index 3f47c8c93f8549ae15e5874ccfeed8e9683f7d28..4885617df1a861909d39bf941c44ad800d468abf 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -113,26 +113,30 @@ class CRM_Profile_Form_Edit extends CRM_Profile_Form {
 
     parent::preProcess();
 
-    // make sure the gid is set and valid
-    if (!$this->_gid) {
-      CRM_Core_Error::fatal(ts('The requested Profile (gid=%1) is disabled, OR there is no Profile with that ID, OR a valid \'gid=\' integer value is missing from the URL. Contact the site administrator if you need assistance.',
-          array(1 => $this->_gid)
-        ));
-    }
-
     // and also the profile is of type 'Profile'
     $query = "
-SELECT module
-  FROM civicrm_uf_join
- WHERE module = 'Profile'
-   AND uf_group_id = %1
+SELECT module,is_reserved
+  FROM civicrm_uf_group
+  LEFT JOIN civicrm_uf_join ON uf_group_id = civicrm_uf_group.id
+  WHERE civicrm_uf_group.id = %1
 ";
+
     $params = array(1 => array($this->_gid, 'Integer'));
     $dao = CRM_Core_DAO::executeQuery($query, $params);
-    if (!$dao->fetch()) {
+
+    $isProfile = FALSE;
+    while ($dao->fetch()) {
+      $isProfile = ($isProfile || ($dao->module == "Profile"));
+    }
+
+    //Check that the user has the "add contacts" Permission
+    $canAdd = CRM_Core_Permission::check("add contacts");
+
+    //Remove need for Profile module type when using reserved profiles [CRM-14488]
+    if (!$dao->N || (!$isProfile && !($dao->is_reserved && $canAdd))) {
       CRM_Core_Error::fatal(ts('The requested Profile (gid=%1) is not configured to be used for \'Profile\' edit and view forms in its Settings. Contact the site administrator if you need assistance.',
-          array(1 => $this->_gid)
-        ));
+        array(1 => $this->_gid)
+      ));
     }
   }
 
@@ -143,11 +147,7 @@ SELECT module
    * @access public
    */
   public function buildQuickForm() {
-    // add the hidden field to redirect the postProcess from
-    $ufGroup = new CRM_Core_DAO_UFGroup();
-
-    $ufGroup->id = $this->_gid;
-    if (!$ufGroup->find(TRUE)) {
+    if (empty($this->_ufGroup['id'])) {
       CRM_Core_Error::fatal();
     }
 
@@ -157,24 +157,20 @@ SELECT module
         'Edit ' . $this->_customGroupTitle . ' Record' : $this->_customGroupTitle;
 
     } else {
-      $groupTitle = $ufGroup->title;
+      $groupTitle = $this->_ufGroup['title'];
     }
     CRM_Utils_System::setTitle($groupTitle);
     $this->assign('recentlyViewed', FALSE);
 
     if ($this->_context != 'dialog') {
-      $this->_postURL = CRM_Utils_Array::value('postURL', $_POST);
-      $this->_cancelURL = CRM_Utils_Array::value('cancelURL', $_POST);
+      $this->_postURL = $this->_ufGroup['post_URL'];
+      $this->_cancelURL = $this->_ufGroup['cancel_URL'];
 
       $gidString = $this->_gid;
       if (!empty($this->_profileIds)) {
         $gidString = implode(',', $this->_profileIds);
       }
 
-      if (!$this->_postURL) {
-        $this->_postURL = $ufGroup->post_URL;
-      }
-
       if (!$this->_postURL) {
         if ($this->_context == 'Search') {
           $this->_postURL = CRM_Utils_System::url('civicrm/contact/search');
@@ -193,49 +189,15 @@ SELECT module
       }
 
       if (!$this->_cancelURL) {
-        if ($ufGroup->cancel_URL) {
-          $this->_cancelURL = $ufGroup->cancel_URL;
-        }
-        else {
-          $this->_cancelURL = CRM_Utils_System::url('civicrm/profile',
-            "reset=1&gid={$gidString}"
-          );
-        }
-      }
-
-      if ($this->_multiRecordProfile) {
-        $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
-
-        // get checksum if present
-        if ($this->get('cs')) {
-          $urlParams .= "&cs=" . $this->get('cs');
-        }
-        $this->_postURL = CRM_Utils_System::url('civicrm/profile/edit', $urlParams);
-        $this->_cancelURL = CRM_Utils_System::url('civicrm/profile/edit', $urlParams);
-
-        //passing the post url to template so the popup form does
-        //proper redirection and proccess form errors if any
-        if (!isset($this->_onPopupClose) || $this->_onPopupClose == 'redirectToProfile') {
-          $popupRedirect = CRM_Utils_System::url('civicrm/profile/edit', $urlParams, FALSE, NULL, FALSE);
-        }
-        elseif ($this->_onPopupClose == 'redirectToTab') {
-          $popupRedirect = CRM_Utils_System::url('civicrm/contact/view',
-            "reset=1&cid={$this->_id}&selectedChild=custom_{$this->_customGroupId}", FALSE, NULL, FALSE);
-        }
-
-        $this->assign('urlParams', $urlParams);
-        $this->assign('postUrl', $popupRedirect);
+        $this->_cancelURL = CRM_Utils_System::url('civicrm/profile',
+          "reset=1&gid={$gidString}"
+        );
       }
 
       // we do this gross hack since qf also does entity replacement
       $this->_postURL = str_replace('&amp;', '&', $this->_postURL);
       $this->_cancelURL = str_replace('&amp;', '&', $this->_cancelURL);
 
-      $this->addElement('hidden', 'postURL', $this->_postURL);
-      if ($this->_cancelURL) {
-        $this->addElement('hidden', 'cancelURL', $this->_cancelURL);
-      }
-
       // also retain error URL if set
       $this->_errorURL = CRM_Utils_Array::value('errorURL', $_POST);
       if ($this->_errorURL) {
@@ -260,6 +222,7 @@ SELECT module
         'type' => 'cancel',
         'name' => ts('Cancel'),
         'isDefault' => TRUE,
+        'js' => array('onclick' => "location.href='{$this->_cancelURL}'; return false;"),
       );
       $this->addButtons($buttons);
       return;
@@ -286,6 +249,7 @@ SELECT module
       'type' => 'cancel',
       'name' => ts('Cancel'),
       'isDefault' => TRUE,
+      'js' => array('onclick' => "location.href='{$this->_cancelURL}'; return false;"),
     );
 
     $this->addButtons($buttons);