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

[ready-for-core-team-review]CRM-19804, added pre and post hook invokation for FinancialAccount #9584

Merged
merged 1 commit into from
Dec 30, 2016
Merged
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
16 changes: 16 additions & 0 deletions CRM/Financial/BAO/FinancialAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ public static function add(&$params) {

// action is taken depending upon the mode
$financialAccount = new CRM_Financial_DAO_FinancialAccount();

// invoke pre hook
$op = 'create';
if (!empty($params['id'])) {
$op = 'edit';
}
CRM_Utils_Hook::pre($op, 'FinancialAccount', CRM_Utils_Array::value('id', $params), $params);

if (!empty($params['id'])) {
$financialAccount->id = $params['id'];
$financialAccount->find(TRUE);
Expand All @@ -128,6 +136,14 @@ public static function add(&$params) {
$financialAccount->opening_balance = $financialAccount->current_period_opening_balance = '0.00';
}
$financialAccount->save();

// invoke post hook
$op = 'create';
if (!empty($params['id'])) {
$op = 'edit';
}
Copy link
Member

Choose a reason for hiding this comment

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

@pradpnayak isn't this a repetition? I mean can we not initialise the $op parameter once ? and need optimisation
$op = empty($params['id']) ? 'create' : 'edit';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think one can change the $params values in pre hook i.e they might unset($params['id']) when $op == 'edit' which will end up creating new entry in db rather update and hence post hook should invoke as $op == 'create'.

$op = empty($params['id']) ? 'create' : 'edit';
I was told by one of member from core team in past to use if () condition rather ternary operators. If this pattern is allowed i happy to optimize it and use it in future :)

Copy link
Member

Choose a reason for hiding this comment

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

ahh yes .. u're right

CRM_Utils_Hook::post($op, 'FinancialAccount', $financialAccount->id, $financialAccount);

return $financialAccount;
}

Expand Down