Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-21520: Implement Search Action to Add Contacts to Case #11371

Merged
31 changes: 31 additions & 0 deletions CRM/Case/Form/AddContact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

require_once 'CRM/Core/Page.php';

class CRM_Case_Form_AddContact extends CRM_Core_Form {
public function buildQuickForm() {
$formBuilder = new CRM_Case_FormBuilder($this);
$formBuilder->build();
}

public function postProcess() {
$values = $this->controller->exportValues();

$caseId = (int)$values['assign_to'];
$roleTypeId = (int)$values['role_type'];
$contacts = array((int)CRM_Utils_Request::retrieve('cid', 'Positive'));

$clients = CRM_Case_BAO_Case::getCaseClients($caseId);

$params = array(
'contact_id_a' => $clients[0],
'contact_id_b' => $contacts,
'case_id' => $caseId,
'relationship_type_id' => $roleTypeId
);

CRM_Contact_BAO_Relationship::createMultiple($params, 'a');

CRM_Core_Session::setStatus(ts('Contact has been added to case.'), 'Information', 'success');
}
}
33 changes: 33 additions & 0 deletions CRM/Case/Form/AddToCaseAsRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

class CRM_Case_Form_AddToCaseAsRole extends CRM_Contact_Form_Task {
public function buildQuickForm() {
$formBuilder = new CRM_Case_FormBuilder($this);
$formBuilder->build();
}

public function postProcess() {
$values = $this->controller->exportValues();

$caseId = (int)$values['assign_to'];
$roleTypeId = (int)$values['role_type'];
$contacts = $this->_contactIds;

$clients = CRM_Case_BAO_Case::getCaseClients($caseId);

$params = array(
'contact_id_a' => $clients[0],
'contact_id_b' => $contacts,
'case_id' => $caseId,
'relationship_type_id' => $roleTypeId
);

CRM_Contact_BAO_Relationship::createMultiple($params, 'a');

$url = CRM_Utils_System::url(
'civicrm/contact/view/case',
sprintf('cid=%d&id=%d', $clients[0], $caseId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing a few arguments so the redirect doesn't quite work. IMO an array is more readable than sprintf. Recommend changing to:

array(
  'cid' => $clients[0],
  'id' => $caseId,
  'reset' => 1,
  'action' => 'view',
)

);
CRM_Utils_System::redirect($url);
}
}
43 changes: 43 additions & 0 deletions CRM/Case/FormBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

class CRM_Case_FormBuilder {
private $form;

public function __construct(CRM_Core_Form $form) {
$this->form = $form;
}

public function build() {
$this->form->add('text', 'assign_to', ts('Assign to'));
$roleTypes = $this->getRoleTypes();

$this->form->add(
'select',
'role_type',
ts('Relationship Type'),
array('' => ts('- select type -')) + $roleTypes,
FALSE,
array('class' => 'crm-select2 twenty')
);

$this->form->addButtons(array(
array(
'type' => 'submit',
'name' => ts('Submit'),
'isDefault' => true
)
));
}

/**
* @return array
*/
private function getRoleTypes() {
$relType = CRM_Contact_BAO_Relationship::getRelationType('Individual');
$roleTypes = array();
foreach ($relType as $k => $v) {
$roleTypes[substr($k, 0, strpos($k, '_'))] = $v;
}
return $roleTypes;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand the purpose of breaking these 2 functions into a separate file. What's wrong with doing this in the CRM_Case_Form_AddToCaseAsRole class?

7 changes: 7 additions & 0 deletions CRM/Case/xml/Menu/Case.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,11 @@
<path>civicrm/ajax/delcaserole</path>
<page_callback>CRM_Case_Page_AJAX::deleteCaseRoles</page_callback>
</item>
<item>
<path>civicrm/case/add-contact</path>
<page_callback>CRM_Case_Form_AddContact</page_callback>
<title>AddContact</title>
<access_arguments>access CiviCRM</access_arguments>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think access_arguments should be access my cases and activities

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, what is this entry? I don't think this class exists. Can this be removed?

<page_type>0</page_type>
</item>
</menu>
8 changes: 8 additions & 0 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3105,6 +3105,14 @@ public static function contextMenu($contactId = NULL) {
);
}

$menu['add-to-case-as-role'] = array(
'title' => 'Add to case as role',
'href' => CRM_Utils_System::url('civicrm/case/add-contact', 'reset=1'),
'weight' => 100,
'ref' => 'add-to-case-as-role',
'key' => 'add-to-case-as-role'
);

CRM_Utils_Hook::summaryActions($menu, $contactId);
//1. check for component is active.
//2. check for user permissions.
Expand Down
6 changes: 6 additions & 0 deletions CRM/Contact/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ public static function initTasks() {
);
}

self::$_tasks[] = array(
'title' => 'Add to case as role',
'class' => 'CRM_Case_Form_AddToCaseAsRole',
'result' => FALSE
);

self::$_tasks += CRM_Core_Component::taskList();

CRM_Utils_Hook::searchTasks('contact', self::$_tasks);
Expand Down
1 change: 1 addition & 0 deletions templates/CRM/Case/Form/AddContact.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{include file="CRM/Caseroles/Form/AddToCaseAsRole.tpl"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file seems quite useless. What's the purpose of not putting the contents of templates/CRM/Case/Form/AddToCaseAsRole.tpl here?

19 changes: 19 additions & 0 deletions templates/CRM/Case/Form/AddToCaseAsRole.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div><label for="assign_to">{ts}Assign To{/ts}:</label></div>
<div><input name="assign_to" data-api-entity="case" placeholder="{ts}- select case -{/ts}" class="huge" /></div>

<div>{$form.role_type.label}</div>
<div>{$form.role_type.html}</div><br />

<div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>

{literal}
<script type="text/javascript">
(function($, CRM) {
$(function() {
$('[name=assign_to], [name=role]', this)
.val('')
.crmEntityRef({create: false});
});
})(cj, CRM);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for adding a simple textfield from the formbuilder and then javascripting it into an entityref after the fact? Why not just add an entityref from the getgo?

</script>
{/literal}