Example #1
0
/**
 * Check mandatory fields are included.
 *
 * @param array $params
 *   Array of fields to check.
 * @param array $daoName
 *   String DAO to check for required fields (create functions only).
 * @param array $keys
 *   List of required fields. A value can be an array denoting that either this or that is required.
 * @param bool $verifyDAO
 *
 * @throws \API_Exception
 */
function civicrm_api3_verify_mandatory($params, $daoName = NULL, $keys = array(), $verifyDAO = TRUE)
{
    $unmatched = array();
    if ($daoName != NULL && $verifyDAO && empty($params['id'])) {
        $unmatched = _civicrm_api3_check_required_fields($params, $daoName, TRUE);
        if (!is_array($unmatched)) {
            $unmatched = array();
        }
    }
    if (!empty($params['id'])) {
        $keys = array('version');
    } else {
        if (!in_array('version', $keys)) {
            // required from v3 onwards
            $keys[] = 'version';
        }
    }
    foreach ($keys as $key) {
        if (is_array($key)) {
            $match = 0;
            $optionset = array();
            foreach ($key as $subkey) {
                if (!array_key_exists($subkey, $params) || empty($params[$subkey])) {
                    $optionset[] = $subkey;
                } else {
                    // as long as there is one match then we don't need to rtn anything
                    $match = 1;
                }
            }
            if (empty($match) && !empty($optionset)) {
                $unmatched[] = "one of (" . implode(", ", $optionset) . ")";
            }
        } else {
            // Disallow empty values except for the number zero.
            // TODO: create a utility for this since it's needed in many places
            if (!array_key_exists($key, $params) || empty($params[$key]) && $params[$key] !== 0 && $params[$key] !== '0') {
                $unmatched[] = $key;
            }
        }
    }
    if (!empty($unmatched)) {
        throw new API_Exception("Mandatory key(s) missing from params array: " . implode(", ", $unmatched), "mandatory_missing", array("fields" => $unmatched));
    }
}
function civicrm_api3_verify_mandatory($params, $daoName = NULL, $keys = array(), $verifyDAO = TRUE)
{
    if ($daoName != NULL && $verifyDAO && !CRM_Utils_Array::value('id', $params)) {
        if (!is_array($unmatched = _civicrm_api3_check_required_fields($params, $daoName, TRUE))) {
            $unmatched = array();
        }
    } else {
        // always define to prevent E_NOTICE warning
        $unmatched = array();
    }
    require_once 'CRM/Utils/Array.php';
    if (CRM_Utils_Array::value('id', $params)) {
        $keys = array('version');
    } else {
        if (!in_array('version', $keys)) {
            // required from v3 onwards
            $keys[] = 'version';
        }
    }
    foreach ($keys as $key) {
        if (is_array($key)) {
            $match = 0;
            $optionset = array();
            foreach ($key as $subkey) {
                if (!array_key_exists($subkey, $params) || empty($params[$subkey])) {
                    $optionset[] = $subkey;
                } else {
                    // as long as there is one match then we don't need to rtn anything
                    $match = 1;
                }
            }
            if (empty($match) && !empty($optionset)) {
                $unmatched[] = "one of (" . implode(", ", $optionset) . ")";
            }
        } else {
            if (!array_key_exists($key, $params) || empty($params[$key])) {
                $unmatched[] = $key;
            }
        }
    }
    if (!empty($unmatched)) {
        throw new Exception("Mandatory key(s) missing from params array: " . implode(", ", $unmatched));
    }
}