Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Added Resource Owner #148

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CRM/Admin/Form/ResourceConfigOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function buildQuickForm($check = FALSE) {
$this->add('text', 'label', ts('Label'), array('size' => 50, 'maxlength' => 255), TRUE);
$this->add('text', 'price', ts('Price'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'price '), TRUE);
$this->add('text', 'max_size', ts('Max Size'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'max_size '), TRUE);
$this->addEntityRef('owner_id', ts('Select Resource Owner'));
$this->add('text', 'weight', ts('Weight'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'weight'), TRUE);
$this->add('checkbox', 'is_active', ts('Enabled?'));

Expand Down
11 changes: 11 additions & 0 deletions CRM/Admin/Page/ResourceConfigOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ function browse($action = NULL) {
CRM_Core_DAO::storeValues($dao, $configOptions[$dao->id]);
$configOptions[$dao->id]['unit'] = CRM_Utils_Array::value(CRM_Utils_Array::value('unit_id', $configOptions[$dao->id]), $units);

if ($dao->owner_id > 0) {
$configOptions[$dao->id]['owner'] = civicrm_api3('Contact', 'getvalue', array(
'sequential' => 1,
'return' => "display_name",
'id' => $dao->owner_id,
));
}
else {
$configOptions[$dao->id]['owner'] = '';
}

// form all action links
$action = array_sum(array_keys($this->links()));

Expand Down
10 changes: 10 additions & 0 deletions CRM/Booking/DAO/ResourceConfigOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ static function &fields()
'dataPattern' => '',
'export' => true,
) ,
'owner_id' => array(
'name' => 'owner_id',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Owner Id') ,
'import' => true,
'where' => 'civicrm_booking_resource_config_option.owner_id',
'headerPattern' => '',
'dataPattern' => '',
'export' => true,
) ,
);
}
return self::$_fields;
Expand Down
6 changes: 5 additions & 1 deletion CRM/Booking/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ public function upgrade_1100() {
return TRUE;
}


public function upgrade_1510() {
$this->ctx->log->info('Applying update 1510');
$this->executeSqlFile('sql/upgrade_1510.sql');
return TRUE;
}

/**
* Example: Run a couple simple queries
Expand Down
36 changes: 23 additions & 13 deletions booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,33 @@ function booking_civicrm_entityTypes(&$entityTypes) {
* Implementation of hook_civicrm_merge
*/
function booking_civicrm_merge ( $type, &$data, $mainId = NULL, $otherId = NULL, $tables = NULL ){
if (!empty($mainId) && !empty($otherId) && $type == 'sqls'){
if (!empty($mainId) && !empty($otherId)){
if ($type == 'sqls') {
$query1 = "
UPDATE civicrm_booking
SET primary_contact_id=$mainId
WHERE primary_contact_id=$otherId;
";
$query2 = "
UPDATE civicrm_booking
SET secondary_contact_id=$mainId
WHERE secondary_contact_id=$otherId;
";

require_once('CRM/Core/DAO.php');
$dao = CRM_Core_DAO::executeQuery( $query1 );
$dao = CRM_Core_DAO::executeQuery( $query2 );
}

$query1 = "
UPDATE civicrm_booking
SET primary_contact_id=$mainId
WHERE primary_contact_id=$otherId;
";
$query2 = "
UPDATE civicrm_booking
SET secondary_contact_id=$mainId
WHERE secondary_contact_id=$otherId;
$updateResourceOwnerQuery = "
UPDATE civicrm_booking_resource_config_option
SET owner_id=$mainId
WHERE owner_id=$otherId;
";

require_once('CRM/Core/DAO.php');
$dao = CRM_Core_DAO::executeQuery( $query1 );
$dao = CRM_Core_DAO::executeQuery( $query2 );
$dao = CRM_Core_DAO::executeQuery( $updateResourceOwnerQuery );

echo "merge hook thrown\n";
}
}

Expand Down
18 changes: 18 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" bootstrap="tests/phpunit/bootstrap.php">
<testsuites>
<testsuite name="My Test Suite">
<directory>./tests/phpunit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./</directory>
</whitelist>
</filter>
<listeners>
<listener class="Civi\Test\CiviTestListener">
<arguments/>
</listener>
</listeners>
</phpunit>
3 changes: 2 additions & 1 deletion sql/civibooking_install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ CREATE TABLE `civicrm_booking_resource_config_option` (
`unit_id` varchar(512) COMMENT 'The unit associated with this config option. Implicit FK to option_value row in booking_size_unit option_group.',
`weight` int unsigned NOT NULL ,
`is_active` tinyint DEFAULT 1 ,
`is_deleted` tinyint DEFAULT 0
`is_deleted` tinyint DEFAULT 0,
`owner_id` int(10) unsigned
,
PRIMARY KEY ( `id` )

Expand Down
5 changes: 5 additions & 0 deletions sql/upgrade_1510.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE `civicrm_booking_resource_config_option`
ADD `owner_id` int(10) unsigned
DEFAULT NULL
COMMENT 'Add an owner id for resources of type contact.'
AFTER `is_deleted`
5 changes: 4 additions & 1 deletion templates/CRM/Admin/Form/ResourceConfigOption.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
{$form.unit_id.html}
</td>
</tr>
<tr class="crm-resource-configuration-option-form-block-weight">
<tr class="crm-resource-configuration-option-form-block-owner">
<td class="label">{$form.owner_id.label}</td><td>{$form.owner_id.html}</td>
</tr>
<tr class="crm-resource-configuration-option-form-block-weight">
<td class="label">{$form.weight.label}</td><td>{$form.weight.html}</td>
</tr>
<tr class="crm-resource-configuration-option-form-block-weight">
Expand Down
4 changes: 3 additions & 1 deletion templates/CRM/Admin/Page/ResourceConfigOption.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<th >{ts}Price{/ts}</th>
<th >{ts}Max Size{/ts}</th>
<th >{ts}Unit{/ts}</th>
<th >{ts}Owner{/ts}</th>
<th >{ts}Weight{/ts}</th>
<th >{ts}Enabled?{/ts}</th>
<th ></th>
Expand All @@ -47,6 +48,7 @@
<td class="crm-booking-resource-description">{$row.price}</td>
<td class="crm-booking-resource-type">{$row.max_size}</td>
<td class="crm-booking-resource-location">{$row.unit}</td>
<td class="crm-booking-resource-location">{$row.owner}</td>
<td class="crm-booking-resource-weight">{$row.weight}</td>
<td id="row_{$row.id}_status" class="crm-booking-resource-is_active">{if $row.is_active eq 1} {ts}Yes{/ts} {else} {ts}No{/ts} {/if}</td>
<td>{$row.action|replace:'xx':$row.id}</td>
Expand All @@ -67,7 +69,7 @@
{ts}There are no resources configuration option.{/ts}
</div>
<div class="action-link">
<a href="{crmURL p='civicrm/admin/resource/config_set/config_option' q="action=add&sid=$sid&reset=1"}" id="newResourceConfigOption" class="button"><span><div class="icon add-icon"></div>{ts}Add Resource Configruation Option{/ts}</span></a>
<a href="{crmURL p='civicrm/admin/resource/config_set/config_option' q="action=add&sid=$sid&reset=1"}" id="newResourceConfigOption" class="button"><span><div class="icon add-icon"></div>{ts}Add Resource Configuration Option{/ts}</span></a>
</div>
{/if}
{/if}
77 changes: 77 additions & 0 deletions tests/phpunit/CRM/Booking/HookTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;

/**
* FIXME - Add test description.
*
* Tips:
* - With HookInterface, you may implement CiviCRM hooks directly in the test class.
* Simply create corresponding functions (e.g. "hook_civicrm_post(...)" or similar).
* - With TransactionalInterface, any data changes made by setUp() or test****() functions will
* rollback automatically -- as long as you don't manipulate schema or truncate tables.
* If this test needs to manipulate schema or truncate tables, then either:
* a. Do all that using setupHeadless() and Civi\Test.
* b. Disable TransactionalInterface, and handle all setup/teardown yourself.
*
* @group headless
*/
class CRM_Booking_HookTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {

public function setUpHeadless() {

// Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
// See: https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
return \Civi\Test::headless()
->installMe(__DIR__)
->apply();
}

public function setUp() {
parent::setUp();
}

public function tearDown() {
parent::tearDown();
}

public function testMergeResourceOwner() {
$mainOwner = civicrm_api3('Contact', 'create', array(
'contact_type' => 'organization',
'organization_name' => 'Main Owner',
));

$otherOwner = civicrm_api3('Contact', 'create', array(
'contact_type' => 'organization',
'organization_name' => 'Other Owner',
));

$resourceConfigSet = civicrm_api3('ResourceConfigSet', 'create', array(
'sequential' => 1,
'title' => "test resource config set",
'weight' => 1,
));

civicrm_api3('ResourceConfigOption', 'create', array(
'sequential' => 1,
'owner_id' => $otherOwner['id'],
'set_id' => $resourceConfigSet['id'],
'label' => "Test",
'price' => "5.00",
'weight' => 1,
));

$merger = new CRM_Dedupe_Merger();

$pairs = array(array('dstID' => $mainOwner['id'], 'srcID' => $otherOwner['id']));

$merger::merge($pairs);

// Check that ownership of the resource config option has move to the main owner.
$this->assertEquals(1, civicrm_api3('ResourceConfigOption', 'getcount', array(
'owner_id' => $mainOwner['id'],
)));
}
}
49 changes: 49 additions & 0 deletions tests/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

ini_set('memory_limit', '2G');
ini_set('safe_mode', 0);
eval(cv('php:boot --level=classloader', 'phpcode'));

/**
* Call the "cv" command.
*
* @param string $cmd
* The rest of the command to send.
* @param string $decode
* Ex: 'json' or 'phpcode'.
* @return string
* Response output (if the command executed normally).
* @throws \RuntimeException
* If the command terminates abnormally.
*/
function cv($cmd, $decode = 'json') {
$cmd = 'cv ' . $cmd;
$descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR);
$oldOutput = getenv('CV_OUTPUT');
putenv("CV_OUTPUT=json");
$process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__);
putenv("CV_OUTPUT=$oldOutput");
fclose($pipes[0]);
$result = stream_get_contents($pipes[1]);
fclose($pipes[1]);
if (proc_close($process) !== 0) {
throw new RuntimeException("Command failed ($cmd):\n$result");
}
switch ($decode) {
case 'raw':
return $result;

case 'phpcode':
// If the last output is /*PHPCODE*/, then we managed to complete execution.
if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") {
throw new \RuntimeException("Command failed ($cmd):\n$result");
}
return $result;

case 'json':
return json_decode($result, 1);

default:
throw new RuntimeException("Bad decoder format ($decode)");
}
}