Skip to content

Commit

Permalink
[REF] Update Product Create to use hooks and also switch the manage p…
Browse files Browse the repository at this point in the history
…roduct/premium page to use APIv4 Product Entity

Update to use suggestion from Coleman

Set Product permissions to be the same as contribution entity
  • Loading branch information
seamuslee001 committed Sep 6, 2021
1 parent 6979ce6 commit 7e49aa9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
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) {
];
$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

0 comments on commit 7e49aa9

Please sign in to comment.