| 1 |
<?php |
| 2 |
namespace LWS\WOOREWARDS\Ui\Editlists; |
| 3 |
|
| 4 |
// don't call the file directly |
| 5 |
if( !defined( 'ABSPATH' ) ) exit(); |
| 6 |
|
| 7 |
/** Edit point earning amouts and the way to get them. |
| 8 |
* Tips: prevent page nav with EditList::setPageDisplay(false) */ |
| 9 |
class EventList extends \LWS\WOOREWARDS\Ui\Editlists\MultiFormList |
| 10 |
{ |
| 11 |
protected $stepInfo = false; |
| 12 |
protected $choices = false; |
| 13 |
|
| 14 |
function labels() |
| 15 |
{ |
| 16 |
$labels = array( |
| 17 |
'earning' => array(__("Earned points", 'woorewards-lite'), 'max-content'), |
| 18 |
'title' => __("Public title", 'woorewards-lite'), |
| 19 |
'description' => __("Action to perform", 'woorewards-lite') |
| 20 |
); |
| 21 |
return \apply_filters('lws_woorewards_eventlist_labels', $labels); |
| 22 |
} |
| 23 |
|
| 24 |
function read($limit=null) |
| 25 |
{ |
| 26 |
$events = array(); |
| 27 |
foreach( $this->pool->getEvents()->asArray() as $event ) |
| 28 |
{ |
| 29 |
$events[] = $this->objectToArray($event); |
| 30 |
} |
| 31 |
return $events; |
| 32 |
} |
| 33 |
|
| 34 |
private function objectToArray($item) |
| 35 |
{ |
| 36 |
$class = 'number'; |
| 37 |
$label = $item->getGainRaw(true); |
| 38 |
$earning = \esc_attr(\wp_kses($label, array())); |
| 39 |
if ('=' == substr($item->getGainRaw(false), 0, 1)) { |
| 40 |
$class = 'formula'; |
| 41 |
$label = __("Formula", 'woorewards-lite'); |
| 42 |
} |
| 43 |
|
| 44 |
return array_merge( |
| 45 |
array( |
| 46 |
self::ROW_ID => $item->getId(), // it is important that id is first for javascript purpose |
| 47 |
'wre_type' => $item->getType(), |
| 48 |
'earning' => "<div class='lws-wr-event-multiplier {$class}' title='{$earning}'>{$label}</div>", |
| 49 |
'title' => "<div class='lws-wr-event-title'>" . $item->getTitle() . "</div><div class='lws-wr-event-id'>ID : " . $item->getId() . "</div>", |
| 50 |
'description' => $item->getDescription() |
| 51 |
), |
| 52 |
$item->getData() |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
function getPopupTitle() |
| 57 |
{ |
| 58 |
return __("Action to perform to earn points", 'woorewards-lite'); |
| 59 |
} |
| 60 |
|
| 61 |
protected function getStepInfo() |
| 62 |
{ |
| 63 |
if (!$this->stepInfo) { |
| 64 |
$this->stepInfo = __("Points value and options", 'woorewards-lite'); |
| 65 |
} |
| 66 |
return $this->stepInfo; |
| 67 |
} |
| 68 |
|
| 69 |
protected function getGroupTitles() |
| 70 |
{ |
| 71 |
return array( |
| 72 |
array( |
| 73 |
'idle' => __("Select a category", 'woorewards-lite'), |
| 74 |
'selected' => __("Category : ", 'woorewards-lite') |
| 75 |
), |
| 76 |
array( |
| 77 |
'idle' => __("Select an action", 'woorewards-lite'), |
| 78 |
'selected' => __("Action : ", 'woorewards-lite') |
| 79 |
) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
protected function loadChoices() |
| 84 |
{ |
| 85 |
if (false === $this->choices) |
| 86 |
{ |
| 87 |
$blacklist = $this->pool->getOption('blacklist'); |
| 88 |
if( !\LWS\Adminpanel\Tools\Conveniences::isWC() ) |
| 89 |
$blacklist = array_merge(array('woocommerce'=>'woocommerce'), is_array($blacklist)?$blacklist:array()); |
| 90 |
|
| 91 |
$this->choices = \LWS\WOOREWARDS\Collections\Events::instanciate()->create()->byCategory( |
| 92 |
$blacklist, |
| 93 |
$this->pool->getOption('whitelist'), |
| 94 |
$this->pool->getEvents()->getTypes() |
| 95 |
)->usort(function($a, $b){return strcmp($a->getDisplayType(), $b->getDisplayType());}); |
| 96 |
} |
| 97 |
return $this->choices; |
| 98 |
} |
| 99 |
|
| 100 |
static function getChoiceCategories() |
| 101 |
{ |
| 102 |
$dftIcon = 'lws-icon-c-pulse'; |
| 103 |
return \apply_filters('lws_woorewards_system_item_type_groups', array( |
| 104 |
'order' => array('label' => _x("Orders", "Option Group", 'woorewards-lite'), 'descr' => __("Customers earn points when they place an order", 'woorewards-lite'), 'color' => '#cc1d25', 'icon' => 'lws-icon-cart-2'), |
| 105 |
'site' => array('label' => _x("Website", "Option Group", 'woorewards-lite'), 'descr' => __("Users earn points by registering or posting content", 'woorewards-lite'), 'color' => '#0e97af', 'icon' => 'lws-icon-home-3'), |
| 106 |
'social' => array('label' => _x("Social Media", "Option Group", 'woorewards-lite'), 'descr' => __("Users earn points for sharing on social media", 'woorewards-lite'), 'color' => '#0136a7', 'icon' => 'lws-icon-network-communication'), |
| 107 |
'sponsorship' => array('label' => _x("Referral", "Option Group", 'woorewards-lite'), 'descr' => __("Referrers earn points when they refer someone", 'woorewards-lite'), 'color' => '#7801a7', 'icon' => 'lws-icon-handshake'), |
| 108 |
'miscellaneous' => array('label' => _x("Miscellaneous", "Option Group", 'woorewards-lite'), 'descr' => __("Users earn points for various reasons", 'woorewards-lite'), 'color' => '#a70190', 'icon' => 'lws-icon-c-pulse'), |
| 109 |
'woovip' => array('label' => _x("WooVIP", "Option Group", 'woorewards-lite'), 'descr' => __("Earn points related to the WooVIP Plugin", 'woorewards-lite'), 'color' => '#c79648', 'icon' => 'lws-icon-crown'), |
| 110 |
'woovirtualwallet' => array('label' => _x("WooVirtualWallet", "Option Group", 'woorewards-lite'), 'descr' => __("Earn points related to the WooVirtualWallet Plugin", 'woorewards-lite'), 'color' => '#cd7627', 'icon' => $dftIcon), |
| 111 |
), 'event'); |
| 112 |
} |
| 113 |
|
| 114 |
protected function getGroups() |
| 115 |
{ |
| 116 |
return self::getChoiceCategories(); |
| 117 |
} |
| 118 |
|
| 119 |
function write($row) |
| 120 |
{ |
| 121 |
$item = null; |
| 122 |
$type = (is_array($row) && isset($row['wre_type'])) ? trim($row['wre_type']) : ''; |
| 123 |
if( is_array($row) && isset($row[self::ROW_ID]) && !empty($id = intval($row[self::ROW_ID])) ) |
| 124 |
{ |
| 125 |
$item = $this->pool->getEvents()->find($id); |
| 126 |
if( empty($item) ) |
| 127 |
return new \WP_Error('404', __("The selected Earning Points System cannot be found.", 'woorewards-lite')); |
| 128 |
if( $type != $item->getType() ) |
| 129 |
return new \WP_Error('403', __("Earning Points System Type cannot be changed. Delete this and create a new one instead.", 'woorewards-lite')); |
| 130 |
} |
| 131 |
else if( !empty($type) ) |
| 132 |
{ |
| 133 |
$item = \LWS\WOOREWARDS\Collections\Events::instanciate()->create($type)->last(); |
| 134 |
if( empty($item) ) |
| 135 |
return new \WP_Error('404', __("The selected Earning Points System type cannot be found.", 'woorewards-lite')); |
| 136 |
} |
| 137 |
|
| 138 |
if( !empty($item) ) |
| 139 |
{ |
| 140 |
if( true === ($err = $item->submit($row)) ) |
| 141 |
{ |
| 142 |
$item->save($this->pool); |
| 143 |
return $this->objectToArray($item); |
| 144 |
} |
| 145 |
else |
| 146 |
return new \WP_Error('update', $err); |
| 147 |
} |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
function erase($row) |
| 152 |
{ |
| 153 |
if( is_array($row) && isset($row[self::ROW_ID]) && !empty($id = intval($row[self::ROW_ID])) ) |
| 154 |
{ |
| 155 |
$item = $this->pool->getEvents()->find($id); |
| 156 |
if( empty($item) ) |
| 157 |
{ |
| 158 |
return new \WP_Error('404', __("The selected Earning Point System cannot be found.", 'woorewards-lite')); |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
$this->pool->removeEvent($item); |
| 163 |
$item->delete(); |
| 164 |
return true; |
| 165 |
} |
| 166 |
} |
| 167 |
return false; |
| 168 |
} |
| 169 |
} |