Skip to content

Commit

Permalink
working version
Browse files Browse the repository at this point in the history
  • Loading branch information
elabx committed Jan 17, 2023
1 parent c8213ff commit 9331103
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 145 deletions.
47 changes: 35 additions & 12 deletions FieldtypeRecurringDates.module
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use RRule\RRule;
class FieldtypeRecurringDates extends FieldtypeMulti
{


public static function getModuleInfo()
{
return array(
Expand All @@ -24,6 +25,7 @@ class FieldtypeRecurringDates extends FieldtypeMulti
);
}

const defaultLimit = 10;
const EXTRAS_TABLE_NAME_SUFFIX = '_extras';
const EXTRAS_TABLE_COLS = [
'pages_id',
Expand All @@ -40,7 +42,7 @@ class FieldtypeRecurringDates extends FieldtypeMulti
{
$this->set('usePagination', true);
$this->set('useOrderByCols', true);
require_once(__DIR__ . '/RecurringDatesSettings.php');
require_once(__DIR__ . '/RecurringDateSettings.php');
require_once(__DIR__ . '/RecurringDate.php');
require_once(__DIR__ . '/Occurrence.php');
require_once(__DIR__ . '/OccurrenceArray.php');
Expand All @@ -50,14 +52,32 @@ class FieldtypeRecurringDates extends FieldtypeMulti
public function init()
{

$this->addHook('/fieldtype-recurring-dates/get-dates/', function ($event) {
});
$this->addHook('/fieldtype-recurring-dates/get-dates/', $this, 'hookGetDates');
$this->addHookAfter('Fields::added', $this, 'hookAfterFieldAdded');
$this->addHookAfter('Fields::deleted', $this, 'hookAfterFieldDeleted');
$this->addHookAfter('Fields::deleted', $this, 'hookAfterFieldDeleted');
$this->addHookBefore('Pages::deleted', $this, 'hookAfterPageDelete');
}

public function hookGetDates($event){

if(!$event->user->isLoggedin()) return false;

$input = $event->input;
$page = $input->get->int('id');
$field = $input->get->int('field_id');
$start = $input->get->int('start');
$limit = $input->get->int('limit');

if(!$page || !$field) return "{}";

$field_name = $event->fields->get($field)->name;
$page = $event->pages->get($page);
$value = $page->$field_name("start=$start, limit=$limit, sort=data");
//bd($value);
return $value->occurrences;
}

/**
* Adds the rrule settings table
*
Expand Down Expand Up @@ -207,18 +227,20 @@ class FieldtypeRecurringDates extends FieldtypeMulti
$recurring_date = $this->getBlankValue($page, $field);
$recurring_date->settings = json_encode($recurring_date->settings, true);
if (empty($value) || !is_array($value)) return $recurring_date;

bd($value);
if ($this->isSettingsValue($value)) {
$recurring_date->settings = $value['settings'];
$rrule_value = json_decode($value['rrule'], true);
$rrule = new RRule($rrule_value);
$recurring_date->rrule = $rrule;
} else {
$settings = $this->getSettings($page, $field);
$recurring_date->settings = $settings->settings;
$rrule_value = json_decode($settings->rrule, true);
$rrule = new RRule($rrule_value);
$recurring_date->rrule = $rrule;
if($settings) {
$recurring_date->settings = $settings->settings;
$rrule_value = json_decode($settings->rrule, true);
$rrule = new RRule($rrule_value);
$recurring_date->rrule = $rrule;
}

if (isset($value['_pagination_limit'])) {
$recurring_date->occurrences->setLimit($value['_pagination_limit']);
Expand Down Expand Up @@ -263,15 +285,15 @@ class FieldtypeRecurringDates extends FieldtypeMulti
//bd($page);
if (!$value instanceof RecurringDate) return $sleepValue;
// make the events sort by date ascending
$value->occurrences->sort('date');
// $value->occurrences->sort('date');

// convert each Event to an array within sleepValue
foreach ($value->occurrences as $occurrence) {
// if no date specified then skip it
if (!$occurrence->date) continue;
// if($occurrence->formatted) throw new WireException('Formatted events cannot be saved');
$sleepValue[] = array(
'data' => $occurrence->date, // note: 'date' is becoming 'data' (with an 'a')
'data' => $occurrence, // note: 'date' is becoming 'data' (with an 'a')
'excluded' => $occurrence->excluded
);
}
Expand All @@ -292,7 +314,7 @@ class FieldtypeRecurringDates extends FieldtypeMulti

public function ___markupValue(Page $page, Field $field, $value = null, $property = '')
{
if (!$value instanceof RecurrentD) $value = $page->get($field->name);
/*if (!$value instanceof RecurrentD) $value = $page->get($field->name);
if (!$value instanceof EventArray || !$value->count()) return '';
$a = [];
foreach ($value as $event) {
Expand All @@ -302,7 +324,8 @@ class FieldtypeRecurringDates extends FieldtypeMulti
$a[] = $this->sanitizer->entities1("$event->date");
}
}
return "<ul><li>" . implode("</li><li>", $a) . "</li></ul>";
return "<ul><li>" . implode("</li><li>", $a) . "</li></ul>";*/
return $value;
}

public function getMonths()
Expand Down
76 changes: 68 additions & 8 deletions InputfieldRecurringDates.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,8 @@ document.addEventListener('alpine:init', (e) => {
},
_settings: null,
settings: null,
is_filtering: function (filter) {
if (this.rrule[filter] !== null || this.rrule[filter] !== undefined) {
if (this.rrule[filter].length) {
return true;
}
}
},
show_table: false,

show_table: true,
catalogues: {
filters: [
{label: "Months", value: 'BYMONTH'},
Expand All @@ -64,9 +58,29 @@ document.addEventListener('alpine:init', (e) => {
{name: 'Saturday', value: 'SAT'},
],
},
data: {
dates: [],
pagination: {
start: 0,
limit: null,
total: null,
sort: 'ascending',
pagination_string: '',
markup_pager: null,
}
},

init: function () {
this.inputfield = this.$el.dataset.inputfieldName;
this.pageId = parseInt(this.$el.dataset.pageId);
this.fieldId = parseInt(this.$el.dataset.fieldId)
this.data.pagination.limit = this.$el.dataset.inputfieldLimit;
this.updateEventList();

/*this.$watch('data.pagination', (prop) => {
this.updateEventList();
});*/

this.$watch('rrule', (prop) => {
this.saveString();
});
Expand Down Expand Up @@ -104,6 +118,52 @@ document.addEventListener('alpine:init', (e) => {
this.settings.limit_mode = "count";
}
},


updateEventList: function () {
var url = new URL('fieldtype-recurring-dates/get-dates/', window.origin);
var params = {
id: this.pageId,
field_id: this.fieldId,
limit: this.data.pagination.limit,
start: this.data.pagination.start
}
console.log(params);
url.search = new URLSearchParams(params).toString();

fetch(url, {
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => {
if (!response.ok) alert(`Something went wrong: ${response.status} - ${response.statusText}`)
return response.json()
})
.then(response => {

this.data = response;
});
},

previousPage(){
this.data.pagination.start -= this.data.pagination.limit
this.updateEventList()
},
nextPage(){
this.data.pagination.start += this.data.pagination.limit
this.updateEventList()
},

is_filtering: function (filter) {
if (this.rrule[filter] !== null || this.rrule[filter] !== undefined) {
if (this.rrule[filter].length) {
return true;
}
}
},


cloneObject: function (obj) {
// basic type deep copy
if (obj === null || obj === undefined || typeof obj !== 'object') {
Expand Down
18 changes: 17 additions & 1 deletion InputfieldRecurringDates.module
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class InputfieldRecurringDates extends Inputfield
public function init()
{
// Load Alpine.js in <header>
$this->setAttribute('pageSize', 10);
$this->modules->AlpineJS;
}

public function ___render()
{
/** @var RecurringDate $recurring_dates */
$recurring_dates = $this->value;
bd($recurring_dates);
$this->setAttribute('class', $this->getAttribute('class') . ' main-input uk-width-1-1');

$occurrences = $recurring_dates->occurrences;
Expand Down Expand Up @@ -75,6 +75,9 @@ class InputfieldRecurringDates extends Inputfield
return $fieldtype->markupValue($this->hasPage, $this->hasField, $this->value);
}

public function __getEventsUrl(){

}

public function ___processInput(WireInputData $input)
{
Expand Down Expand Up @@ -124,4 +127,17 @@ class InputfieldRecurringDates extends Inputfield
return $this;

}

public function ___getConfigInputfields() {
// Get the defaults and $inputfields wrapper we can add to
$inputfields = parent::___getConfigInputfields();
// Add a new Inputfield to it
$f = $this->modules->get('InputfieldInteger');
$f->attr('name', 'pageSize');
$f->label = 'Page size';
$f->value = $this->pageSize;
$inputfields->add($f);

return $inputfields;
}
}
13 changes: 7 additions & 6 deletions Occurrence.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@
class Occurrence extends WireData {

/**
* Construct a new Event
* Construct a new occurrence
*
*/
public function __construct() {
// define the fields that represent our event (and their default/blank values)
$this->set('date', '');
$this->set('excluded', false);
$this->set('formatted', false);
$this->set('format', 'Y-m-d');
parent::__construct();
}

/**
* Set a value to the event: date, location or notes
*
* @param string $key
* @param string $value
* @param DateTime $value
* @return WireData|self
*
*/
Expand All @@ -43,11 +44,11 @@ public function set($key, $value) {
if($key === 'excluded') {
$value = false;
}
if($key === 'date') {
/*if($key === 'date') {
if($value instanceof \DateTime){
$value = $value->format('Y-m-d H:i:s');
$value = $value->format($this->format);
}
}
}*/
return parent::set($key, $value);
}

Expand All @@ -62,6 +63,6 @@ public function formatDate($format = "U"){
*
*/
public function __toString() {
return "$this->date";
return $this->date->format($this->format);
}
}
50 changes: 30 additions & 20 deletions OccurrenceArray.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
<?php namespace ProcessWire;

/**
* FieldtypeEvents: RecurringDateArray
*
* Contains multiple occurrences of an RRule definition
*
*/
class OccurrenceArray extends PaginatedArray {
class OccurrenceArray extends PaginatedArray
{

/**
* Is given item valid to store in this EventArray?
*
* @param Event $item
* @return bool
*
* @return Occurrence $item
*/

public function makeBlankItem()
{
return $this->wire(new Occurrence());
}

public function isValidItem($item) {
public function isValidItem($item)
{
return $item instanceof Occurrence;
}

/**
* Make a string value to represent these events that can be used for comparison purposes
*
* @return string
*
* @return false|string
* @throws WireException
*/
public function __toString() {
$a = [];
foreach($this as $item) $a[] = (string) $item;
return implode("\n", $a);
public function __toString()
{
$pager = $this->modules->get("MarkupPagerNav");
$pager = $pager->render($this, [
'listClass' => 'uk-pagination MarkupPagerNav',
'linkMarkup' => "<a @click='setPage' data-item='{index}' href=''><span>{out}</span></a>",
]);
$a = [
'dates' => [],
'pagination' => [
'start' => $this->getStart(),
'limit' => $this->getLimit(),
'total' => $this->getTotal(),
'pagination_string' => $this->getPaginationString(),
'markup_pager' => $pager
]
];
foreach ($this->data as $item) $a['dates'][] = (string)$item;
//return implode("\n", $a);
bd($this);
return json_encode($a, true);

}
}
Loading

0 comments on commit 9331103

Please sign in to comment.