Commit 05255389 authored by bgm's avatar bgm Committed by Aegir user
Browse files

Add an admin UI for the activity type, and fix the profile submission when the...

Add an admin UI for the activity type, and fix the profile submission when the activity type is not atype=1.
parent 36cc6da4
<?php
use CRM_Activityprofile_ExtensionUtil as E;
class CRM_Activityprofile_UF_Form_Group {
/**
* @see activityprofile_civicrm_buildForm().
*/
public static function buildForm(&$form) {
$activity_types = [
'' => E::ts('- none -'),
];
$activity_types += civicrm_api3('Activity', 'getoptions', [
'field' => 'activity_type_id',
])['values'];
$form->add('select', 'activityprofile_atype', E::ts('Create Activity'), $activity_types, NULL, ['class' => 'crm-select2']);
CRM_Core_Region::instance('page-body')->add([
'template' => 'CRM/Activityprofile/UF/Form/Group-atype.tpl',
]);
// Set default
if ($id = $form->getVar('_id')) {
if ($atype = Civi::settings()->get('activityprofile_' . $id . '_atype')) {
$form->setDefaults([
'activityprofile_atype' => $atype,
]);
}
}
}
/**
* @see activityprofile_civicrm_postProcess().
*/
public static function postProcess(&$form) {
$values = $form->exportValues();
$id = $form->getVar('_id');
Civi::settings()->set('activityprofile_' . $id . '_atype', $values['activityprofile_atype']);
}
}
......@@ -8,11 +8,11 @@ For more complex requirements, you may want to look at [form-processor](https://
Known issues:
* This only works when used in combination with the [remoteforms](https://github.com/progressivetech/net.ourpowerbase.remoteform/) extension.
* Profile submissions will always create a new contact. Your profile should include at least one of: first_name, last_name, email or display_name. The creation of a new contact is required to workaround checks in CiviCRM core to validate that a given contact can update an activity.
Roadmap:
* Blocker: Profile Admin UI: add an option to associate a profile to an activity type. Currently the DB must be manually changed so that group_type allows the activity type.
* Wishlist: Profile Admin UI: option to automatically assign the activity to a specific contact (ex: for a contact form, assign to the person who handles the contact form).
* Wishlist: Auto-dedupe (according to the profile settings).
......@@ -29,4 +29,6 @@ Install as a regular CiviCRM extension.
## Usage
(* FIXME: Where would a new user navigate to get started? What changes would they see? *)
* Create a new CiviCRM profile (Administer > Customize Data and Screens > Profiles)
* In the profile settings, set the Activity Type for the profile submissions.
* Add fields in the profile. Avoid mixing fields from incompatible entities (only one contact type, and custom fields associated with only one Activity Type, if any).
......@@ -125,10 +125,71 @@ function activityprofile_civicrm_apiWrappers(&$wrappers, $apiRequest) {
/**
* Implements hook_civicrm_buildForm().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/
* Add remoteform options to key forms in CiviCRM Core.
*
* @param string $formName
* @param CRM_Core_Form $form
*/
function activityprofile_civicrm_buildForm($formName, &$form) {
if ($formName == 'CRM_Profile_Form_FIXME') {
if ($formName == 'CRM_UF_Form_Group') {
CRM_Activityprofile_UF_Form_Group::buildForm($form);
}
}
/**
* Implements hook_civicrm_postProcess().
*
* Add remoteform options to key forms in CiviCRM Core.
*
* @param string $formName
* @param CRM_Core_Form $form
*/
function activityprofile_civicrm_postProcess($formName, &$form) {
if ($formName == 'CRM_UF_Form_Group') {
CRM_Activityprofile_UF_Form_Group::postProcess($form);
}
}
/**
* Implements hook_civicrm_post().
*
* @param string $op
* @param string $objectName
* @param integer $objectId
* @param object $objectRef
*/
function activityprofile_civicrm_post($op, $objectName, $objectId, &$objectRef) {
if ($op == 'create' && $objectName == 'Activity') {
// Detect if we are being called from remoteform
// and if so, set the correct Activity Type.
// CiviCRM core has other safeguards against this, so it's a bit of a loophole.
$path = CRM_Utils_System::getUrlPath();
if ($path == 'civicrm/remoteform') {
// Copied from remoteform
$remoteform = new CRM_Remoteform_Page_RemoteForm();
$data = json_decode(stripslashes(file_get_contents("php://input")));
try {
$data = $remoteform->sanitizeInput($data);
}
catch (Exception $e) {
$remoteform->exitError($e->getMessage());
}
// Only a safeguard, this should not happen.
if (empty($data) || empty($data['params']['profile_id'])) {
return;
}
$profile_id = $data['params']['profile_id'];
$activity_type_id = Civi::settings()->get('activityprofile_' . $profile_id . '_atype');
if ($activity_type_id) {
CRM_Core_DAO::executeQuery('UPDATE civicrm_activity SET activity_type_id = 28 WHERE id = %1', [
1 => [$objectRef->id, 'Positive'],
]);
}
}
}
}
<table>
<tr id="activityprofile-atype">
<td class="label">
<label>{$form.activityprofile_atype.label}</label>
</td>
<td>
{$form.activityprofile_atype.html}
<div class="description">{ts}If set, an activity will be created each time the profile is submitted. It also makes it possible to add activity-specific custom fields to the profile.{/ts}</div>
</td>
</tr>
</table>
{literal}
<script>
CRM.$('tr#activityprofile-atype').appendTo('.crm-uf-advancesetting-form-block > table > tbody');
</script>
{/literal}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment