Exemplo n.º 1
0
 /**
  * Returns all the rows in the given offset and rowCount.
  *
  * @param string $action
  *   The action being performed.
  * @param int $offset
  *   The row number to start from.
  * @param int $rowCount
  *   The number of rows to return.
  * @param string $sort
  *   The sql string that describes the sort order.
  * @param string $output
  *   What should the result set include (web/email/csv).
  *
  * @return int
  *   the total number of rows for this action
  */
 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL)
 {
     $config = CRM_Core_Config::singleton();
     if (($output == CRM_Core_Selector_Controller::EXPORT || $output == CRM_Core_Selector_Controller::SCREEN) && $this->_formValues['radio_ts'] == 'ts_sel') {
         $includeContactIds = TRUE;
     } else {
         $includeContactIds = FALSE;
     }
     // note the formvalues were given by CRM_Contact_Form_Search to us
     // and contain the search criteria (parameters)
     // note that the default action is basic
     if ($rowCount) {
         $cacheKey = $this->buildPrevNextCache($sort);
         $result = $this->_query->getCachedContacts($cacheKey, $offset, $rowCount, $includeContactIds);
     } else {
         $result = $this->_query->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds);
     }
     // process the result of the query
     $rows = array();
     $permissions = array(CRM_Core_Permission::getPermission());
     if (CRM_Core_Permission::check('delete contacts')) {
         $permissions[] = CRM_Core_Permission::DELETE;
     }
     $mask = CRM_Core_Action::mask($permissions);
     // mask value to hide map link if there are not lat/long
     $mapMask = $mask & 4095;
     if ($this->_searchContext == 'smog') {
         $gc = CRM_Core_SelectValues::groupContactStatus();
     }
     if ($this->_ufGroupID) {
         $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
         $names = array();
         static $skipFields = array('group', 'tag');
         foreach ($this->_fields as $key => $field) {
             if (!empty($field['in_selector']) && !in_array($key, $skipFields)) {
                 if (strpos($key, '-') !== FALSE) {
                     list($fieldName, $id, $type) = CRM_Utils_System::explode('-', $key, 3);
                     if ($id == 'Primary') {
                         $locationTypeName = 1;
                     } elseif ($fieldName == 'url') {
                         $locationTypeName = "website-{$id}";
                     } else {
                         $locationTypeName = CRM_Utils_Array::value($id, $locationTypes);
                         if (!$locationTypeName) {
                             continue;
                         }
                     }
                     $locationTypeName = str_replace(' ', '_', $locationTypeName);
                     if (in_array($fieldName, array('phone', 'im', 'email'))) {
                         if ($type) {
                             $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
                         } else {
                             $names[] = "{$locationTypeName}-{$fieldName}";
                         }
                     } else {
                         $names[] = "{$locationTypeName}-{$fieldName}";
                     }
                 } else {
                     $names[] = $field['name'];
                 }
             }
         }
         $names[] = "status";
     } elseif (!empty($this->_returnProperties)) {
         $names = self::makeProperties($this->_returnProperties);
     } else {
         $names = self::$_properties;
     }
     $multipleSelectFields = array('preferred_communication_method' => 1);
     $links = self::links($this->_context, $this->_contextMenu, $this->_key);
     //check explicitly added contact to a Smart Group.
     $groupID = CRM_Utils_Array::value('group', $this->_formValues);
     $pseudoconstants = array();
     // for CRM-3157 purposes
     if (in_array('world_region', $names)) {
         $pseudoconstants['world_region'] = array('dbName' => 'worldregion_id', 'values' => CRM_Core_PseudoConstant::worldRegion());
     }
     $seenIDs = array();
     while ($result->fetch()) {
         $row = array();
         $this->_query->convertToPseudoNames($result);
         // the columns we are interested in
         foreach ($names as $property) {
             if ($property == 'status') {
                 continue;
             }
             if ($cfID = CRM_Core_BAO_CustomField::getKeyID($property)) {
                 $row[$property] = CRM_Core_BAO_CustomField::getDisplayValue($result->{$property}, $cfID, $this->_options, $result->contact_id);
             } elseif ($multipleSelectFields && array_key_exists($property, $multipleSelectFields)) {
                 $key = $property;
                 $paramsNew = array($key => $result->{$property});
                 $name = array($key => array('newName' => $key, 'groupName' => $key));
                 CRM_Core_OptionGroup::lookupValues($paramsNew, $name, FALSE);
                 $row[$key] = $paramsNew[$key];
             } elseif (strpos($property, '-im')) {
                 $row[$property] = $result->{$property};
                 if (!empty($result->{$property})) {
                     $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
                     $providerId = $property . "-provider_id";
                     $providerName = $imProviders[$result->{$providerId}];
                     $row[$property] = $result->{$property} . " ({$providerName})";
                 }
             } elseif (in_array($property, array('addressee', 'email_greeting', 'postal_greeting'))) {
                 $greeting = $property . '_display';
                 $row[$property] = $result->{$greeting};
             } elseif (isset($pseudoconstants[$property])) {
                 $row[$property] = CRM_Utils_Array::value($result->{$pseudoconstants[$property]['dbName']}, $pseudoconstants[$property]['values']);
             } elseif (strpos($property, '-url') !== FALSE) {
                 $websiteUrl = '';
                 $websiteKey = str_replace('-url', '', $property);
                 $propertyArray = explode('-', $property);
                 $websiteFld = $websiteKey . '-' . array_pop($propertyArray);
                 if (!empty($result->{$websiteFld})) {
                     $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id');
                     $websiteType = $websiteTypes[$result->{"{$websiteKey}-website_type_id"}];
                     $websiteValue = $result->{$websiteFld};
                     $websiteUrl = "<a href=\"{$websiteValue}\">{$websiteValue}  ({$websiteType})</a>";
                 }
                 $row[$property] = $websiteUrl;
             } else {
                 $row[$property] = isset($result->{$property}) ? $result->{$property} : NULL;
             }
         }
         if (!empty($result->postal_code_suffix)) {
             $row['postal_code'] .= "-" . $result->postal_code_suffix;
         }
         if ($output != CRM_Core_Selector_Controller::EXPORT && $this->_searchContext == 'smog') {
             if (empty($result->status) && $groupID) {
                 $contactID = $result->contact_id;
                 if ($contactID) {
                     $gcParams = array('contact_id' => $contactID, 'group_id' => $groupID);
                     $gcDefaults = array();
                     CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_GroupContact', $gcParams, $gcDefaults);
                     if (empty($gcDefaults)) {
                         $row['status'] = ts('Smart');
                     } else {
                         $row['status'] = $gc[$gcDefaults['status']];
                     }
                 } else {
                     $row['status'] = NULL;
                 }
             } else {
                 $row['status'] = $gc[$result->status];
             }
         }
         if ($output != CRM_Core_Selector_Controller::EXPORT) {
             $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contact_id;
             if (!empty($this->_formValues['deleted_contacts']) && CRM_Core_Permission::check('access deleted contacts')) {
                 $links = array(array('name' => ts('View'), 'url' => 'civicrm/contact/view', 'qs' => 'reset=1&cid=%%id%%', 'class' => 'no-popup', 'title' => ts('View Contact Details')), array('name' => ts('Restore'), 'url' => 'civicrm/contact/view/delete', 'qs' => 'reset=1&cid=%%id%%&restore=1', 'title' => ts('Restore Contact')));
                 if (CRM_Core_Permission::check('delete contacts')) {
                     $links[] = array('name' => ts('Delete Permanently'), 'url' => 'civicrm/contact/view/delete', 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1', 'title' => ts('Permanently Delete Contact'));
                 }
                 $row['action'] = CRM_Core_Action::formLink($links, NULL, array('id' => $result->contact_id), ts('more'), FALSE, 'contact.selector.row', 'Contact', $result->contact_id);
             } elseif (is_numeric(CRM_Utils_Array::value('geo_code_1', $row)) || !empty($row['city']) && CRM_Utils_Array::value('state_province', $row)) {
                 $row['action'] = CRM_Core_Action::formLink($links, $mask, array('id' => $result->contact_id), ts('more'), FALSE, 'contact.selector.row', 'Contact', $result->contact_id);
             } else {
                 $row['action'] = CRM_Core_Action::formLink($links, $mapMask, array('id' => $result->contact_id), ts('more'), FALSE, 'contact.selector.row', 'Contact', $result->contact_id);
             }
             // allow components to add more actions
             CRM_Core_Component::searchAction($row, $result->contact_id);
             $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id);
             $row['contact_type_orig'] = $result->contact_sub_type ? $result->contact_sub_type : $result->contact_type;
             $row['contact_sub_type'] = $result->contact_sub_type ? CRM_Contact_BAO_ContactType::contactTypePairs(FALSE, $result->contact_sub_type, ', ') : $result->contact_sub_type;
             $row['contact_id'] = $result->contact_id;
             $row['sort_name'] = $result->sort_name;
             if (array_key_exists('id', $row)) {
                 $row['id'] = $result->contact_id;
             }
         }
         // Dedupe contacts
         if (in_array($row['contact_id'], $seenIDs) === FALSE) {
             $seenIDs[] = $row['contact_id'];
             $rows[] = $row;
         }
     }
     return $rows;
 }