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

[REF] Update Product Create to use hooks and also switch the manage p… #20822

Merged
merged 1 commit into from
Sep 7, 2021
Merged
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
4 changes: 3 additions & 1 deletion CRM/Contribute/BAO/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static function setIsActive($id, $is_active) {
*/
public static function create($params) {
$id = $params['id'] ?? NULL;
$op = !empty($id) ? 'edit' : 'create';
if (empty($id)) {
$defaultParams = [
'id' => $id,
Expand All @@ -90,7 +91,7 @@ public static function create($params) {
];
Copy link
Member

Choose a reason for hiding this comment

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

I know this is a pre-existing condition and not a blocker for this PR, but I really hate this array of defaults. They should be set in the schema. Also, is_active should default to TRUE for consistency with every other entity.

$params = array_merge($defaultParams, $params);
}

CRM_Utils_Hook::pre($op, 'Product', $id, $params);
// Modify the submitted values for 'image' and 'thumbnail' so that we use
// local URLs for these images when possible.
if (isset($params['image'])) {
Expand All @@ -104,6 +105,7 @@ public static function create($params) {
$premium = new CRM_Contribute_DAO_Product();
$premium->copyValues($params);
$premium->save();
CRM_Utils_Hook::post($op, 'Product', $id, $premium);
return $premium;
}

Expand Down
9 changes: 5 additions & 4 deletions CRM/Contribute/Form/ManagePremiums.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Civi\Api4\Product;

/**
* This class generates form components for Premiums.
*/
Expand All @@ -35,8 +37,7 @@ public function getDefaultEntity() {
public function setDefaultValues() {
$defaults = parent::setDefaultValues();
if ($this->_id) {
$params = ['id' => $this->_id];
CRM_Contribute_BAO_Product::retrieve($params, $tempDefaults);
$tempDefaults = Product::get()->addWhere('id', '=', $this->_id)->execute()->first();
if (isset($tempDefaults['image']) && isset($tempDefaults['thumbnail'])) {
$defaults['imageUrl'] = $tempDefaults['image'];
$defaults['thumbnailUrl'] = $tempDefaults['thumbnail'];
Expand Down Expand Up @@ -291,10 +292,10 @@ public function postProcess() {
$this->_processImages($params);

// Save the premium product to database
$premium = CRM_Contribute_BAO_Product::create($params);
$premium = Product::save()->addRecord($params)->execute()->first();

CRM_Core_Session::setStatus(
ts("The Premium '%1' has been saved.", [1 => $premium->name]),
ts("The Premium '%1' has been saved.", [1 => $premium['name']]),
ts('Saved'), 'success');
}

Expand Down
1 change: 1 addition & 0 deletions CRM/Core/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ public static function getEntityActionPermissions() {
],
];
$permissions['line_item'] = $permissions['contribution'];
$permissions['product'] = $permissions['contribution'];

$permissions['financial_item'] = $permissions['contribution'];

Expand Down