| | |
| | | <?php |
| | | namespace JVBase\ui; |
| | | |
| | | use JVBase\base\Site; |
| | | use JVBase\managers\UserTermsManager; |
| | | use JVBase\meta\Form; |
| | | use JVBase\registrar\Registrar; |
| | |
| | | protected ?array $uploaderConfig = null; |
| | | |
| | | // Data |
| | | protected $dataSourceCallback = null; |
| | | protected array $templates = []; |
| | | |
| | | // UI Options |
| | |
| | | protected array $customDateRanges = []; |
| | | protected array $additionalClasses = []; |
| | | |
| | | protected Registrar $registrar; |
| | | protected ?Registrar $registrar; |
| | | public function __construct() { |
| | | $this->icon = jvbDefaultIcon(); |
| | | $this->user = wp_get_current_user(); |
| | |
| | | */ |
| | | public function content(string $type, string $singular, string $plural): self { |
| | | $this->dataType = $type; |
| | | $this->registrar = Registrar::getInstance($type); |
| | | $registrar = Registrar::getInstance($type); |
| | | if ($registrar) { |
| | | $this->registrar = Registrar::getInstance($type)??null; |
| | | $this->sections = $this->registrar->getSections(); |
| | | } |
| | | |
| | | $this->singular = $singular; |
| | | $this->plural = $plural; |
| | | return $this; |
| | |
| | | |
| | | protected function taxConfig(string $taxonomy, string $label = ''):array |
| | | { |
| | | $isVerified = jvbUserIsVerified(); |
| | | $isVerified = $this->userIsVerified(); |
| | | $label = ($label === '') ? Registrar::getInstance($taxonomy)->getPlural() : $label; |
| | | return [ |
| | | 'type' => 'taxonomy', |
| | |
| | | ]; |
| | | } |
| | | |
| | | protected function userIsVerified():bool |
| | | { |
| | | $membership = Site::membership(); |
| | | |
| | | return !($membership && $membership->has('member_verified')) || current_user_can('skip_moderation'); |
| | | } |
| | | |
| | | public function addSearch():self |
| | | { |
| | | $this->hasSearch = true; |
| | |
| | | } |
| | | |
| | | $this->timelineSharedFields = array_keys(array_filter($this->fields, function ($field) { |
| | | if (!array_key_exists('for_all', $field) || $field['for_all'] === false){ |
| | | if (!array_key_exists('for_all', $field) || $field['for_all'] === false || is_null($field['for_all'])){ |
| | | return true; |
| | | } |
| | | return false; |
| | |
| | | return false; |
| | | })); |
| | | |
| | | |
| | | $all = array_merge($this->timelineUniqueFields, $this->timelineSharedFields); |
| | | $this->nonTimelineFields = array_filter($this->fields, function ($field) use ($all) { |
| | | return !in_array($field, $all); |
| | |
| | | } |
| | | |
| | | /** |
| | | * Set the data source callback |
| | | * Callback should accept filters and return array of items |
| | | */ |
| | | public function dataSource(callable $callback): self { |
| | | $this->dataSourceCallback = $callback; |
| | | return $this; |
| | | } |
| | | |
| | | /** |
| | | * Add a custom template |
| | | */ |
| | | public function addTemplate(string $name, string $template): self { |
| | |
| | | protected function renderUploader(): void { |
| | | ?> |
| | | <details open class="uploader"> |
| | | <summary class="row btw"><?= esc_html($this->uploaderConfig['label'] ?? 'Upload Files') ?></summary> |
| | | <?php |
| | | echo Form::render( |
| | | 'new_' . $this->dataType, |
| | | '', |
| | | $this->uploaderConfig |
| | | ); |
| | | ?> |
| | | <summary class="row x-btw"><?= esc_html($this->uploaderConfig['label'] ?? 'Upload Files') ?></summary> |
| | | <form id="uploader" data-form-id="upload_new_<?=$this->dataType ?>"> |
| | | <?php |
| | | echo jvbFormRestore(); |
| | | echo Form::render( |
| | | 'new_' . $this->dataType, |
| | | '', |
| | | $this->uploaderConfig |
| | | ); |
| | | ?> |
| | | </form> |
| | | </details> |
| | | <?php |
| | | } |
| | |
| | | return; |
| | | } |
| | | ?> |
| | | <details class="all-filters col start" data-ignore> |
| | | <summary>Filters <button hidden data-action="clear-filters" data-ignore><?=jvbIcon('x')?><span>Clear Filters</span></span></button></summary> |
| | | <details class="all-filters col top" data-ignore> |
| | | <summary>Filters</summary> |
| | | <?php |
| | | |
| | | $this->renderSearch(); |
| | |
| | | ?> |
| | | <button data-action="refresh" data-ignore><?=jvbIcon('arrows-clockwise')?><span>Hard Refresh</span></span></button> |
| | | </details> |
| | | <button hidden data-action="clear-filters" data-ignore hidden><?=jvbIcon('x')?><span>Clear Filters</span></span></button> |
| | | <?php |
| | | } |
| | | |
| | |
| | | return; |
| | | } |
| | | ?> |
| | | <div class="search row start nowrap"> |
| | | <div class="search row left nowrap"> |
| | | <span class="label">Search:</span> |
| | | <?= jvbSearch() ?> |
| | | </div> |
| | |
| | | return; |
| | | } |
| | | ?> |
| | | <div class="radio-options view row"> |
| | | <div class="radio-options view row left"> |
| | | <span class="label">View:</span> |
| | | <?php |
| | | $views = [ |
| | |
| | | <label for="view-<?=$view?>" |
| | | title="<?=$views[$view]['label']?>"> |
| | | <?= jvbDashIcon($views[$view]['icon']) ?> |
| | | <span class="screen-reader-text"><?=$views[$view]['label']?></span> |
| | | <span class="label"><?=$views[$view]['label']?></span> |
| | | </label> |
| | | <?php |
| | | } |
| | |
| | | return; |
| | | } |
| | | ?> |
| | | <div class="radio-options status row"> |
| | | <div class="radio-options status row left"> |
| | | <span class="label">Status:</span> |
| | | <?php |
| | | $i = 1; |
| | |
| | | <input type="radio" class="btn" data-filter="status" value="<?=$status?>" name="status" id="<?=$status?>"<?=$checked?>> |
| | | <label for="<?=$status?>" title="<?=$config['label']?>"> |
| | | <?= jvbDashIcon($config['icon']) ?> |
| | | <span class="label"><?= $config['label'] ?></span> |
| | | </label> |
| | | <?php |
| | | $i++; |
| | |
| | | protected function renderOrderControls():void |
| | | { |
| | | ?> |
| | | <div class="radio-options order row btw w-full"> |
| | | <div class="radio-options order row x-btw w-full"> |
| | | <?php |
| | | $order = [ |
| | | 'orderby' => [ |
| | | 'date' => 'Order by date created', |
| | | 'alphabetical' => 'Order alphabetically' |
| | | [ |
| | | 'slug' => 'date', |
| | | 'label' => 'Date Created', |
| | | 'icon' => 'calendar' |
| | | ], |
| | | [ |
| | | 'slug' => 'alphabetical', |
| | | 'label' => 'Alphabetically', |
| | | 'icon' => 'alphabetical', |
| | | ], |
| | | [ |
| | | 'slug' => 'date_modified', |
| | | 'icon' => 'clock-clockwise', |
| | | 'label' => 'Date Modified', |
| | | ], |
| | | ], |
| | | 'order' => [ |
| | | 'sort-ascending' => 'In ascending order (Z-A, oldest to newest)', |
| | | 'sort-descending' => 'In descending order (A-Z, newest to oldest)' |
| | | [ |
| | | 'slug' => 'desc', |
| | | 'icon' => 'sort-descending', |
| | | 'label' => 'Descending (A-Z, 1-10)' |
| | | ], |
| | | [ |
| | | 'slug' => 'asc', |
| | | 'icon' => 'sort-ascending', |
| | | 'label' => 'Ascending (Z-A, 10-1)' |
| | | ] |
| | | ] |
| | | ]; |
| | | |
| | | foreach ($order as $o => $option) { |
| | | ?> |
| | | <div class="row start"> |
| | | <div class="row left"> |
| | | <span class="label"><?= ucfirst($o)?>:</span> |
| | | <?php |
| | | $title = $o === 'orderby' ? 'Order by ' : 'Sort '; |
| | | $i = 0; |
| | | foreach ($option as $opt => $label) { |
| | | $icon = $opt === 'date' ? 'calendar' : $opt; |
| | | $value = $opt; |
| | | $value = ($value === 'sort-ascending') ? 'asc' : $value; |
| | | $value = ($value === 'sort-descending') ? 'desc' : $value; |
| | | foreach ($option as $conf) { |
| | | ?> |
| | | <input id="<?=$opt?>" class="btn" type="radio" name="<?=$o?>" data-filter="<?=$o?>" value="<?=$value?>"<?=$i===0 ? ' checked':''?>> |
| | | <input id="<?=$o.'-'.$conf['slug'] ?>" class="btn" type="radio" name="<?=$o?>" data-filter="<?=$o?>" value="<?=$conf['slug']?>"<?=$i===0 ? ' checked':''?>> |
| | | |
| | | <label for="<?=$opt?>" title="<?=$label?>"><?=jvbDashIcon($icon)?></label> |
| | | <label for="<?=$o.'-'.$conf['slug']?>" title="<?=$title.' '.$conf['label']?>"><?=jvbDashIcon($conf['icon'])?><span class="label"><?= $conf['label'] ?></span></label> |
| | | <?php |
| | | $i++; |
| | | } |
| | |
| | | return; |
| | | } |
| | | ?> |
| | | <div class="filters row start"> |
| | | <div class="filters row left"> |
| | | <span class="label">Filters:</span> |
| | | <?php |
| | | foreach ($this->filters as $key => $config) { |
| | |
| | | foreach ($terms as $term) { |
| | | $out .= sprintf( |
| | | '<option value="%s">%s</option>', |
| | | esc_attr($term['term_id']), |
| | | esc_html($term['name']) |
| | | esc_attr(is_object($term) ? $term->term_id : $term['term_id']), |
| | | esc_html(is_object($term) ? $term->name : $term['name']) |
| | | ); |
| | | } |
| | | $out .= '</select></div>'; |
| | |
| | | */ |
| | | protected function getCommonTerms(string $taxonomy, ?string $limit = null):array { |
| | | if ($limit) { |
| | | if ($limit === 'user') { |
| | | if (Site::has('membership') && $limit === 'user') { |
| | | $manager = new UserTermsManager(); |
| | | return $manager->fetchUserTerms($this->user_id, $taxonomy); |
| | | } else { |
| | |
| | | ob_start(); |
| | | ?> |
| | | <details class="multi-select" title="Select columns" hidden> |
| | | <summary class="row start nowrap"> |
| | | <summary class="row left nowrap"> |
| | | <?= jvbDashIcon('columns') ?> |
| | | <span class="labels">Toggle Columns</span> |
| | | </summary> |
| | |
| | | return; |
| | | } |
| | | ?> |
| | | <div class="bulk-controls row nowrap btw"> |
| | | <div class="bulk-controls row nowrap x-btw"> |
| | | <div class="bulk-select"> |
| | | <input type="checkbox" id="select-all" class="select-all"> |
| | | <label for="select-all" class="row"><span>Select All</span><span class="selected-count" hidden></span></label> |
| | |
| | | }, ARRAY_FILTER_USE_KEY); |
| | | |
| | | echo '<template class="timelineItem">'; |
| | | echo Form::renderImagePreview(null, ['fields' => $temp]); |
| | | echo Form::renderImagePreview(null, $temp); |
| | | echo '</template>'; |
| | | } |
| | | if (!array_key_exists('empty', $templates)) { |
| | |
| | | } |
| | | ob_start(); |
| | | ?> |
| | | <div class="item-actions row btw abs"> |
| | | <div class="item-actions row x-btw abs"> |
| | | <?php |
| | | foreach ($this->itemActions as $action) { |
| | | $config = $this->defaultItemActions[$action]; |
| | |
| | | <div class="item <?=esc_attr($this->dataType)?> row nowrap"> |
| | | <?= $this->renderItemSelect()?> |
| | | <?=$this->renderImage() ?> |
| | | <div class="col start w-full"> |
| | | <div class="col top w-full"> |
| | | <h3 data-field="post_title"></h3> |
| | | <p data-attr="date"></p> |
| | | <p data-field="price"></p> |
| | |
| | | <td class="field show-<?= esc_attr($name) ?>" data-field="<?= esc_attr($name) ?>" data-field-type="<?=$config['type']?>"<?=(in_array($name, $this->stuck)) ? ' data-stuck':''?>> |
| | | <?php |
| | | if (in_array('edit', $this->caps)) { |
| | | echo $makeThisDetailed ? '<details><summary class="row btw">See Value</summary>' : ''; |
| | | echo $makeThisDetailed ? '<details><summary class="row x-btw">See Value</summary>' : ''; |
| | | if (in_array($config['type'], ['selector', 'taxonomy', 'post'])) { |
| | | $config['autocomplete'] = true; |
| | | } |
| | |
| | | $makeThisDetailed = (in_array($config['type'], $makeDetails)); |
| | | ?> |
| | | <td class="field show-<?= esc_attr($name) ?>" data-field="<?= esc_attr($name) ?>" data-field-type="<?=$config['type']?>"<?=(in_array($name, $this->stuck)) ? ' data-stuck':''?>> |
| | | <?= $makeThisDetailed ? '<details><summary class="row btw">See Value</summary>' : '' ?> |
| | | <?= $makeThisDetailed ? '<details><summary class="row x-btw">See Value</summary>' : '' ?> |
| | | <?php |
| | | if (in_array($config['type'], ['selector', 'taxonomy', 'post'])) { |
| | | $config['autocomplete'] = true; |
| | |
| | | $makeThisDetailed = (in_array($config['type'], $makeDetails)); |
| | | ?> |
| | | <td class="field show-<?= esc_attr($name) ?>" data-field="<?= esc_attr($name) ?>" data-field-type="<?=$config['type']?>"<?=(in_array($name, $this->stuck)) ? ' data-stuck':''?>> |
| | | <?= $makeThisDetailed ? '<details><summary class="row btw">See Value</summary>' : '' ?> |
| | | <?= $makeThisDetailed ? '<details><summary class="row x-btw">See Value</summary>' : '' ?> |
| | | <?= Form::render($name, '', $config); ?> |
| | | <?= $makeThisDetailed ? '</details>' : '' ?> |
| | | </td> |
| | |
| | | protected function renderTableActions(): string { |
| | | ob_start(); |
| | | ?> |
| | | <div class="table-actions row btw nowrap"> |
| | | <div class="table-actions row x-btw nowrap"> |
| | | <?php if (count(array_intersect(['create', 'edit'], $this->caps)) > 0) { ?> |
| | | <?= jvbRenderToggleTextField( |
| | | 'vertical', |
| | |
| | | echo Form::render('post_status', '', $this->getStatusFieldConfig('edit-')); |
| | | } |
| | | |
| | | |
| | | |
| | | if (!empty($this->sections)) { |
| | | $tabs = []; |
| | | foreach ($this->sections as $config) { |
| | |
| | | } |
| | | |
| | | $fields = $this->fields; |
| | | |
| | | if (!$this->isTimeline) { |
| | | $first = ['post_thumbnail', 'post_title', 'price']; |
| | | $first = $this->registrar->getType() === 'post' ? |
| | | ['post_thumbnail', 'post_title', 'price'] : |
| | | ( $this->registrar->getType() === 'term' ? |
| | | ['name', 'thumbnail', 'description'] : |
| | | ['username', 'display_name', 'description']); |
| | | |
| | | foreach ($first as $f) { |
| | | if (array_key_exists($f, $fields)) { |
| | |
| | | } |
| | | |
| | | if ($this->isTimeline) { |
| | | $temp = array_filter($fields, function ($field) { |
| | | return in_array($field, $this->timelineUniqueFields); |
| | | $temp = array_filter($fields, function ($field) use ($fields) { |
| | | return in_array($field, $this->timelineUniqueFields) && (!array_key_exists('hidden', $fields[$field]) || $fields[$field]['hidden'] === false); |
| | | }, ARRAY_FILTER_USE_KEY); |
| | | $config = [ |
| | | 'type' => 'upload', |
| | | 'subtype' => 'timeline', |
| | | 'data' => 'timeline', |
| | | 'multiple' => true, |
| | | 'limit' => 0, |
| | | 'data' => ['timeline'], |
| | | 'label' => 'Progression', |
| | | 'fields' => $temp |
| | | ]; |
| | |
| | | if (in_array($field['type'], ['taxonomy', 'selector'])) { |
| | | $field = array_merge($field, $this->taxConfig($field['taxonomy'], $field['label'])); |
| | | } |
| | | $content .= Form::render($slug, '', $field); |
| | | if (!array_key_exists('hidden', $field) || $field['hidden'] === false) { |
| | | $content .= Form::render($slug, '', $field); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | $content .= Form::render('timeline', '', $config); |
| | | $content .= Form::render('timeline_gallery', '', $config); |
| | | |
| | | $tabs['progression']['content'] = $content; |
| | | if ($tabs) { |
| | | $tabs['progression']['content'] = $content; |
| | | } else { |
| | | echo $content; |
| | | } |
| | | |
| | | $fields = $this->nonTimelineFields; |
| | | } |
| | | foreach ($fields as $n => $config) { |
| | |
| | | $out = ob_get_clean(); |
| | | echo Form::fieldWrap('post_status', $out, ['type'=>'group']); |
| | | } |
| | | |
| | | |
| | | public static function searchFilter():string |
| | | { |
| | | $self = new self(); |
| | | $self->hasSearch = true; |
| | | ob_start(); |
| | | $self->renderSearch(); |
| | | return ob_get_clean(); |
| | | } |
| | | |
| | | public static function viewFilter(array $views):string |
| | | { |
| | | $self = new self(); |
| | | $self->views = $views; |
| | | ob_start(); |
| | | $self->renderViewControls(); |
| | | return ob_get_clean(); |
| | | } |
| | | |
| | | public static function orderFilter(bool $random = false):string |
| | | { |
| | | $self = new self(); |
| | | ob_start(); |
| | | $self->renderOrderControls(); |
| | | return ob_get_clean(); |
| | | } |
| | | } |