Fix spelling and capitalization of Contact ID and External ID
[civicrm-core.git] / CRM / Profile / Form / Edit.php
index 216bf8fc72f5d1cca12dd36de5ce8288143c0b8d..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)
+      ));
     }
   }
 
@@ -190,30 +194,6 @@ SELECT module
         );
       }
 
-      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);
-      }
-
       // 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);
@@ -236,10 +216,7 @@ SELECT module
     if (($this->_multiRecord & CRM_Core_Action::DELETE) && $this->_recordExists) {
       $this->_deleteButtonName = $this->getButtonName('upload', 'delete');
 
-      $this->addElement('submit',
-        $this->_deleteButtonName,
-        ts('Delete')
-      );
+      $this->addElement('submit', $this->_deleteButtonName, ts('Delete'));
 
       $buttons[] = array(
         'type' => 'cancel',
@@ -268,14 +245,12 @@ SELECT module
       'isDefault' => TRUE,
     );
 
-    if ($this->_context != 'dialog') {
-      $buttons[] = array(
-        'type' => 'cancel',
-        'name' => ts('Cancel'),
-        'isDefault' => TRUE,
-        'js' => array('onclick' => "location.href='{$this->_cancelURL}'; return false;"),
-      );
-    }
+    $buttons[] = array(
+      'type' => 'cancel',
+      'name' => ts('Cancel'),
+      'isDefault' => TRUE,
+      'js' => array('onclick' => "location.href='{$this->_cancelURL}'; return false;"),
+    );
 
     $this->addButtons($buttons);
 
@@ -292,23 +267,12 @@ SELECT module
   public function postProcess() {
     parent::postProcess();
 
-    // this is special case when we create contact using Dialog box
-    if ($this->_context == 'dialog') {
-      $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'display_name');
-      $sortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'sort_name');
-      $returnArray = array(
-        'contactID' => $this->_id,
-        'displayName' => $displayName,
-        'sortName' => $sortName,
-        'newContactSuccess' => TRUE,
-      );
-
-      echo json_encode($returnArray);
-      CRM_Utils_System::civiExit();
-    }
+    $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'display_name');
+    $sortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'sort_name');
+    $this->ajaxResponse['label'] = $sortName;
 
-    //for delete record handling
-    if (!CRM_Utils_Array::value($this->_deleteButtonName, $_POST)) {
+    // When saving (not deleting) and not in an ajax popup
+    if (empty($_POST[$this->_deleteButtonName]) && $this->_context != 'dialog') {
       CRM_Core_Session::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success');
     }
 
@@ -370,9 +334,7 @@ SELECT module
   function validate() {
     $errors = parent::validate();
 
-    if (!$errors &&
-      CRM_Utils_Array::value('errorURL', $_POST)
-    ) {
+    if (!$errors && !empty($_POST['errorURL'])) {
       $message = NULL;
       foreach ($this->_errors as $name => $mess) {
         $message .= $mess;