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

N°3366 - Multiple target state on approbal-extended #7

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
83 changes: 59 additions & 24 deletions datamodel.combodo-approval-extended.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@
<default_value/>
<is_null_allowed>true</is_null_allowed>
</field>
<field id="target_class" xsi:type="AttributeClass">
<class_category>bizmodel</class_category>
<sql>target_class</sql>
<default_value/>
<is_null_allowed>false</is_null_allowed>
</field>
<field id="target_class_state" xsi:type="AttributeString">
<sql>target_class_state</sql>
<default_value/>
<is_null_allowed>false</is_null_allowed>
</field>
<field id="level1_rule" xsi:type="AttributeOQL">
<sql>level1_rule</sql>
<default_value/>
Expand Down Expand Up @@ -200,15 +211,21 @@
<item id="fieldset:ApprovalRule:baseinfo">
<rank>10</rank>
<items>
<item id="name">
<rank>10</rank>
</item>
<item id="description">
<rank>20</rank>
</item>
<item id="coveragewindow_id">
<rank>30</rank>
</item>
<item id="name">
<rank>10</rank>
</item>
<item id="description">
<rank>20</rank>
</item>
<item id="target_class">
<rank>30</rank>
</item>
<item id="target_class_state">
<rank>40</rank>
</item>
<item id="coveragewindow_id">
<rank>50</rank>
</item>
</items>
</item>
</items>
Expand Down Expand Up @@ -279,9 +296,15 @@
<item id="description">
<rank>20</rank>
</item>
<item id="coveragewindow_id">
<rank>30</rank>
</item>
<item id="target_class">
<rank>30</rank>
</item>
<item id="target_class_state">
<rank>40</rank>
</item>
<item id="coveragewindow_id">
<rank>50</rank>
</item>
</items>
</search>
<list>
Expand Down Expand Up @@ -342,7 +365,14 @@
</attributes>
</reconciliation>
</properties>
<fields/>
<fields>
<field id="target_class_state" xsi:type="AttributeString">
<sql>target_class_state</sql>
<class_field>obj_class</class_field>
<default_value/>
<is_null_allowed>false</is_null_allowed>
</field>
</fields>
<methods>
<method id="GetApprovalScheme">
<static>true</static>
Expand All @@ -355,32 +385,35 @@
'first_reject' => ApprovalScheme::EXIT_ON_FIRST_REJECT,
'first_approve' => ApprovalScheme::EXIT_ON_FIRST_APPROVE,
);
if ((get_class($oObject) != 'UserRequest'))
{

$aAllowedClasses = MetaModel::GetConfig()->GetModuleSetting('combodo-approval-extended', 'targets', ApprovalConfiguration::DEFAULT_TARGET);
if (array_key_exists(get_class($oObject), $aAllowedClasses) === false) {
Comment on lines +389 to +390
Copy link

@Hipska Hipska Sep 18, 2024

Choose a reason for hiding this comment

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

It would make more sense later in the code when this var is named differently..

Suggested change
$aAllowedClasses = MetaModel::GetConfig()->GetModuleSetting('combodo-approval-extended', 'targets', ApprovalConfiguration::DEFAULT_TARGET);
if (array_key_exists(get_class($oObject), $aAllowedClasses) === false) {
$aTargets = MetaModel::GetConfig()->GetModuleSetting('combodo-approval-extended', 'targets', ApprovalConfiguration::DEFAULT_TARGET);
if (array_key_exists(get_class($oObject), $aTargets) === false) {

return null;
}
$sTargetState = MetaModel::GetConfig()->GetModuleSetting('combodo-approval-extended', 'target_state', 'new');
if ($sReachingState != $sTargetState)
{

$aTargetState = $aAllowedClasses[get_class($oObject)]['target_states'];
if (in_array($sReachingState, $aTargetState) === false) {
return null;
}

$sOQL = 'SELECT ApprovalRule AS ar JOIN ServiceSubcategory AS sc ON sc.approvalrule_id = ar.id WHERE sc.id = :servicesubcategory';
$sOQL = 'SELECT ApprovalRule AS ar JOIN ServiceSubcategory AS sc ON sc.approvalrule_id = ar.id WHERE sc.id = :servicesubcategory AND ar.target_class = :target_class AND ar.target_class_state = :target_class_state';
$oApprovalRuleSet = new DBObjectSet(
DBObjectSearch::FromOQL($sOQL),
array(),
array('servicesubcategory' => $oObject->Get('servicesubcategory_id'))
);
[],
['servicesubcategory' => $oObject->Get('servicesubcategory_id'), 'target_class' => get_class($oObject), 'target_class_state' => $sReachingState]);

if ($oApprovalRuleSet->count() == 0)
{
// No approval rule applies to the current object
return null;
}

$bReusePreviousAnswers = MetaModel::GetConfig()->GetModuleSetting('combodo-approval-extended', 'reuse_previous_answers', true);
$bReusePreviousAnswers = $aAllowedClasses[get_class($oObject)][ 'reuse_previous_answers'];

$oApprovalRule = $oApprovalRuleSet->fetch();
$oScheme = new ExtendedApprovalScheme();
$oScheme->Set('target_class_state', $sReachingState);
$oScheme->Set('started_state', $sReachingState);

$bStepAdded = false;

Expand Down Expand Up @@ -551,7 +584,9 @@
return false;
}

$sAllowedProfiles = MetaModel::GetConfig()->GetModuleSetting('combodo-approval-extended', 'bypass_profiles', 'Administrator, Service Manager');
$aAllowedClasses = MetaModel::GetConfig()->GetModuleSetting('combodo-approval-extended', 'targets', ApprovalConfiguration::DEFAULT_TARGET);

$sAllowedProfiles = $aAllowedClasses[$this->Get('obj_class')]['bypass_profiles'];
$aAllowed = array();
foreach (explode(',', $sAllowedProfiles) as $sProfileRaw)
{
Expand Down
36 changes: 25 additions & 11 deletions main.combodo-approval-extended.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@
* @author Denis Flaven <[email protected]>
* @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
*/

class ApprovalConfiguration
{
const DEFAULT_TARGET = [
'UserRequest' => [
'target_states' => ['new'],
'bypass_profiles' => 'Service Manager',
'reuse_previous_answers' => true,
]
];
}
class ApprovalComputeWorkingHours implements iWorkingTimeComputer
{
public static function GetDescription()
Expand Down Expand Up @@ -51,17 +60,22 @@ class HideButtonsPlugin implements iApplicationUIExtension
{
public function OnDisplayProperties($oObject, WebPage $oPage, $bEditMode = false)
{
if ( (get_class($oObject) == 'UserRequest' ) && ( $oObject->IsNew()) )
$aAllowedClasses = MetaModel::GetConfig()->GetModuleSetting('combodo-approval-extended', 'targets',ApprovalConfiguration::DEFAULT_TARGET);
if (array_key_exists(get_class($oObject), $aAllowedClasses) && MetaModel::HasLifecycle(get_class($oObject)))
{
$oSet = new DBObjectSet(new DBObjectSearch('ApprovalRule'));
$iCount = $oSet->Count();
if ($iCount > 0)
{
$oPage->add_ready_script(
<<<EOF
$('button.action[name="next_action"]').hide();
EOF
);
$sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObject));
$sTargetState = $oObject->Get($sStateAttCode);

if (in_array($sTargetState, $aAllowedClasses[get_class($oObject)][ 'target_states']) ) {
$sOQL = 'SELECT ApprovalRule AS ar JOIN ServiceSubcategory AS sc ON sc.approvalrule_id = ar.id WHERE ar.target_class = :target_class AND ar.target_class_state = :target_state';
$oApprovalRuleSet = new DBObjectSet( DBObjectSearch::FromOQL($sOQL), [], ['target_class' => get_class($oObject), 'target_states' => $sTargetState]);
if ($oApprovalRuleSet->Count() > 0) {
$oPage->add_ready_script(
<<<JS
$('button.action[name="next_action"]').hide();
JS
);
}
}
}
}
Expand Down
42 changes: 34 additions & 8 deletions module.combodo-approval-extended.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

SetupWebPage::AddModule(
__FILE__, // Path to the current file, all other file names are relative to the directory containing this file
'combodo-approval-extended/1.4.2',
'combodo-approval-extended/1.5.0-dev',
array(
// Identification
//
Expand Down Expand Up @@ -56,12 +56,17 @@
'doc.manual_setup' => '', // hyperlink to manual setup documentation, if any
'doc.more_information' => '', // hyperlink to more information, if any

'settings' => array(
// Module specific settings go here, if any
'target_state' => 'new',
'bypass_profiles' => 'Administrator, Service Manager',
'reuse_previous_answers' => true
),
'settings' =>
[
'targets' => [
'UserRequest' =>
[
'target_states' => ['new'],
'bypass_profiles' => 'Service Manager',
'reuse_previous_answers' => true,
],
],
],
)
);

Expand All @@ -73,7 +78,28 @@ class ApprovalExtendedInstaller extends ModuleInstallerAPI
{
public static function BeforeWritingConfig(Config $oConfiguration)
{
// If you want to override/force some configuration values, do it here
if (empty(utils::GetConfig()->GetModuleSetting('combodo-approval-extended', 'targets', null))
&& !empty(utils::GetConfig()->GetModuleSetting('combodo-approval-extended', 'target_states', null) )) {
// Migration of the old configuration to the new one
$newConfiguration = [
'UserRequest' => [
'target_states' => [$oConfiguration->GetModuleSetting('combodo-approval-extended', 'target_states', null) ],
'bypass_profiles' =>$oConfiguration->GetModuleSetting('combodo-approval-extended', 'bypass_profiles', 'Service Manager') ,
'reuse_previous_answers' => $oConfiguration->GetModuleSetting('combodo-approval-extended', 'reuse_previous_answers', true) ,
]
];
Comment on lines +84 to +90
Copy link

@Hipska Hipska Sep 18, 2024

Choose a reason for hiding this comment

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

Still some indentation problem, a typo and to keep backward compatibility, the admin profile should also be included when migrating.

Suggested change
$newConfiguration = [
'UserRequest' => [
'target_states' => [$oConfiguration->GetModuleSetting('combodo-approval-extended', 'target_states', null) ],
'bypass_profiles' =>$oConfiguration->GetModuleSetting('combodo-approval-extended', 'bypass_profiles', 'Service Manager') ,
'reuse_previous_answers' => $oConfiguration->GetModuleSetting('combodo-approval-extended', 'reuse_previous_answers', true) ,
]
];
$newConfiguration = [
'UserRequest' => [
'target_states' => [$oConfiguration->GetModuleSetting('combodo-approval-extended', 'target_state', 'new')],
'bypass_profiles' => $oConfiguration->GetModuleSetting('combodo-approval-extended', 'bypass_profiles', 'Administrator, Service Manager'),
'reuse_previous_answers' => $oConfiguration->GetModuleSetting('combodo-approval-extended', 'reuse_previous_answers', true),
]
];

$oConfiguration->SetModuleSetting('combodo-approval-extended', 'targets', $newConfiguration);
}
// Replacing old conf parameters value to indicate that it is obsolete
$aParamsToRemove = array('target_states', 'bypass_profiles', 'reuse_previous_answers');
foreach($aParamsToRemove as $sParamToRemove)
{
$sParamCurrentValue = $oConfiguration->GetModuleSetting('combodo-approval-extended', $sParamToRemove, null);
if(!empty($sParamCurrentValue))
{
$oConfiguration->SetModuleSetting('combodo-approval-extended', $sParamToRemove, 'No longer used, you can remove this parameter.');
}
}
Comment on lines +93 to +102
Copy link

Choose a reason for hiding this comment

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

I would remove this section, since you said this: https://github.com/Combodo/combodo-approval-extended/pull/7/files#r1763440755

return $oConfiguration;
}

Expand Down