Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
public_html
/
admin_new2
/
app
/
V2
/
Resources
/
Filename :
RewardResource.php
back
Copy
<?php namespace App\V2\Resources; use App\Http\Controllers\Api\SPWalletController; use App\Libraries\Helpers; use App\Models\Product; use App\Models\Store; use App\Models\StoreProduct; use App\Models\User; use App\V2\Dtos\FilterRewardDTO; use Illuminate\Support\Facades\Log; /** * Transforms Reward data for API responses */ class RewardResource extends BaseResource { private $spWalletController; public function __construct() { $this->spWalletController = new SPWalletController(); } public function fromRewardsToArray($rewards, $should_paginate = false, $load_full_details = false, User $currentUser = null, $load_button_details = false, Store $currentStore = null, ? FilterRewardDTO $filterRewardDto = null) { $rewards_arr_paginated = $rewards->toArray(); $rewards_arr = []; foreach($rewards AS $reward){ $rewards_arr[] = $this->fromRewardToArray($reward, $load_full_details, $currentUser, $load_button_details, $currentStore); } $rewards_arr_paginated['data'] = $rewards_arr; if($should_paginate) return $rewards_arr_paginated; else return $rewards_arr; } public function fromRewardToArray($reward, $load_full_details = false, User $currentUser = null, $load_button_details = false, Store $currentStore = null, ? FilterRewardDTO $filterRewardDto = null) { $reward_arr = $reward->toArray(); $reward_arr['added_in_store'] = $reward->getStoreProduct() != null ? true : false; $reward_arr['store'] = $reward->getStoreProduct(); $short_summary = []; if( $reward->product_sp > 0) $short_summary[] = $reward->product_sp . " Scholarship Points"; if($reward->getAssociatedTask() != null) $short_summary[] = "Task"; $reward_arr['short_summary'] = implode(" & ", $short_summary); $reward_arr['task_id'] = $reward->getAssociatedTask() != null ? $reward->getAssociatedTask()->task_id : null; //TODO: implement show to children // $reward_arr['show_to_children'] = $reward->created_by != -1 ? true : $reward_arr['added_in_store'] ; $reward_arr['show_to_children'] = $this->isShowToChildrenEnabled($reward, $currentStore); if($load_full_details){ $expires_in_str = $reward->expires_on ? Helpers::expiresInStrForHumans($reward->expiresOnYmd) : null; $repeate_after_days_in_str = $reward->productSetting != null && $reward->productSetting->repeate_after_days ? "Claim Every ". Helpers::daysToHumanCalenderTerm($reward->productSetting->repeate_after_days) : null; if($currentStore != null && $currentUser != null){ Product::populateRewardAdditionalAttributes($reward, $currentUser, $currentStore); $reward_arr['claim_status'] = $reward['claim_status']; } $reward_arr['task'] = $reward->getAssociatedTask(); $reward_arr['category'] = $reward->category; $reward_arr['images'] = $reward->images; $reward_arr['expires_in_str'] = $expires_in_str; $reward_arr['repeate_after_days_in_str'] = $repeate_after_days_in_str; $reward_arr['added_in_wishlist'] =$reward->getProductWishlist() != null ? true : false; } $reward_arr['category_name'] = $reward->category ? $reward->category->product_category_name : ''; $reward_arr['custom_fields'] = []; if( $filterRewardDto != null && $filterRewardDto->getRequiredFor() == 'PARENT' && isset($reward->customFields) && count($reward->customFields) > 0){ foreach($reward->customFields AS $customField){ $custom_field_arr = []; $custom_field_arr['field_type'] = $customField->field_type; $custom_field_arr['field_name'] = $customField->field_name; $custom_field_arr['field_label'] = $customField->field_label; $custom_field_arr['data'] = $customField->data; $reward_arr['custom_fields'][] = $custom_field_arr; } } $reward_arr['readonly'] = $this->isReadonly($reward); if($load_button_details) $reward_arr['buttons'] = $this->getButtonsDetails($currentUser, $reward); return $reward_arr; } private function isShowToChildrenEnabled(Product $reward, Store $currentStore){ $storeProduct = null; if($currentStore != null) { $storeProduct = StoreProduct::where('product_id', $reward->product_id) ->where('store_id', $currentStore->store_id) ->withTrashed() ->latest('created_at') ->first(); } if($storeProduct != null ){ if($storeProduct->is_published == 1){ return true; } else { return false; } } else { if($reward->rewardSetting != null){ if( in_array( $reward->rewardSetting->visible_to, ['GLOBAL' ,'GLOBAL_FOR_PARENT_AND_CHILD', 'ZIPCODE_PARENT_AND_CHILD', 'COUNTRY_PARENT_AND_CHILD'])){ return true; } } } return false; } private function isReadonly(Product $reward){ if( $reward->rewardSetting != null && in_array($reward->rewardSetting->visible_to, ['GLOBAL','GLOBAL_FOR_PARENT','GLOBAL_FOR_PARENT_AND_CHILD'])){ return true; } else if( $reward->created_by == null || $reward->created_by == -1){ return true; } return false; } public function getButtonsDetails(User $currentUser, Product $reward) { $buttons_arr = []; $claimButtom = new AppButtonResource(true, "Claim this reward"); $buyButton = new AppButtonResource(true, "Buy with {$reward->product_sp} SP"); //check active/inactive $productSetting = $reward->productSetting; if($productSetting != null){ if(in_array($productSetting->requirement_for_claim, ['SP', 'SP_AND_TASK', 'SP_OR_TASK'])){ //Check if enough balance is present? if($currentUser != null && $this->spWalletController->canDebitSP($currentUser, $reward->product_sp)) { //User can purchase the reward } else { $buyButton->setActive(false); $buyButton->setText("Need {$reward->product_sp} SP to buy"); } $buttons_arr['purchase'] = $buyButton->toArray(); } if(in_array($productSetting->requirement_for_claim, ['TASK', 'SP_AND_TASK', 'SP_OR_TASK']) && $reward->getAssociatedTask() != null){ $buttons_arr['claim'] = $claimButtom->toArray(); } } else { $buttons_arr['purchase'] = $buyButton->toArray(); $buttons_arr['claim'] = $claimButtom->toArray(); } return $buttons_arr; } public function toShortArray($rewards) { $rewards_arr_paginated = $rewards->toArray(); $new_rewards_arr = []; foreach($rewards AS $reward){ $short_summary = []; if( $reward->product_sp > 0) $short_summary[] = $reward->product_sp . " Scholarship Points"; if($reward->tasks->count() > 0) $short_summary[] = "Task"; $new_reward_arr = []; $new_reward_arr['product_id'] = $reward->product_id; $new_reward_arr['product_name'] = $reward->product_name; $new_reward_arr['description'] = $reward->description; $new_reward_arr['short_summary'] = implode(" & ", $short_summary); $new_reward_arr['product_image'] = $reward->product_image; $new_reward_arr['added_in_store'] = $reward->getStoreProduct() != null ? true : false; $new_rewards_arr[] = $new_reward_arr; } $rewards_arr_paginated['data'] = $new_rewards_arr; return $rewards_arr_paginated; } } ?>