Skip to content

Commit

Permalink
Convert relationship form to datepicker
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Oct 19, 2018
1 parent 26b4525 commit 425d606
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
4 changes: 3 additions & 1 deletion CRM/Contact/DAO/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Contact/Relationship.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:49381da59affbf165a4c9ce87c9a68ec)
* (GenCodeChecksum:ce5f84e10ef36ac340837227265123a4)
*/

/**
Expand Down Expand Up @@ -203,6 +203,7 @@ public static function &fields() {
'localizable' => 0,
'html' => [
'type' => 'Select Date',
'formatType' => 'activityDate',
],
],
'end_date' => [
Expand All @@ -216,6 +217,7 @@ public static function &fields() {
'localizable' => 0,
'html' => [
'type' => 'Select Date',
'formatType' => 'activityDate',
],
],
'is_active' => [
Expand Down
36 changes: 13 additions & 23 deletions CRM/Contact/Form/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,13 @@ public function preProcess() {
* Set default values for the form.
*/
public function setDefaultValues() {

$defaults = array();

if ($this->_action & CRM_Core_Action::UPDATE) {
if (!empty($this->_values)) {
$defaults['relationship_type_id'] = $this->_rtypeId;
if (!empty($this->_values['start_date'])) {
list($defaults['start_date']) = CRM_Utils_Date::setDateDefaults($this->_values['start_date']);
}
if (!empty($this->_values['end_date'])) {
list($defaults['end_date']) = CRM_Utils_Date::setDateDefaults($this->_values['end_date']);
}
$defaults['start_date'] = CRM_Utils_Array::value('start_date', $this->_values);
$defaults['end_date'] = CRM_Utils_Array::value('end_date', $this->_values);
$defaults['description'] = CRM_Utils_Array::value('description', $this->_values);
$defaults['is_active'] = CRM_Utils_Array::value('is_active', $this->_values);

Expand Down Expand Up @@ -271,7 +266,6 @@ public function setDefaultValues() {
* Add the rules for form.
*/
public function addRules() {

if (!($this->_action & CRM_Core_Action::DELETE)) {
$this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule'));
}
Expand Down Expand Up @@ -335,8 +329,8 @@ public function buildQuickForm() {

$this->add('advcheckbox', 'is_current_employer', $this->_contactType == 'Organization' ? ts('Current Employee') : ts('Current Employer'));

$this->addField('start_date', array('label' => ts('Start Date'), 'formatType' => 'searchDate'));
$this->addField('end_date', array('label' => ts('End Date'), 'formatType' => 'searchDate'));
$this->addField('start_date', array('label' => ts('Start Date')), FALSE, FALSE);
$this->addField('end_date', array('label' => ts('End Date')), FALSE, FALSE);

$this->addField('is_active', array('label' => ts('Enabled?'), 'type' => 'advcheckbox'));

Expand Down Expand Up @@ -373,9 +367,11 @@ public function buildQuickForm() {

/**
* This function is called when the form is submitted and also from unit test.
*
* @param array $params
*
* @return array
* @throws \CRM_Core_Exception
*/
public function submit($params) {
switch ($this->getAction()) {
Expand Down Expand Up @@ -425,7 +421,6 @@ public function postProcess() {
'#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId),
),
);

}

/**
Expand All @@ -442,9 +437,7 @@ public static function dateRule($params) {

// check start and end date
if (!empty($params['start_date']) && !empty($params['end_date'])) {
$start_date = CRM_Utils_Date::format(CRM_Utils_Array::value('start_date', $params));
$end_date = CRM_Utils_Date::format(CRM_Utils_Array::value('end_date', $params));
if ($start_date && $end_date && (int ) $end_date < (int ) $start_date) {
if ($params['end_date'] < $params['start_date']) {
$errors['end_date'] = ts('The relationship end date cannot be prior to the start date.');
}
}
Expand Down Expand Up @@ -494,6 +487,7 @@ protected function setMessage($outcome) {

/**
* @param $relationshipList
*
* @return array
*/
public static function getRelationshipTypeMetadata($relationshipList) {
Expand Down Expand Up @@ -539,11 +533,10 @@ private function deleteAction($id) {
* @param array $params
*
* @return array
* @throws \CRM_Core_Exception
*/
private function updateAction($params) {
$params = $this->preparePostProcessParameters($params);
$params = $params[0];

list($params, $_) = $this->preparePostProcessParameters($params);
try {
civicrm_api3('relationship', 'create', $params);
}
Expand All @@ -561,6 +554,7 @@ private function updateAction($params) {
* @param array $params
*
* @return array
* @throws \CRM_Core_Exception
*/
private function createAction($params) {
list($params, $primaryContactLetter) = $this->preparePostProcessParameters($params);
Expand Down Expand Up @@ -595,12 +589,6 @@ private function preparePostProcessParameters($values) {
else {
$params['id'] = $this->_relationshipId;
$params['contact_id_' . $b] = $params['related_contact_id'];

foreach (array('start_date', 'end_date') as $dateParam) {
if (!empty($params[$dateParam])) {
$params[$dateParam] = CRM_Utils_Date::processDate($params[$dateParam]);
}
}
}

// If this is a b_a relationship these form elements are flipped
Expand All @@ -615,6 +603,8 @@ private function preparePostProcessParameters($values) {
*
* @param array $relationshipIds
* @param string $note
*
* @throws \CiviCRM_API3_Exception
*/
private function saveRelationshipNotes($relationshipIds, $note) {
foreach ($relationshipIds as $id) {
Expand Down
3 changes: 1 addition & 2 deletions templates/CRM/Contact/Form/Relationship.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@
</tr>
<tr class="crm-relationship-form-block-start_date">
<td class="label">{$form.start_date.label}</td>
<td>{include file="CRM/common/jcalendar.tpl" elementName=start_date}<span>{$form.end_date.label} {include file="CRM/common/jcalendar.tpl" elementName=end_date}</span><br />
<span class="description">{ts}If this relationship has start and/or end dates, specify them here.{/ts}</span></td>
<td>{$form.start_date.html} {$form.end_date.label} {$form.end_date.html}<br /><span class="description">{ts}If this relationship has start and/or end dates, specify them here.{/ts}</span></td>
</tr>
<tr class="crm-relationship-form-block-description">
<td class="label">{$form.description.label}</td>
Expand Down
2 changes: 2 additions & 0 deletions xml/schema/Contact/Relationship.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<add>1.1</add>
<html>
<type>Select Date</type>
<formatType>activityDate</formatType>
</html>
</field>
<field>
Expand All @@ -88,6 +89,7 @@
<add>1.1</add>
<html>
<type>Select Date</type>
<formatType>activityDate</formatType>
</html>
</field>
<field>
Expand Down

0 comments on commit 425d606

Please sign in to comment.