From 982ece690d98cb649a2b4f9129f6d6b731f45500 Mon Sep 17 00:00:00 2001 From: Michael Z Daryabeygi Date: Sun, 15 Mar 2020 23:08:25 -0400 Subject: [PATCH] Address PHP Notices (#536) * don't extend DAO::copyValues; encapsulate need to pass DAO objects to copyValues in constructor; * pass seed to new constructor; * quiet PHP Notices --- CRM/Volunteer/BAO/NeedSearch.php | 8 ++- CRM/Volunteer/BAO/Project.php | 85 ++++++++++---------------- CRM/Volunteer/Form/VolunteerSignUp.php | 2 +- api/v3/VolunteerUtil.php | 6 +- 4 files changed, 44 insertions(+), 57 deletions(-) diff --git a/CRM/Volunteer/BAO/NeedSearch.php b/CRM/Volunteer/BAO/NeedSearch.php index 33e28ac4..94af0936 100644 --- a/CRM/Volunteer/BAO/NeedSearch.php +++ b/CRM/Volunteer/BAO/NeedSearch.php @@ -262,8 +262,12 @@ private function getSearchResultsProjectData() { $countryId = $api['api.LocBlock.getsingle']['api.Address.getsingle']['country_id']; $country = $countryId ? CRM_Core_PseudoConstant::country($countryId) : NULL; - $stateProvinceId = $api['api.LocBlock.getsingle']['api.Address.getsingle']['state_province_id']; - $stateProvince = $stateProvinceId ? CRM_Core_PseudoConstant::stateProvince($stateProvinceId) : NULL; + $stateProvince = NULL; + if (isset($api['api.LocBlock.getsingle']['api.Address.getsingle']['state_province_id'])) { + $stateProvinceId = $api['api.LocBlock.getsingle']['api.Address.getsingle']['state_province_id']; + $stateProvince = CRM_Core_PseudoConstant::stateProvince($stateProvinceId); + } + $project['location'] = array( 'city' => $api['api.LocBlock.getsingle']['api.Address.getsingle']['city'], diff --git a/CRM/Volunteer/BAO/Project.php b/CRM/Volunteer/BAO/Project.php index 000215de..ce86f88f 100644 --- a/CRM/Volunteer/BAO/Project.php +++ b/CRM/Volunteer/BAO/Project.php @@ -112,8 +112,16 @@ class CRM_Volunteer_BAO_Project extends CRM_Volunteer_DAO_Project { /** * class constructor */ - function __construct() { + function __construct($params=null) { parent::__construct(); + + if (!empty($params)) { + if (is_a($params, 'CRM_Core_DAO')) { + $daoClone = clone $params; // seems uncessary: lost in the fog of war + $params = get_object_vars($daoClone); // get an array + } + $this->copyValues($params); + } } /** @@ -269,8 +277,7 @@ public static function create(array $params) { $params = self::validateCreateParams($params); $params = self::supplyDefaults($params); - $project = new CRM_Volunteer_BAO_Project(); - $project->copyValues($params); + $project = new CRM_Volunteer_BAO_Project($params); $project->save(); $customData = CRM_Core_BAO_CustomField::postProcess($params, $project->id, 'VolunteerProject'); @@ -412,19 +419,14 @@ private static function allowedToRetrieve($projectId = NULL) { } /** - * Get a list of Projects matching the params. + * Get a list of Projects filtered by project-fields + * or related entities: Project-Contacts and Proximity (loc_block) + * NOTE: related entities are not returned, just available for filtering. * * This function is invoked from within the web form layer and also from the - * API layer. Special params include: - *
    - *
  1. project_contacts (@see CRM_Volunteer_BAO_Project::create() and - * CRM_Volunteer_BAO_Project::buildContactJoin)
  2. - *
  3. proximity (@see CRM_Volunteer_BAO_Project::buildProximityWhere)
  4. - *
- * - * NOTE: This method does not return data related to the special params - * outlined above; however, these parameters can be used to filter the list - * of Projects that is returned. + * API layer. + * + * @see CRM_Volunteer_BAO_Project::create(), CRM_Volunteer_BAO_Project::buildContactJoin(), CRM_Volunteer_BAO_Project::buildProximityWhere() * * @param array $params * @return array of CRM_Volunteer_BAO_Project objects @@ -454,16 +456,19 @@ public static function retrieve(array $params) { ->join('civicrm_address', 'INNER JOIN `civicrm_address` ON civicrm_address.id = loc.address_id') ->where(self::buildProximityWhere($params['proximity'])); } + + if (isset($params['is_active'])) { + if (CRM_Volunteer_BAO_Project::isOff($params['is_active'])) { + $params['is_active'] = 0; + } else { + $params['is_active'] = 1; + } + } - // This step is here to support both naming conventions for specifying params - // (e.g., volunteer_project_id and id) while normalizing how we access them - // (e.g., $project->id) - $project = new CRM_Volunteer_BAO_Project(); - $project->copyValues($params); - + // normalize field names and get DAO defaults: + $project = new CRM_Volunteer_BAO_Project($params); foreach ($project->fields() as $field) { $fieldName = $field['name']; - if (!empty($project->$fieldName)) { $query->where('!column = @value', array( 'column' => $fieldName, @@ -473,11 +478,9 @@ public static function retrieve(array $params) { } $dao = self::executeQuery($query->toSQL()); + while ($dao->fetch()) { - $fetchedProject = new CRM_Volunteer_BAO_Project(); - $daoClone = clone $dao; - $fetchedProject->copyValues($daoClone); - $result[(int) $dao->id] = $fetchedProject; + $result[(int) $dao->id] = new CRM_Volunteer_BAO_Project($dao); } $dao->free(); @@ -611,40 +614,16 @@ static function retrieveByID($id) { } /** - * Returns TRUE if value represents an "off" value, FALSE otherwise + * Convert truthy to Boolean. + * Empty or null return FALSE (on) + * FALSE, 0, '0' return TRUE (Off) * * @param type $value * @return boolean * @access public */ public static function isOff($value) { - if (in_array($value, array(FALSE, 0, '0'), TRUE)) { - return TRUE; - } else { - return FALSE; - } - } - - /** - * @inheritDoc This override adds a little data massaging prior to calling its - * parent. - * - * @deprecated since version 4.7.21-2.3.0 - * Internal core methods should not be extended by third-party code. - */ - public function copyValues(&$params, $serializeArrays = FALSE) { - if (is_a($params, 'CRM_Core_DAO')) { - $params = get_object_vars($params); - } - - if (array_key_exists('is_active', $params)) { - /* - * don't force is_active to have a value if none was set, to allow searches - * where the is_active state of Projects is irrelevant - */ - $params['is_active'] = CRM_Volunteer_BAO_Project::isOff($params['is_active']) ? 0 : 1; - } - return parent::copyValues($params, $serializeArrays); + return in_array($value, array(FALSE, 0, '0'), TRUE); } /** diff --git a/CRM/Volunteer/Form/VolunteerSignUp.php b/CRM/Volunteer/Form/VolunteerSignUp.php index 0e4e3e1a..3dd851e6 100644 --- a/CRM/Volunteer/Form/VolunteerSignUp.php +++ b/CRM/Volunteer/Form/VolunteerSignUp.php @@ -608,7 +608,7 @@ function processAdditionalVolunteers(array $data) { $profileFields = $this->getProfileFields($this->getAdditionalVolunteerProfileIDs()); $profileFieldsByType = array_reduce($profileFields, array($this, 'reduceByType'), array()); - $activityProfileFields = CRM_Utils_Array::value('Activity', $profileFrieldsByType, array()); + $activityProfileFields = CRM_Utils_Array::value('Activity', $profileFieldsByType, array()); $index = 0; while ($index < $qty) { diff --git a/api/v3/VolunteerUtil.php b/api/v3/VolunteerUtil.php index b7da725b..cfed3da0 100644 --- a/api/v3/VolunteerUtil.php +++ b/api/v3/VolunteerUtil.php @@ -296,7 +296,11 @@ function civicrm_api3_volunteer_util_getcountries($params) { foreach ($results as $k => $country) { // since we are wrapping CiviCRM's API, and it provides even boolean data // as quoted strings, we'll do the same - $results[$k]['is_default'] = ($country['id'] === $settings['values'][0]['defaultContactCountry']) ? "1" : "0"; + $is_default = "0"; + if (isset($settings['values'][0]['defaultContactCountry']) && $country['id'] === $settings['values'][0]['defaultContactCountry'] ) { + $is_default = "1"; + } + $results[$k]['is_default'] = $is_default; } return civicrm_api3_create_success($results, "VolunteerUtil", "getcountries", $params);