Beispiel #1
0
/**
 * Defines 'uf field' within a group.
 *
 * @param array $params
 *   Array per getfields metadata.
 *
 * @throws API_Exception
 *
 * @return array
 *   Newly created $ufFieldArray
 */
function civicrm_api3_uf_field_create($params)
{
    // CRM-14756: kind of a hack-ish fix. If the user gives the id, uf_group_id is retrieved and then set.
    if (isset($params['id'])) {
        $groupId = civicrm_api3('UFField', 'getvalue', array('return' => 'uf_group_id', 'id' => $params['id']));
    } else {
        $groupId = CRM_Utils_Array::value('uf_group_id', $params);
    }
    $field_type = CRM_Utils_Array::value('field_type', $params);
    $field_name = CRM_Utils_Array::value('field_name', $params);
    $location_type_id = CRM_Utils_Array::value('location_type_id', $params, CRM_Utils_Array::value('website_type_id', $params));
    $phone_type = CRM_Utils_Array::value('phone_type_id', $params, CRM_Utils_Array::value('phone_type', $params));
    if (strpos($field_name, 'formatting') !== 0 && !CRM_Core_BAO_UFField::isValidFieldName($field_name)) {
        throw new API_Exception('The field_name is not valid');
    }
    $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
    if (!CRM_Utils_Array::value('group_id', $params)) {
        $params['group_id'] = $groupId;
    }
    $ids = $ufFieldArray = array();
    $ids['uf_group'] = $groupId;
    $fieldId = CRM_Utils_Array::value('id', $params);
    if (!empty($fieldId)) {
        $UFField = new CRM_Core_BAO_UFField();
        $UFField->id = $fieldId;
        if ($UFField->find(TRUE)) {
            $ids['uf_group'] = $UFField->uf_group_id;
            if (!CRM_Utils_Array::value('group_id', $params)) {
                // this copied here from previous api function - not sure if required
                $params['group_id'] = $UFField->uf_group_id;
            }
        } else {
            throw new API_Exception("there is no field for this fieldId");
        }
        $ids['uf_field'] = $fieldId;
    }
    if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) {
        throw new API_Exception("The field was not added. It already exists in this profile.");
    }
    //@todo why is this even optional? Surely weight should just be 'managed' ??
    if (CRM_Utils_Array::value('option.autoweight', $params, TRUE)) {
        $params['weight'] = CRM_Core_BAO_UFField::autoWeight($params);
    }
    $ufField = CRM_Core_BAO_UFField::add($params);
    $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($groupId, TRUE);
    CRM_Core_BAO_UFGroup::updateGroupTypes($groupId, $fieldsType);
    _civicrm_api3_object_to_array($ufField, $ufFieldArray[$ufField->id]);
    civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE));
    return civicrm_api3_create_success($ufFieldArray, $params);
}
/**
 * Defines 'uf field' within a group.
 *
 * @param array $params
 *   Array per getfields metadata.
 *
 * @throws API_Exception
 *
 * @return array
 *   Newly created $ufFieldArray
 */
function civicrm_api3_uf_field_create($params)
{
    civicrm_api3_verify_one_mandatory($params, NULL, array('field_name', 'uf_group_id'));
    $groupId = CRM_Utils_Array::value('uf_group_id', $params);
    if ((int) $groupId < 1) {
        throw new API_Exception('Params must be a field_name-carrying array and a positive integer.');
    }
    $field_type = CRM_Utils_Array::value('field_type', $params);
    $field_name = CRM_Utils_Array::value('field_name', $params);
    $location_type_id = CRM_Utils_Array::value('location_type_id', $params, CRM_Utils_Array::value('website_type_id', $params));
    $phone_type = CRM_Utils_Array::value('phone_type_id', $params, CRM_Utils_Array::value('phone_type', $params));
    if (!CRM_Core_BAO_UFField::isValidFieldName($field_name)) {
        throw new API_Exception('The field_name is not valid');
    }
    $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
    if (!CRM_Utils_Array::value('group_id', $params)) {
        $params['group_id'] = $groupId;
    }
    $ids = $ufFieldArray = array();
    $ids['uf_group'] = $groupId;
    $fieldId = CRM_Utils_Array::value('id', $params);
    if (!empty($fieldId)) {
        $UFField = new CRM_Core_BAO_UFField();
        $UFField->id = $fieldId;
        if ($UFField->find(TRUE)) {
            $ids['uf_group'] = $UFField->uf_group_id;
            if (!CRM_Utils_Array::value('group_id', $params)) {
                // this copied here from previous api function - not sure if required
                $params['group_id'] = $UFField->uf_group_id;
            }
        } else {
            throw new API_Exception("there is no field for this fieldId");
        }
        $ids['uf_field'] = $fieldId;
    }
    if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) {
        throw new API_Exception("The field was not added. It already exists in this profile.");
    }
    //@todo why is this even optional? Surely weight should just be 'managed' ??
    if (CRM_Utils_Array::value('option.autoweight', $params, TRUE)) {
        $params['weight'] = CRM_Core_BAO_UFField::autoWeight($params);
    }
    $ufField = CRM_Core_BAO_UFField::add($params, $ids);
    $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($groupId, TRUE);
    CRM_Core_BAO_UFGroup::updateGroupTypes($groupId, $fieldsType);
    _civicrm_api3_object_to_array($ufField, $ufFieldArray[$ufField->id]);
    civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE));
    return civicrm_api3_create_success($ufFieldArray, $params);
}
 /**
  * returns the column headers as an array of tuples:
  * (name, sortName (key to the sort array))
  *
  * @param string $action the action being performed
  * @param enum   $output what should the result set include (web/email/csv)
  *
  * @return array the column headers that need to be displayed
  * @access public
  */
 function &getColumnHeaders($action = NULL, $output = NULL)
 {
     static $skipFields = array('group', 'tag');
     $multipleFields = array('url');
     $direction = CRM_Utils_Sort::ASCENDING;
     $empty = TRUE;
     if (!isset(self::$_columnHeaders)) {
         self::$_columnHeaders = array(array('name' => ''), array('name' => ts('Name'), 'sort' => 'sort_name', 'direction' => CRM_Utils_Sort::ASCENDING, 'field_name' => 'sort_name'));
         $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
         foreach ($this->_fields as $name => $field) {
             // skip pseudo fields
             if (substr($name, 0, 9) == 'phone_ext') {
                 continue;
             }
             if (!empty($field['in_selector']) && !in_array($name, $skipFields)) {
                 if (strpos($name, '-') !== FALSE) {
                     $value = explode('-', $name);
                     $fieldName = CRM_Utils_Array::value(0, $value);
                     $lType = CRM_Utils_Array::value(1, $value);
                     $type = CRM_Utils_Array::value(2, $value);
                     if (!in_array($fieldName, $multipleFields)) {
                         if ($lType == 'Primary') {
                             $locationTypeName = 1;
                         } else {
                             $locationTypeName = $locationTypes[$lType];
                         }
                         if (in_array($fieldName, array('phone', 'im', 'email'))) {
                             if ($type) {
                                 $name = "`{$locationTypeName}-{$fieldName}-{$type}`";
                             } else {
                                 $name = "`{$locationTypeName}-{$fieldName}`";
                             }
                         } else {
                             $name = "`{$locationTypeName}-{$fieldName}`";
                         }
                     } else {
                         $name = "website-{$lType}-{$fieldName}";
                     }
                 }
                 self::$_columnHeaders[] = array('name' => $field['title'], 'sort' => $name, 'direction' => $direction, 'field_name' => CRM_Core_BAO_UFField::isValidFieldName($name) ? $name : $fieldName);
                 $direction = CRM_Utils_Sort::DONTCARE;
                 $empty = FALSE;
             }
         }
         // if we dont have any valid columns, dont add the implicit ones
         // this allows the template to check on emptiness of column headers
         if ($empty) {
             self::$_columnHeaders = array();
         } else {
             self::$_columnHeaders[] = array('desc' => ts('Actions'));
         }
     }
     return self::$_columnHeaders;
 }