Merge remote-tracking branch 'origin/4.5' into 4.5-master-2015-02-24-17-24-05
[civicrm-core.git] / CRM / PCP / Form / Campaign.php
index 846fbe31af7778d78f3f23e5b2750ce2e73a1323..b84bae80aec248fded2363b6123037c96b23c999 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -86,6 +86,7 @@ class CRM_PCP_Form_Campaign extends CRM_Core_Form {
       $defaults['is_active'] = 1;
       $defaults['is_honor_roll'] = 1;
       $defaults['is_thermometer'] = 1;
+      $defaults['is_notify'] = 1;
     }
 
     $this->_contactID = CRM_Utils_Array::value('contact_id', $defaults);
@@ -95,7 +96,7 @@ class CRM_PCP_Form_Campaign extends CRM_Core_Form {
   }
 
   /**
-   * Build the form object
+   * Build the form object.
    *
    * @return void
    */
@@ -120,14 +121,30 @@ class CRM_PCP_Form_Campaign extends CRM_Core_Form {
     }
 
     $attrib = array('rows' => 8, 'cols' => 60);
-    $this->add('textarea', 'page_text', ts('Your Message'), null, false );
+    $this->add('textarea', 'page_text', ts('Your Message'), NULL, FALSE);
 
     $maxAttachments = 1;
     CRM_Core_BAO_File::buildAttachment($this, 'civicrm_pcp', $this->_pageId, $maxAttachments);
 
     $this->addElement('checkbox', 'is_thermometer', ts('Progress Bar'));
     $this->addElement('checkbox', 'is_honor_roll', ts('Honor Roll'), NULL);
+    if ($this->_pageId) {
+      $params = array('id' => $this->_pageId);
+      CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $params, $pcpInfo);
+      $owner_notification_option = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCPBlock', $pcpInfo['pcp_block_id'], 'owner_notify_id');
+    }
+    else {
+      $owner_notification_option = CRM_PCP_BAO_PCP::getOwnerNotificationId($this->controller->get('component_page_id'), $this->_component ? $this->_component : 'contribute');
+    }
+    if ($owner_notification_option == CRM_Core_OptionGroup::getValue('pcp_owner_notify', 'owner_chooses', 'name')) {
+      $this->assign('owner_notification_option', TRUE);
+      $this->addElement('checkbox', 'is_notify', ts('Notify me via email when someone donates to my page'), NULL);
+    }
+
     $this->addElement('checkbox', 'is_active', ts('Active'));
+    if ($this->_context == 'dashboard') {
+      CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin/pcp', 'reset=1'));
+    }
 
     $this->addButtons(
       array(
@@ -146,15 +163,17 @@ class CRM_PCP_Form_Campaign extends CRM_Core_Form {
   }
 
   /**
-   * Global form rule
+   * Global form rule.
    *
-   * @param array $fields the input form values
-   * @param array $files the uploaded files if any
+   * @param array $fields
+   *   The input form values.
+   * @param array $files
+   *   The uploaded files if any.
    * @param $self
    *
    *
-   * @return true if no errors, else array of errors
-   * @static
+   * @return bool|array
+   *   true if no errors, else array of errors
    */
   public static function formRule($fields, $files, $self) {
     $errors = array();
@@ -168,14 +187,14 @@ class CRM_PCP_Form_Campaign extends CRM_Core_Form {
   }
 
   /**
-   * Process the form submission
+   * Process the form submission.
    *
    *
    * @return void
    */
   public function postProcess() {
-    $params  = $this->controller->exportValues( $this->_name );
-    $checkBoxes = array('is_thermometer', 'is_honor_roll', 'is_active');
+    $params = $this->controller->exportValues($this->_name);
+    $checkBoxes = array('is_thermometer', 'is_honor_roll', 'is_active', 'is_notify');
 
     foreach ($checkBoxes as $key) {
       if (!isset($params[$key])) {
@@ -195,9 +214,9 @@ class CRM_PCP_Form_Campaign extends CRM_Core_Form {
 
     // since we are allowing html input from the user
     // we also need to purify it, so lets clean it up
-    $htmlFields = array( 'intro_text', 'page_text', 'title' );
-    foreach ( $htmlFields as $field ) {
-      if ( ! empty($params[$field]) ) {
+    $htmlFields = array('intro_text', 'page_text', 'title');
+    foreach ($htmlFields as $field) {
+      if (!empty($params[$field])) {
         $params[$field] = CRM_Utils_String::purifyHTML($params[$field]);
       }
     }
@@ -225,7 +244,6 @@ class CRM_PCP_Form_Campaign extends CRM_Core_Form {
 
     $pcp = CRM_PCP_BAO_PCP::add($params, FALSE);
 
-
     // add attachments as needed
     CRM_Core_BAO_File::formatAttachment($params,
       $params,
@@ -237,7 +255,7 @@ class CRM_PCP_Form_Campaign extends CRM_Core_Form {
     $statusId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcp->id, 'status_id');
 
     //send notification of PCP create/update.
-    $pcpParams    = array('entity_table' => $entity_table, 'entity_id' => $pcp->page_id);
+    $pcpParams = array('entity_table' => $entity_table, 'entity_id' => $pcp->page_id);
     $notifyParams = array();
     $notifyStatus = "";
     CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $pcpParams, $notifyParams, array('notify_email'));
@@ -265,7 +283,6 @@ class CRM_PCP_Form_Campaign extends CRM_Core_Form {
       $supporterName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $pcp->contact_id, 'display_name');
       $this->assign('supporterName', $supporterName);
 
-
       if ($this->_component == 'contribute') {
         $pageUrl = CRM_Utils_System::url('civicrm/contribute/transact',
           "reset=1&id={$pcpBlock->entity_id}",
@@ -343,8 +360,8 @@ class CRM_PCP_Form_Campaign extends CRM_Core_Form {
     }
 
     CRM_Core_Session::setStatus(ts("Your Personal Campaign Page has been %1 %2 %3",
-        array(1 => $pageStatus, 2 => $approvalMessage, 3 => $notifyStatus)
-      ), '', 'info');
+      array(1 => $pageStatus, 2 => $approvalMessage, 3 => $notifyStatus)
+    ), '', 'info');
     if (!$this->_pageId) {
       $session->pushUserContext(CRM_Utils_System::url('civicrm/pcp/info', "reset=1&id={$pcp->id}&ap={$anonymousPCP}"));
     }
@@ -352,4 +369,5 @@ class CRM_PCP_Form_Campaign extends CRM_Core_Form {
       $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/pcp', 'reset=1'));
     }
   }
+
 }