| 1 |
<?php |
| 2 |
namespace LWS\WOOREWARDS\Ui\Editlists; |
| 3 |
|
| 4 |
// don't call the file directly |
| 5 |
if( !defined( 'ABSPATH' ) ) exit(); |
| 6 |
|
| 7 |
/** manage form with several screens */ |
| 8 |
abstract class MultiFormList extends \LWS\Adminpanel\EditList\Source |
| 9 |
{ |
| 10 |
const ROW_ID = 'post_id'; |
| 11 |
|
| 12 |
protected $pool = null; |
| 13 |
|
| 14 |
public function getEditCapability(): string |
| 15 |
{ |
| 16 |
return 'manage_rewards'; |
| 17 |
} |
| 18 |
|
| 19 |
/** @return array of key => category. category is an array with [label, color, icon] |
| 20 |
* for key @see Event::getCategories() */ |
| 21 |
abstract protected function getGroups(); |
| 22 |
/// @return an array of Event|Unlockable instances |
| 23 |
abstract protected function loadChoices(); |
| 24 |
/** @return string|array with step information |
| 25 |
* icon : step icon |
| 26 |
* title : step title */ |
| 27 |
abstract protected function getStepInfo(); |
| 28 |
|
| 29 |
/** @param \LWS\WOOREWARDS\Core\Pool|null $pool */ |
| 30 |
function __construct($pool=null) |
| 31 |
{ |
| 32 |
$this->pool = $pool; |
| 33 |
} |
| 34 |
|
| 35 |
public function defaultValues() |
| 36 |
{ |
| 37 |
$values = array(); |
| 38 |
foreach( $this->loadChoices()->asArray() as $choice ) |
| 39 |
$values = array_merge($values, $choice->getData()); |
| 40 |
|
| 41 |
return array_merge($values, array( |
| 42 |
self::ROW_ID => '', // it is important that id is reset and first for javascript purpose |
| 43 |
'wre_type' => '' |
| 44 |
)); |
| 45 |
} |
| 46 |
|
| 47 |
protected function getHiddenInputs() |
| 48 |
{ |
| 49 |
$rowId = static::ROW_ID; |
| 50 |
return "<input type='hidden' name='{$rowId}' class='lws_woorewards_system_id' />"; |
| 51 |
} |
| 52 |
|
| 53 |
protected function getGroupTitles() |
| 54 |
{ |
| 55 |
return array( |
| 56 |
array( |
| 57 |
'idle' => __("First Step", 'woorewards-lite'), |
| 58 |
'selected' => __("Step : ", 'woorewards-lite') |
| 59 |
), |
| 60 |
array( |
| 61 |
'idle' => __("Second Step", 'woorewards-lite'), |
| 62 |
'selected' => __("Step : ", 'woorewards-lite') |
| 63 |
) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** radio-grid */ |
| 68 |
protected function optionGroups() |
| 69 |
{ |
| 70 |
$groups = $this->getGroups(); |
| 71 |
if( !isset($groups['']) ) |
| 72 |
$groups[''] = array('label'=>'', 'color' => false, 'icon' => false); |
| 73 |
foreach( $groups as &$group ) |
| 74 |
{ |
| 75 |
$group['items'] = array(); |
| 76 |
if( !$group['color'] ) $group['color'] = '#a9a9a9'; |
| 77 |
if( !$group['icon'] ) $group['icon'] = 'lws-icon-show-more'; |
| 78 |
if( !isset($group['descr']) ) $group['descr'] = ''; |
| 79 |
} |
| 80 |
|
| 81 |
foreach( $this->loadChoices()->asArray() as $choice ) |
| 82 |
{ |
| 83 |
$sort = ''; |
| 84 |
foreach( $choice->getCategories() as $cat => $name ) |
| 85 |
{ |
| 86 |
if( isset($groups[$cat]) ) |
| 87 |
{ |
| 88 |
$sort = $cat; |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
$type = \esc_attr($choice->getType()); |
| 93 |
$info = $choice->getInformation(); |
| 94 |
if( !$info['color'] ) $info['color'] = $groups[$sort]['color']; |
| 95 |
if( !$info['icon'] ) $info['icon'] = $groups[$sort]['icon']; |
| 96 |
$groups[$sort]['items'][$type] = $info; |
| 97 |
} |
| 98 |
|
| 99 |
return $groups; |
| 100 |
} |
| 101 |
|
| 102 |
protected function groupsToRadioGrid($groups) |
| 103 |
{ |
| 104 |
$html = ''; |
| 105 |
$groupTitles = $this->getGroupTitles(); |
| 106 |
$maingrid = ''; |
| 107 |
$itemsgrids = ''; |
| 108 |
$colors = array(); |
| 109 |
foreach( $groups as $key => $group ) |
| 110 |
{ |
| 111 |
if( !$group['items'] ) continue; |
| 112 |
|
| 113 |
$colorstring = " style='" . \lws_get_theme_colors('--radiogrid-group-color', $group['color']) . "'"; |
| 114 |
$maingrid .= "<div class='radiogrid-item main lws_radiobutton_radio'{$colorstring} data-key='{$key}'>"; |
| 115 |
$maingrid .= "<div class='inner-background'></div>"; |
| 116 |
$maingrid .= "<div class='icon {$group['icon']}'></div>"; |
| 117 |
$maingrid .= "<div class='label'>{$group['label']}</div>"; |
| 118 |
$maingrid .= "<div class='descr'>{$group['descr']}</div>"; |
| 119 |
$maingrid .= "</div>"; |
| 120 |
|
| 121 |
$itemsgrids .= "<div class='lws_woorewards_system_type_choices radiogrid-container big-items hidden'{$colorstring}' data-key='{$key}'>"; |
| 122 |
foreach( $group['items'] as $type => $info ) |
| 123 |
{ |
| 124 |
$info['group'] = $group['label']; |
| 125 |
$colors[$type] = $colorstring; |
| 126 |
$data = \base64_encode(\json_encode($info)); |
| 127 |
$itemsgrids .= "<div class='radiogrid-item lws_radiobutton_radio lws_wre_system_selector_item'{$colorstring} value='{$type}' data-info='{$data}' tabindex='0'>" |
| 128 |
. "<div class='inner-background'></div>" |
| 129 |
. "<div class='icon lws-icons {$info['icon']}'></div>" |
| 130 |
. "<div class='label'>{$info['label']}</div>" |
| 131 |
. "<div class='descr'>{$info['short']}</div>" |
| 132 |
. "</div>"; |
| 133 |
} |
| 134 |
$itemsgrids .= "</div>"; |
| 135 |
} |
| 136 |
$html = "<div class='lws-editlist-group-wrapper first_group canfold'>" |
| 137 |
. "<div class='group-header-line'>" |
| 138 |
. "<div class='header' data-selected='{$groupTitles[0]['selected']}' data-idle='{$groupTitles[0]['idle']}'>{$groupTitles[0]['idle']}</div>" |
| 139 |
. "<div class='fold-button'></div>" |
| 140 |
. "</div>" |
| 141 |
. "<div class='radiogrid-container big-items'>" |
| 142 |
. $maingrid |
| 143 |
. "</div>" |
| 144 |
. "</div>" |
| 145 |
. "<div class='lws-editlist-group-wrapper second_group canfold hidden'>" |
| 146 |
. "<div class='group-header-line'>" |
| 147 |
. "<div class='header' data-selected='{$groupTitles[1]['selected']}' data-idle='{$groupTitles[1]['idle']}'>{$groupTitles[1]['idle']}</div>" |
| 148 |
. "<div class='fold-button'></div>" |
| 149 |
. "</div>" |
| 150 |
. $itemsgrids |
| 151 |
. "</div>"; |
| 152 |
return array( |
| 153 |
'html' => $html, |
| 154 |
'colors' => $colors, |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
/** no edition, use bulk action */ |
| 159 |
function input() |
| 160 |
{ |
| 161 |
\wp_enqueue_script('lws_wre_system_selector'); |
| 162 |
\wp_enqueue_style('lws_wre_system_selector'); |
| 163 |
|
| 164 |
$divs = array(); |
| 165 |
$uppergroups = $this->groupsToRadioGrid($this->optionGroups()); |
| 166 |
foreach( $this->loadChoices()->asArray() as $choice ) |
| 167 |
{ |
| 168 |
if (null != $this->pool) { |
| 169 |
$choice->setPool($this->pool); |
| 170 |
} |
| 171 |
$type = \esc_attr($choice->getType()); |
| 172 |
$colorstring = $uppergroups['colors'][$type]; |
| 173 |
$divs[] = "<div data-type='$type' class='lws_woorewards_system_choice editlist-content-grid hidden $type'$colorstring>" |
| 174 |
. $choice->getForm('editlist') |
| 175 |
. "</div>"; |
| 176 |
} |
| 177 |
|
| 178 |
$divs = implode("\n\t", $divs); |
| 179 |
$hiddens = $this->getHiddenInputs(); |
| 180 |
$stepInfo = $this->getStepInfo(); |
| 181 |
return "<div class='lws-woorewards-system-edit lws_woorewards_system_master'>" |
| 182 |
. $hiddens |
| 183 |
. "<input class='lws_woorewards_system_type' name='wre_type' type='hidden'>" |
| 184 |
. $uppergroups['html'] |
| 185 |
. "<div class='lws_woorewards_system_screens lws-editlist-group-wrapper third_group canfold hidden'>" |
| 186 |
. "<div class='group-header-line'>" |
| 187 |
. "<div class='header'>{$stepInfo}</div>" |
| 188 |
. "<div class='fold-button'></div>" |
| 189 |
. "</div>" |
| 190 |
. $divs |
| 191 |
. "</div>" |
| 192 |
. "</div>"; |
| 193 |
} |
| 194 |
} |