PMCORE-1459 Identify if a datetime field has dependencies in minDate, maxDate and defaultDate property

This commit is contained in:
Roly Rudy Gutierrez Pinto
2020-05-21 22:19:26 -04:00
committed by Fabio Guachalla
parent 19c4a93f33
commit ba43807f49
3 changed files with 1765 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -984,6 +984,64 @@ class PmDynaformTest extends TestCase
// Compare the values // Compare the values
$this->assertEquals($dynaformTitle, $dynaform->DYN_TITLE); $this->assertEquals($dynaformTitle, $dynaform->DYN_TITLE);
} }
/**
* This test should verify the setDependentOptionsForDatetime() method, to
* add the dependentOptions property to the datetime control.
* @test
* @covers PmDynaform::jsonr()
* @covers PmDynaform::setDependentOptionsForDatetime()
*/
public function it_should_test_dependent_options_for_datetime_control()
{
$pathData = PATH_TRUNK . "/tests/resources/dynaform1.json";
$data = file_get_contents($pathData);
$json = json_decode($data);
//assert for not contain property: dependentOptions
$result = json_decode(json_encode($json), JSON_OBJECT_AS_ARRAY);
$fn = function($item) use(&$fn) {
if (is_array($item)) {
if (isset($item['type']) && $item['type'] === 'datetime') {
$this->assertArrayNotHasKey('dependentOptions', $item);
}
array_map($fn, $item);
}
};
array_map($fn, $result);
//assert new property: dependentOptions
$dynaform = new PmDynaform();
$dynaform->jsonr($json);
$result = json_decode(json_encode($json), JSON_OBJECT_AS_ARRAY);
$fn = function($item) use(&$fn) {
if (is_array($item)) {
if (isset($item['type']) && $item['type'] === 'datetime') {
$this->assertArrayHasKey('dependentOptions', $item);
$this->assertArrayHasKey('minDate', $item['dependentOptions']);
$this->assertArrayHasKey('maxDate', $item['dependentOptions']);
$this->assertArrayHasKey('defaultDate', $item['dependentOptions']);
}
array_map($fn, $item);
}
};
array_map($fn, $result);
$dynaform = new PmDynaform();
$reflection = new ReflectionClass($dynaform);
$reflectionMethod = $reflection->getMethod('setDependentOptionsForDatetime');
$reflectionMethod->setAccessible(true);
$a = new stdClass();
$reflectionMethod->invokeArgs($dynaform, [&$a]);
$this->assertInstanceOf('ReflectionMethod', $reflectionMethod);
$a = new stdClass();
$a->type = 'suggest';
$reflectionMethod->invokeArgs($dynaform, [&$a]);
$this->assertInstanceOf('ReflectionMethod', $reflectionMethod);
}
} }
// Dummy function used for the coverture // Dummy function used for the coverture

View File

@@ -29,6 +29,7 @@ class PmDynaform
public $lang = SYS_LANG; public $lang = SYS_LANG;
public $translations = null; public $translations = null;
public $onPropertyRead = "onPropertyReadFormInstance"; public $onPropertyRead = "onPropertyReadFormInstance";
public $onAfterPropertyRead = "onAfterPropertyReadFormInstance";
public $pathRTLCss = ''; public $pathRTLCss = '';
public $record = null; public $record = null;
public $records = null; public $records = null;
@@ -545,6 +546,7 @@ class PmDynaform
if (isset($this->fields["APP_DATA"][$json->name . "_label"])) { if (isset($this->fields["APP_DATA"][$json->name . "_label"])) {
$json->data->label = $this->fields["APP_DATA"][$json->name . "_label"]; $json->data->label = $this->fields["APP_DATA"][$json->name . "_label"];
} }
$this->setDependentOptionsForDatetime($json, $this->fields);
} }
if ($key === "type" && ($value === "file") && isset($this->fields["APP_DATA"]["APPLICATION"])) { if ($key === "type" && ($value === "file") && isset($this->fields["APP_DATA"]["APPLICATION"])) {
$oCriteriaAppDocument = new Criteria("workflow"); $oCriteriaAppDocument = new Criteria("workflow");
@@ -771,6 +773,11 @@ class PmDynaform
} }
} }
} }
//read event after
$fn = $this->onAfterPropertyRead;
if (is_callable($fn) || function_exists($fn)) {
$fn($json, $key, $value);
}
} }
} }
} }
@@ -2489,4 +2496,38 @@ class PmDynaform
$json->dataSchema[$key] = $columnsData; $json->dataSchema[$key] = $columnsData;
} }
} }
/**
* Sets the dependentOptions property for datetime control, if it contains dependent fields.
* @param stdClass $json
* @param array $fields
* @return void
*/
private function setDependentOptionsForDatetime(stdClass &$json, array $fields = []): void
{
if (!isset($json->type)) {
return;
}
if ($json->type !== 'datetime') {
return;
}
$json->dependentOptions = '';
$backup = $this->onAfterPropertyRead;
$properties = [
'defaultDate' => $json->defaultDate,
'minDate' => $json->minDate,
'maxDate' => $json->maxDate
];
$this->onAfterPropertyRead = function(stdClass &$json, $key, $value) use($backup, $properties) {
if (isset($json->type) && $json->type === 'datetime' && $key === "dependentOptions") {
$json->dependentOptions = new stdClass();
foreach ($properties as $property => $value) {
if (is_string($value) && in_array(substr($value, 0, 2), self::$prefixs)) {
$json->dependentOptions->{$property} = $value;
}
}
$this->onAfterPropertyRead = $backup;
}
};
}
} }