CRM-17154 fix delete function.
authoreileen <emcnaughton@wikimedia.org>
Wed, 22 Jun 2016 01:33:11 +0000 (13:33 +1200)
committereileen <emcnaughton@wikimedia.org>
Thu, 23 Jun 2016 02:56:24 +0000 (14:56 +1200)
CRM/Report/BAO/ReportInstance.php
CRM/Report/Form.php
CRM/Report/Page/Instance.php

index 9fe13ebaf6cbd2dfca4ba4f58dd01774b702578a..0df77a97eb695f20e810354d697759fa1d701073 100644 (file)
@@ -239,9 +239,17 @@ class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
    *   $results no of deleted Instance on success, false otherwise
    */
   public static function del($id = NULL) {
+    $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $id, 'navigation_id', 'id');
     $dao = new CRM_Report_DAO_ReportInstance();
     $dao->id = $id;
-    return $dao->delete();
+    $result = $dao->delete();
+
+    // Delete navigation if exists.
+    if ($navId) {
+      CRM_Core_BAO_Navigation::processDelete($navId);
+      CRM_Core_BAO_Navigation::resetNavigation();
+    }
+    return $result;
   }
 
   /**
@@ -316,4 +324,26 @@ class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
     return FALSE;
   }
 
+  /**
+   * Delete a report instance wrapped in handling for the form layer.
+   *
+   * @param int $instanceId
+   * @param string $bounceTo
+   *   Url to redirect the browser to on fail.
+   * @param string $successRedirect
+   */
+  public static function doFormDelete($instanceId, $bounceTo = 'civicrm/report/list?reset=1', $successRedirect = NULL) {
+    if (!CRM_Core_Permission::check('administer Reports')) {
+      $statusMessage = ts('You do not have permission to Delete Report.');
+      CRM_Core_Error::statusBounce($statusMessage, $bounceTo);
+    }
+
+    CRM_Report_BAO_ReportInstance::del($instanceId);
+
+    CRM_Core_Session::setStatus(ts('Selected report has been deleted.'), ts('Deleted'), 'success');
+    if ($successRedirect) {
+      CRM_Utils_System::redirect(CRM_Utils_System::url($successRedirect));
+    }
+  }
+
 }
index cb5c6fb4fe3a9b6dc2c69b053eb671520ffac5d9..384a96d682443aaffb307db17dafff3a0a9c7c61 100644 (file)
@@ -2627,23 +2627,16 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
       $this->setParams($this->_formValues);
     }
 
-    $this->_formValues = $this->_params;
-    if (CRM_Core_Permission::check('administer Reports') &&
-      isset($this->_id) &&
-      ($this->_instanceButtonName ==
-        $this->controller->getButtonName() . '_save' ||
-        $this->_chartButtonName == $this->controller->getButtonName()
-      )
-    ) {
-      $this->assign('updateReportButton', TRUE);
-    }
-
     $this->processReportMode();
 
     if ($this->_outputMode == 'save' || $this->_outputMode == 'copy') {
       $this->_createNew = ($this->_outputMode == 'copy');
       CRM_Report_Form_Instance::postProcess($this);
     }
+    if ($this->_outputMode == 'delete') {
+      CRM_Report_BAO_ReportInstance::doFormDelete($this->_id, 'civicrm/report/list?reset=1', 'civicrm/report/list?reset=1');
+    }
+
     $this->beginPostProcessCommon();
   }
 
index 7c5bd87f34a371733bb1f18663c885c5d2f32394..8012311b5a7e7b913fc767aa46785f93592b1825 100644 (file)
@@ -43,66 +43,54 @@ class CRM_Report_Page_Instance extends CRM_Core_Page {
     if (!$instanceId) {
       $instanceId = CRM_Report_Utils_Report::getInstanceIDForPath();
     }
+
+    $action = CRM_Utils_Request::retrieve('action', 'String', $this);
+    $reportUrl = CRM_Utils_System::url('civicrm/report/list', "reset=1");
+
+    if ($action & CRM_Core_Action::DELETE) {
+      CRM_Report_BAO_ReportInstance::doFormDelete($reportUrl, $instanceId);
+      return CRM_Utils_System::redirect($reportUrl);
+    }
+
     if (is_numeric($instanceId)) {
       $instanceURL = CRM_Utils_System::url("civicrm/report/instance/{$instanceId}", 'reset=1');
       CRM_Core_Session::singleton()->replaceUserContext($instanceURL);
     }
-    $action = CRM_Utils_Request::retrieve('action', 'String', $this);
     $optionVal = CRM_Report_Utils_Report::getValueFromUrl($instanceId);
-    $reportUrl = CRM_Utils_System::url('civicrm/report/list', "reset=1");
-
-    if ($action & CRM_Core_Action::DELETE) {
-      if (!CRM_Core_Permission::check('administer Reports')) {
-        $statusMessage = ts('You do not have permission to Delete Report.');
-        CRM_Core_Error::statusBounce($statusMessage, $reportUrl);
-      }
+    $templateInfo = CRM_Core_OptionGroup::getRowValues('report_template', "{$optionVal}", 'value');
+    if (empty($templateInfo)) {
+      CRM_Core_Error::statusBounce('You have tried to access a report that does not exist.');
+    }
 
-      $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceId, 'navigation_id', 'id');
-      CRM_Report_BAO_ReportInstance::del($instanceId);
+    $extKey = strpos($templateInfo['name'], '.');
 
-      //delete navigation if exists
-      if ($navId) {
-        CRM_Core_BAO_Navigation::processDelete($navId);
-        CRM_Core_BAO_Navigation::resetNavigation();
-      }
+    $reportClass = NULL;
 
-      CRM_Core_Session::setStatus(ts('Selected report has been deleted.'), ts('Deleted'), 'success');
+    if ($extKey !== FALSE) {
+      $ext = CRM_Extension_System::singleton()->getMapper();
+      $reportClass = $ext->keyToClass($templateInfo['name'], 'report');
+      $templateInfo['name'] = $reportClass;
     }
-    else {
-      $templateInfo = CRM_Core_OptionGroup::getRowValues('report_template', "{$optionVal}", 'value');
-      if (empty($templateInfo)) {
-        CRM_Core_Error::statusBounce('You have tried to access a report that does not exist.');
-      }
-
-      $extKey = strpos($templateInfo['name'], '.');
 
-      $reportClass = NULL;
+    if (strstr($templateInfo['name'], '_Form') || !is_null($reportClass)) {
+      $instanceInfo = array();
+      CRM_Report_BAO_ReportInstance::retrieve(array('id' => $instanceId), $instanceInfo);
 
-      if ($extKey !== FALSE) {
-        $ext = CRM_Extension_System::singleton()->getMapper();
-        $reportClass = $ext->keyToClass($templateInfo['name'], 'report');
-        $templateInfo['name'] = $reportClass;
+      if (!empty($instanceInfo['title'])) {
+        CRM_Utils_System::setTitle($instanceInfo['title']);
+        $this->assign('reportTitle', $instanceInfo['title']);
       }
-
-      if (strstr($templateInfo['name'], '_Form') || !is_null($reportClass)) {
-        $instanceInfo = array();
-        CRM_Report_BAO_ReportInstance::retrieve(array('id' => $instanceId), $instanceInfo);
-
-        if (!empty($instanceInfo['title'])) {
-          CRM_Utils_System::setTitle($instanceInfo['title']);
-          $this->assign('reportTitle', $instanceInfo['title']);
-        }
-        else {
-          CRM_Utils_System::setTitle($templateInfo['label']);
-          $this->assign('reportTitle', $templateInfo['label']);
-        }
-
-        $wrapper = new CRM_Utils_Wrapper();
-        return $wrapper->run($templateInfo['name'], NULL, NULL);
+      else {
+        CRM_Utils_System::setTitle($templateInfo['label']);
+        $this->assign('reportTitle', $templateInfo['label']);
       }
 
-      CRM_Core_Session::setStatus(ts('Could not find template for the instance.'), ts('Template Not Found'), 'error');
+      $wrapper = new CRM_Utils_Wrapper();
+      return $wrapper->run($templateInfo['name'], NULL, NULL);
     }
+
+    CRM_Core_Session::setStatus(ts('Could not find template for the instance.'), ts('Template Not Found'), 'error');
+
     return CRM_Utils_System::redirect($reportUrl);
   }