Commit 872c9581 authored by BohdanDmytryshyn's avatar BohdanDmytryshyn
Browse files

Merge branch 'release_5_0_0' into 'master'

Release 5.0.0

See merge request !42
parents c32b3d39 db393ffe
<?php
class CRM_CiviMobileAPI_Api_CiviMobileCalendar_Get extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* Returns results to api
*
* @return array
* @throws Exception
*/
public function getResult() {
$calendarParams = [
'hidePastEvents' => CRM_CiviMobileAPI_Settings_Calendar::getHidePastEvents(),
'startDate' => $this->validParams["start"],
'endDate' => $this->validParams["end"],
'type' => $this->validParams['type']
];
$events = [];
if ($this->getPermission()) {
$eventsHandler = new CRM_CiviMobileAPI_Calendar_Handler($this->validParams['contact_id'], $calendarParams);
$events = $eventsHandler->getAll();
}
return $events;
}
/**
* Returns validated params
* @param $params
*
* @return mixed
*/
protected function getValidParams($params) {
return $params;
}
/**
* Gets permission if user allowed to receive events
*
* @return bool
* @throws \CiviCRM_API3_Exception
*/
private function getPermission() {
$result = civicrm_api3('CiviMobilePermission', 'get', [
'sequential' => 1,
]);
if (!isset($result['values'][0]['event']['view']['all']) || empty($result['values'][0]['event']['view']['all'])) {
return FALSE;
}
return $result['values'][0]['event']['view']['all'] ? TRUE : FALSE;
}
}
<?php
use CRM_CiviMobileAPI_Utils_Cms_Registration as CMSRegistration;
class CRM_CiviMobileAPI_Api_CiviMobileCmsRegistration_Create extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* Returns results to api
*
* @return array
* @throws api_Exception
*/
public function getResult() {
$transaction = new CRM_Core_Transaction();
$currentCMS = CRM_CiviMobileAPI_Utils_CmsUser::getInstance()->getSystem();
if ($currentCMS == CRM_CiviMobileAPI_Utils_CmsUser::CMS_DRUPAL7) {
module_load_include('pages.inc', 'user');
user_logout_current_user();
}
try {
$contact = civicrm_api3('Contact', 'create', [
'contact_type' => "Individual",
'api.Email.create' => ['email' => $this->validParams["email"]],
'first_name' => $this->validParams["first_name"],
'last_name' => $this->validParams["last_name"],
]);
} catch (CiviCRM_API3_Exception $e) {
$transaction->rollback();
throw new api_Exception("CiviCRM creating Contact error: " . $e->getMessage(), 'creating_contact_error');
}
$this->validParams['contactID'] = $contact['id'];
if (!CRM_Core_BAO_CMSUser::create($this->validParams, 'email')) {
$transaction->rollback();
throw new api_Exception('Creating CiviCRM contact error', 'creating_contact_error');
}
$transaction->commit();
$message = 'User was registered.';
$successCode = 'registration_success';
if ($currentCMS == CRM_CiviMobileAPI_Utils_CmsUser::CMS_DRUPAL7) {
$isEmailVerification = variable_get('user_email_verification', TRUE);
$isAdministratorApproval = variable_get('user_register', TRUE) == 2;
if ($isEmailVerification && $isAdministratorApproval) {
$message = 'User was registered. You must to check your email to verify your account. Administrator will check your account and confirm user registration request.';
$successCode = "registration_success_email_verification_administrator_confirmation";
} elseif ($isEmailVerification) {
$message = 'You must to check your email to verify your account.';
$successCode = "registration_success_email_verification";
} elseif ($isAdministratorApproval) {
$message = 'Administrator will check your account and confirm user registration request.';
$successCode = "registration_success_administrator_confirmation";
}
}
if ($currentCMS == CRM_CiviMobileAPI_Utils_CmsUser::CMS_JOOMLA) {
$uParams = JComponentHelper::getParams('com_users');
switch ($uParams->get('useractivation')) {
case 1:
$message = 'You must to check your email to verify your account.';
$successCode = "registration_success_email_verification";
break;
case 2:
$message = 'Administrator will check your account and confirm user registration request.';
$successCode = "registration_success_administrator_confirmation";
break;
}
}
return [
[
'message' => $message,
'success_code' => $successCode,
]
];
}
/**
* Returns validated params
*
* @param $params
*
* @return array
* @throws api_Exception
*/
protected function getValidParams($params) {
$cmsUserParams = [
'name' => $params["username"],
'pass' => $params["password"],
'mail' => $params["email"],
];
$config = CRM_Core_Config::singleton();
if (!$config->userSystem->isUserRegistrationPermitted()) {
throw new api_Exception("No permissions to create user", 'no_permissions_to_create_user');
}
if (strlen($cmsUserParams["name"]) < CMSRegistration::minUsernameLength()) {
throw new api_Exception("Username must have at least " . CMSRegistration::minUsernameLength() . " characters", 'username_has_not_enough_characters');
}
if (strlen($cmsUserParams["name"]) > CMSRegistration::maxUsernameLength()) {
throw new api_Exception("Username must have no more " . CMSRegistration::maxUsernameLength() . " characters", 'username_has_too_much_characters');
}
if (strlen($cmsUserParams["pass"]) < CMSRegistration::minPasswordLength()) {
throw new api_Exception("Password must have at least ". CMSRegistration::minPasswordLength() . " characters", 'password_has_not_enough_characters');
}
if (strlen($cmsUserParams["pass"]) > CMSRegistration::maxPasswordLength()) {
throw new api_Exception("Password must have no more " . CMSRegistration::maxPasswordLength() . " characters", 'password_has_too_much_characters');
}
if (preg_match_all("/[a-z]/", $cmsUserParams["pass"]) < CMSRegistration::minPasswordLowerCase()) {
throw new api_Exception("Password must have at least " . CMSRegistration::minPasswordLowerCase() . " characters in lower case", 'password_has_not_enough_characters_in_lower_case');
}
if (preg_match_all("/[A-Z]/", $cmsUserParams["pass"]) < CMSRegistration::minPasswordUpperCase()) {
throw new api_Exception("Password must have at least " . CMSRegistration::minPasswordUpperCase() . " characters in upper case", 'password_has_not_enough_characters_in_upper_case');
}
if (preg_match_all("/[0-9]/", $cmsUserParams["pass"]) < CMSRegistration::minPasswordIntegers()) {
throw new api_Exception("Password must have at least " . CMSRegistration::minPasswordIntegers() . " integers", 'password_has_not_enough_integers');
}
if (preg_match_all("/[^a-zA-Z0-9]/", $cmsUserParams["pass"]) < CMSRegistration::minPasswordSymbols()) {
throw new api_Exception("Password must have at least " . CMSRegistration::minPasswordSymbols() . " symbols", 'password_has_not_enough_symbols');
}
if (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]+$/ix", $cmsUserParams["mail"])) {
throw new api_Exception("Invalid email", 'email_is_not_valid');
}
if (strlen($params["first_name"]) > CMSRegistration::maxFirstNameLength()) {
throw new api_Exception("First name must have no more " . CMSRegistration::maxFirstNameLength() . " characters", 'first_name_has_too_much_characters');
}
if (strlen($params["last_name"]) > CMSRegistration::maxLastNameLength()) {
throw new api_Exception("Last name must have no more " . CMSRegistration::maxLastNameLength() . " characters", 'last_name_has_too_much_characters');
}
$errors = [];
$config->userSystem->checkUserNameEmailExists($cmsUserParams, $errors);
if (!empty($errors)) {
if (array_key_exists('cms_name', $errors)) {
throw new api_Exception($errors['cms_name'], 'username_already_exists');
}
if (array_key_exists('email',$errors)) {
throw new api_Exception($errors['email'], 'email_already_exists');
}
throw new api_Exception('Unknown error', 'unknown_email_error');
}
return [
'cms_name' => $cmsUserParams['name'],
'cms_pass' => $cmsUserParams['pass'],
'email' => $params["email"],
'first_name' => $params["first_name"],
'last_name' => $params["last_name"],
];
}
}
<?php
class CRM_CiviMobileAPI_Api_CiviMobileGetPriceSetByEvent_Get extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* Returns validated params
*
* @param $params
*
* @return array
* @throws \api_Exception
*/
protected function getValidParams($params) {
return [
'event_id' => $params['event_id']
];
}
/**
* Returns results to api
*
* @return array
*/
public function getResult() {
$eventId = (int) $this->validParams['event_id'];
try {
$eventInfo = civicrm_api3('Event', 'getsingle', [
'return' => ["is_monetary", "currency"],
'id' => $eventId
]);
} catch (CiviCRM_API3_Exception $e) {
$eventInfo = [];
}
$result = [];
$isMonetary = false;
if (!empty($eventInfo)) {
$isMonetary = !empty($eventInfo['is_monetary']) ? $eventInfo['is_monetary'] : false;
$currencyName = $eventInfo['currency'];
}
if ($isMonetary != 1) {
return $result;
}
$priceSetId = CRM_Price_BAO_PriceSet::getFor(CRM_Event_BAO_Event::getTableName(), $eventId);
$priceSetFields = CRM_CiviMobileAPI_Utils_PriceSet::getFields($priceSetId);
if (empty($priceSetFields) && empty($priceSetFields['values'])) {
return $result;
}
foreach ($priceSetFields['values'] as $priceSetField) {
if (!CRM_CiviMobileAPI_Utils_PriceSetField::isActualPriceFieldNow($priceSetField)) {
continue;
}
$priceSetFieldValue = CRM_CiviMobileAPI_Utils_PriceSetField::getPriceSetFieldValue($priceSetField['id']);
foreach ($priceSetFieldValue['values'] as &$value) {
if (!empty($currencyName)) {
$value['amount_currency'] = CRM_Utils_Money::format($value['amount'], $currencyName);
$value['currency_name'] = $currencyName;
}
}
if ($priceSetFieldValue) {
$result[] = [
'id' => $priceSetField['id'],
'price_set_id' => $priceSetId,
'label' => $priceSetField['label'],
'type' => $priceSetField['html_type'],
'is_required' => $priceSetField['is_required'],
'items' => $priceSetFieldValue['values'],
];
}
}
return $result;
}
}
......@@ -75,7 +75,7 @@ class CRM_CiviMobileAPI_Api_CiviMobileParticipantPayment_Create {
CRM_CiviMobileAPI_Utils_Emails_EventConfirmationReceipt::send($participant['id'], 'event_offline_receipt');
}
}
return $result;
}
......@@ -164,7 +164,7 @@ class CRM_CiviMobileAPI_Api_CiviMobileParticipantPayment_Create {
$priceSetField = $this->findPriceSetFiled($priceSetFields, $selectedValue['field_id']);
if (empty($priceSetField)) {
throw new api_Exception('Field id=(' . $selectedValue['field_id'] . ') does not exist for Even\'s perice set', 'field_id_does_not_exist');
throw new api_Exception('Field id=(' . $selectedValue['field_id'] . ') does not exist for Event\'s perice set', 'field_id_does_not_exist');
}
if ($priceSetField['html_type'] != 'Text' && $selectedValue['filed_value_count'] != 1) {
......
<?php
class CRM_CiviMobileAPI_Api_CiviMobileParticipantPaymentLink_Get extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* @return array
* @throws api_Exception
*/
public function getResult() {
$cmbHash = md5(time() . rand(0, 10000). time());
$result = [];
$participantPublicKey = NULL;
if ($tmpData = CRM_CiviMobileAPI_BAO_CivimobileEventPaymentInfo::checkByEvent(
$this->validParams['event_id'],
$this->validParams['contact_id'],
$this->validParams['first_name'],
$this->validParams['last_name'],
$this->validParams['email']
)) {
if (!CRM_CiviMobileAPI_BAO_CivimobileEventPaymentInfo::deleteByHash($tmpData['cmb_hash'])) {
throw new api_Exception(ts('Previous Payment cannot be deleted (hash=%1). Please complete payment', [1 => $tmpData['cmb_hash']]), 'participant_previous_payment_not_completed');
}
}
if (!$this->validParams['contact_id']) {
$participantPublicKey = CRM_CiviMobileAPI_Utils_Participant::generatePublicKey($cmbHash);
$result['participantPublicKey'] = $participantPublicKey;
}
CRM_CiviMobileAPI_BAO_CivimobileEventPaymentInfo::setInfoData(
$this->validParams['event_id'],
$this->validParams['contact_id'],
$cmbHash,
json_encode($this->validParams['price_set']),
$this->validParams['first_name'],
$this->validParams['last_name'],
$this->validParams['email'],
$participantPublicKey
);
$currentCMS = CRM_CiviMobileAPI_Utils_CmsUser::getInstance()->getSystem();
$url = CRM_Utils_System::url('civicrm/event/register', 'id=' . $this->validParams['event_id'] . '&reset=1&cmbHash=' . $cmbHash);
if ($currentCMS == CRM_CiviMobileAPI_Utils_CmsUser::CMS_JOOMLA ) {
$url = str_replace("administrator/", "", $url);
}
$result['link'] = html_entity_decode($url);
return $result;
}
/**
* @param $params
* @return array
* @throws CiviCRM_API3_Exception
* @throws api_Exception
*/
protected function getValidParams($params) {
$currentCMS = CRM_CiviMobileAPI_Utils_CmsUser::getInstance()->getSystem();
if ($currentCMS == CRM_CiviMobileAPI_Utils_CmsUser::CMS_JOOMLA) {
throw new api_Exception(ts('Joomla does not support that functionality'), 'joomla_does_not_support_that_functionality');
}
$eventId = (int) $params['event_id'];
$contactId = (int) $params['contact_id'];
$selPriceSet = ((is_array($params['price_set'])) ? $params['price_set'] : json_decode($params['price_set'],true));
try {
civicrm_api3('Event', 'getsingle', ['id' => $eventId]);
} catch (CiviCRM_API3_Exception $e) {
throw new api_Exception(ts('Event(id = %1) User can not be registered because event does not exist.', $eventId), 'event_does_not_exist');
}
if ($contactId) {
$participants = civicrm_api3('Participant', 'get', [
'return' => ['contact_id'],
'event_id' => $eventId,
'contact_id' => $contactId,
'sequential' => 1,
]);
if (!empty($participants['values'])) {
throw new api_Exception(ts('Contact(id = %1) is already registered on the Event(id = %2).', [1 => $contactId, 2 => $eventId]), 'participant_already_exist');
}
try {
civicrm_api3('Contact', 'getsingle', ['id' => $contactId]);
} catch (CiviCRM_API3_Exception $e) {
throw new api_Exception(ts('Contact(id = %1) does not exist.', [1 => $contactId]), 'contact_does_not_exist');
}
} else {
if (empty($params['first_name'])) {
throw new api_Exception(ts('First name \'%1\' cannot be empty.', [1 => $params['first_name']]), 'contact_has_invalid_first_name');
}
if (empty($params['last_name'])) {
throw new api_Exception(ts('Last name \'%1\' cannot be empty.', [1 => $params['last_name']]), 'contact_has_invalid_last_name');
}
if (!filter_var($params['email'], FILTER_VALIDATE_EMAIL)) {
throw new api_Exception(ts('Email \'%1\' have to be valid.', [1 => $params['email']]), 'contact_has_invalid_email');
}
}
$priceSetId = CRM_Price_BAO_PriceSet::getFor(CRM_Event_BAO_Event::getTableName(), $eventId);
if (empty($priceSetId)) {
throw new api_Exception(ts('Can not get price set assigned to event.'), 'event_empty_price_set');
}
$priceSet = $this->getPriceSet($priceSetId);
if (empty($priceSet)) {
throw new api_Exception(ts('Can not get price set assigned to event.'), 'event_empty_price_set');
}
$priceSetFields = CRM_CiviMobileAPI_Utils_PriceSet::getFields($priceSetId);
if (empty($priceSetFields) && empty($priceSetFields['values'])) {
throw new api_Exception(ts('Can not get price set fields assigned to event.'), 'event_empty_price_set_fields');
}
$this->validatePriceSetItems($selPriceSet, $priceSetFields['values']);
return [
'event_id' => $eventId,
'contact_id' => $contactId,
'price_set' => $params['price_set'],
'first_name' => $params['first_name'],
'last_name' => $params['last_name'],
'email' => $params['email'],
];
}
/**
* @param $priceSetId
* @return array|bool
*/
private function getPriceSet($priceSetId) {
try {
$priceSet = civicrm_api3('PriceSet', 'getsingle', [
'sequential' => 1,
'id' => $priceSetId
]);
} catch (CiviCRM_API3_Exception $e) {
return false;
}
return $priceSet;
}
/**
* @param $selPriceSet
* @param $priceSetFields
* @throws api_Exception
*/
private function validatePriceSetItems($selPriceSet, $priceSetFields) {
if (!is_array($selPriceSet)) {
throw new api_Exception(ts('Can not parse selected price set'), 'can_not_parse_selected_price_set');
}
if (!empty($selPriceSet)) {
foreach ($selPriceSet as $psId => $psFieldIds) {
foreach ($psFieldIds[0] as $psFieldId => $psFieldValues) {
if (empty($psFieldValues)) {
throw new api_Exception(ts('Can not parse selected value for field (id = %1) for price set (id = %2)', [1 => $psFieldId, 2 => $psId]), 'field_value_does_not_exist');
}
$priceField = $this->findPriceSetFiled($priceSetFields, $psId, $psFieldId);
if (empty($priceField)) {
throw new api_Exception(ts('Price Field (id = %1) does not exist for Event\'s', [1 => $psFieldId]), 'field_id_does_not_exist');
}
$priceSetFieldValues = $this->getPriceSetFieldValues($priceField['id']);
if (empty($priceSetFieldValues['values'])) {
throw new api_Exception(ts('Empty filed values for price set field (id = %1). Please create it in administer.', [1 => $priceField['id']]), 'empty_price_set_field_values');
}
foreach ($psFieldValues as $item => $psFieldValueId) {
if ($priceField['html_type'] == 'Text' && empty($psFieldValueId[key($psFieldValueId)])) {
throw new api_Exception(ts('"filed_value_count" must be filled for field with "Text" "html_type"'), 'invalid_filed_value_count');
}
$priceSetFieldValue = $this->findPriceSetFiledValue($priceSetFieldValues['values'], key($psFieldValueId));
if (empty($priceSetFieldValue)) {
throw new api_Exception(ts('Not valid value(id = %1) for price set field (id = %2).', [1 => key($psFieldValueId), 2 => $psFieldId]), 'not_valid_value_for_price_set_field');
}
}
}
foreach ($priceSetFields as $priceSetField) {
if ($priceSetField['is_required'] == 1 && !in_array($priceSetField['id'], array_keys($psFieldIds[0]))) {
throw new api_Exception(ts('Price field (id = %1) is required field for price set(id = %2)', [1 => $priceSetField['id'], 2 => $priceSetField['price_set_id']]), 'required_filed_for_price_set');
}
}
}
}
}
/**
* Finds 'price set field' in list by 'price set field id'
*
* @param $priceSetFields
* @param $psId
* @param $psFieldId
* @return bool
*/
private function findPriceSetFiled($priceSetFields, $psId, $psFieldId) {
foreach ($priceSetFields as $field) {
if ($field['id'] == $psFieldId && $field['price_set_id'] == $psId) {
return $field;
}
}
return false;
}
/**
* Gets price set field value
*
* @param $priceSetFieldId
* @return array|bool
*/
private function getPriceSetFieldValues($priceSetFieldId) {
try {
$priceFieldValue = civicrm_api3('PriceFieldValue', 'get', [
'sequential' => 1,
'price_field_id' => $priceSetFieldId,
'is_active' => 1
]);
} catch (CiviCRM_API3_Exception $e) {
return false;
}
return $priceFieldValue;
}
/**
* Finds 'price set field value' in list by 'price set field value id'
*
* @param $priceSetFieldValues
* @param $valueId
* @return bool
*/
private function findPriceSetFiledValue($priceSetFieldValues, $valueId) {
foreach ($priceSetFieldValues as $fieldValue) {
if ($fieldValue['id'] == $valueId) {
return $fieldValue;
}
}
return false;
}
}
<?php
class CRM_CiviMobileAPI_Api_CiviMobilePublicParticipant_Create extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* Returns results to api
*
* @return array
* @throws CiviCRM_API3_Exception
*/
public function getResult() {