INFRA-132 - CRM/Contribute - phpcbf
[civicrm-core.git] / CRM / Contribute / Page / ContributionPage.php
index 98fcf165cfec88b4cb568624531d861edda78c67..ae80cd90df544c092958e64ca430826685cc3a60 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -56,18 +56,22 @@ class CRM_Contribute_Page_ContributionPage extends CRM_Core_Page {
   private static $_configureActionLinks;
   private static $_onlineContributionLinks;
 
-  private static $_links = NULL;
-
+  /**
+   * @var CRM_Utils_Pager
+   */
   protected $_pager = NULL;
 
+  /**
+   * @var string
+   */
   protected $_sortByCharacter;
 
   /**
    * Get the action links for this page.
    *
-   * @return array $_actionLinks
-   *
-   */ function &actionLinks() {
+   * @return array
+   */
+  function &actionLinks() {
     // check if variable _actionsLinks is populated
     if (!isset(self::$_actionLinks)) {
       // helper variable for nicer formatting
@@ -107,10 +111,9 @@ class CRM_Contribute_Page_ContributionPage extends CRM_Core_Page {
   /**
    * Get the configure action links for this page.
    *
-   * @return array $_configureActionLinks
-   *
+   * @return array
    */
-  function &configureActionLinks() {
+  public function &configureActionLinks() {
     // check if variable _actionsLinks is populated
     if (!isset(self::$_configureActionLinks)) {
       $urlString = 'civicrm/admin/contribute/';
@@ -189,10 +192,9 @@ class CRM_Contribute_Page_ContributionPage extends CRM_Core_Page {
   /**
    * Get the online contribution links.
    *
-   * @return array $_onlineContributionLinks.
-   *
+   * @return array
    */
-  function &onlineContributionLinks() {
+  public function &onlineContributionLinks() {
     if (!isset(self::$_onlineContributionLinks)) {
       $urlString = 'civicrm/contribute/transact';
       $urlParams = 'reset=1&id=%%id%%';
@@ -221,10 +223,9 @@ class CRM_Contribute_Page_ContributionPage extends CRM_Core_Page {
   /**
    * Get the contributions links.
    *
-   * @return array $_contributionLinks
-   *
+   * @return array
    */
-  function &contributionLinks() {
+  public function &contributionLinks() {
     if (!isset(self::$_contributionLinks)) {
       //get contribution dates.
       $dates = CRM_Contribute_BAO_Contribution::getContributionDates();
@@ -271,11 +272,9 @@ class CRM_Contribute_Page_ContributionPage extends CRM_Core_Page {
    * type of action and executes that action.
    * Finally it calls the parent's run method.
    *
-   * @return void
-   * @access public
-   *
+   * @return mixed
    */
-  function run() {
+  public function run() {
     // get the requested action
     $action = CRM_Utils_Request::retrieve('action', 'String',
       // default to 'browse'
@@ -289,7 +288,8 @@ class CRM_Contribute_Page_ContributionPage extends CRM_Core_Page {
     );
 
     // set breadcrumb to append to 2nd layer pages
-    $breadCrumb = array(array('title' => ts('Manage Contribution Pages'),
+    $breadCrumb = array(array(
+    'title' => ts('Manage Contribution Pages'),
         'url' => CRM_Utils_System::url(CRM_Utils_System::currentPath(),
           'reset=1'
         ),
@@ -319,6 +319,9 @@ class CRM_Contribute_Page_ContributionPage extends CRM_Core_Page {
       }
     }
     elseif ($action & CRM_Core_Action::COPY) {
+      // @todo Unused local variable can be safely removed.
+      // But are there any side effects of CRM_Core_Session::singleton() that we
+      // need to preserve?
       $session = CRM_Core_Session::singleton();
       CRM_Core_Session::setStatus(ts('A copy of the contribution page has been created'), ts('Successfully Copied'), 'success');
       $this->copy();
@@ -371,9 +374,8 @@ AND         cp.page_type = 'contribute'
    * all the fields in the page
    *
    * @return void
-   * @access public
    */
-  function copy() {
+  public function copy() {
     $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
       $this, TRUE, 0, 'GET'
     );
@@ -386,17 +388,18 @@ AND         cp.page_type = 'contribute'
   /**
    * Browse all contribution pages
    *
-   * @param null $action
-   *
-   * @return void
-   * @access public
+   * @param mixed $action
+   *   Unused parameter.
    * @static
    */
-  function browse($action = NULL) {
+  public function browse($action = NULL) {
     $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter',
       'String',
       $this
     );
+    // @todo Unused local variable can be safely removed.
+    // But are there any side effects of CRM_Utils_Request::retrieve() that we
+    // need to preserve?
     $createdId = CRM_Utils_Request::retrieve('cid', 'Positive',
       $this, FALSE, 0
     );
@@ -469,15 +472,15 @@ ORDER BY title asc
       $action += array_sum(array_keys(self::contributionLinks()));
 
       if ($dao->is_active) {
-        $action -= CRM_Core_Action::ENABLE;
+        $action -= (int) CRM_Core_Action::ENABLE;
       }
       else {
-        $action -= CRM_Core_Action::DISABLE;
+        $action -= (int) CRM_Core_Action::DISABLE;
       }
 
       //CRM-4418
       if (!$allowToDelete) {
-        $action -= CRM_Core_Action::DELETE;
+        $action -= (int) CRM_Core_Action::DELETE;
       }
 
       //build the configure links.
@@ -534,9 +537,8 @@ ORDER BY title asc
     }
   }
 
-  function search() {
-    if (isset($this->_action) &
-      (CRM_Core_Action::ADD |
+  public function search() {
+    if (isset($this->_action) & (CRM_Core_Action::ADD |
         CRM_Core_Action::UPDATE |
         CRM_Core_Action::DELETE
       )
@@ -560,7 +562,8 @@ ORDER BY title asc
    *
    * @return int|string
    */
-  function whereClause(&$params, $sortBy = TRUE) {
+  public function whereClause(&$params, $sortBy = TRUE) {
+    // @todo Unused local variable can be safely removed.
     $values    = $clauses = array();
     $title     = $this->get('title');
     $createdId = $this->get('cid');
@@ -579,7 +582,7 @@ ORDER BY title asc
       }
     }
 
-        $value = $this->get( 'financial_type_id' );
+    $value = $this->get( 'financial_type_id' );
     $val = array();
     if ($value) {
       if (is_array($value)) {
@@ -590,22 +593,17 @@ ORDER BY title asc
         }
         $type = implode(',', $val);
       }
-
-             $clauses[] = "financial_type_id IN ({$type})";
+      // @todo Variable 'type' might not have been defined.
+      $clauses[] = "financial_type_id IN ({$type})";
     }
 
-    if ($sortBy &&
-      $this->_sortByCharacter !== NULL
-    ) {
+    if ($sortBy && $this->_sortByCharacter !== NULL) {
       $clauses[] = "title LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
     }
 
-    $campainIds = $this->get('campaign_id');
-    if (!CRM_Utils_System::isNull($campainIds)) {
-      if (!is_array($campainIds)) {
-        $campaignIds = array($campaignIds);
-      }
-      $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
+    $campaignIds = $this->getCampaignIds();
+    if (count($campaignIds) >= 1) {
+      $clauses[] = '( campaign_id IN ( ' . implode(' , ', $campaignIds) . ' ) )';
     }
 
     if (empty($clauses)) {
@@ -620,6 +618,23 @@ ORDER BY title asc
     return implode(' AND ', $clauses);
   }
 
+  /**
+   * Gets the campaign ids from the session.
+   *
+   * @return int[]
+   */
+  function getCampaignIds() {
+    // The unfiltered value from the session cannot be trusted, it needs to be
+    // processed to get a clean array of positive integers.
+    $ids = array();
+    foreach ((array) $this->get('campaign_id') as $id) {
+      if ((string) (int) $id === (string) $id && $id > 0) {
+        $ids[] = $id;
+      }
+    }
+    return $ids;
+  }
+
   /**
    * @param $whereClause
    * @param array $whereParams
@@ -637,8 +652,8 @@ ORDER BY title asc
 
     $query = "
 SELECT count(id)
-  FROM civicrm_contribution_page
- WHERE $whereClause";
+FROM civicrm_contribution_page
+WHERE $whereClause";
 
     $params['total'] = CRM_Core_DAO::singleValueQuery($query, $whereParams);
 
@@ -650,13 +665,13 @@ SELECT count(id)
    * @param $whereClause
    * @param array $whereParams
    */
-  function pagerAtoZ($whereClause, $whereParams) {
+  public function pagerAtoZ($whereClause, $whereParams) {
 
     $query = "
-   SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
-     FROM civicrm_contribution_page
-    WHERE $whereClause
- ORDER BY LEFT(title, 1)
+SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
+FROM civicrm_contribution_page
+WHERE $whereClause
+ORDER BY LEFT(title, 1)
 ";
     $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
 
@@ -665,12 +680,12 @@ SELECT count(id)
   }
 
   /**
-   * @param $sectionsInfo
+   * @param array $sectionsInfo
    *
    * @return array
    */
-  function formatConfigureLinks($sectionsInfo) {
-    //build the formatted configure links.
+  public function formatConfigureLinks($sectionsInfo) {
+    // build the formatted configure links.
     $formattedConfLinks = self::configureActionLinks();
     foreach ($formattedConfLinks as $act => & $link) {
       $sectionName = CRM_Utils_Array::value('uniqueName', $link);
@@ -678,11 +693,6 @@ SELECT count(id)
         continue;
       }
 
-      $classes = array();
-      if (isset($link['class'])) {
-        $classes = $link['class'];
-      }
-
       if (empty($sectionsInfo[$sectionName])) {
         $classes = array();
         if (isset($link['class'])) {
@@ -695,4 +705,3 @@ SELECT count(id)
     return $formattedConfLinks;
   }
 }
-