Jump to content

Events Fieldtype & Inputfield (How to make a table Fieldtype/Inputfield)


ryan

Recommended Posts

thanks for that awesome module!
 
I like to extend the inputs with a checkbox:
 
 

<td><input type='checkbox' name='{$name}_ispublic[]' value='$ispublic' {$checked}/></td>
				

 
I already extend the db part and the event class, this works fine.  But i stuck with the inputfield checkbox...i dont get the values to the WireInputData variable. Can anyone help me?

Link to comment
Share on other sites

  • 1 month later...

Hi kongondo,

Sorry, I posted that in a hurry on the way out of the office for the day.

I need to import about 100 pages that have dates from another CMS. I have everything formatted, just not entirely sure how to work with a fieldtype that has multiple inputs from within a template (in this case is an admin tools template I use to import stuff). A repeater field is easier because it's just a page, so setting each of the fields is simple. I'm a little unclear on how to work with this fieldtype.

I just need a nudge in the right direction. :)

P.S. I think I prefer the other profile pic too. I'll switch it back tomorrow.

Link to comment
Share on other sites

$event = new Event();
$event->date = "1979-10-12 00:42:00";
$event->location = "Vogsphere";
$event->notes = "The homeworld of the Vogons";
$page->events->add($event);

That's one way to do it, at least. Since $page->events here is events field it returns an instance of EventArray, which in turn can contain multiple Event objects.

For the most part EventArray acts just like a PageArray or Pagefiles or any other object extending WireArray. This is just the most basic example of what you can do with it.

  • Like 7
Link to comment
Share on other sites

Hello,

Anyone tried setting an Events field visibility to "Locked, value visible but not editable"? After I did that, I am getting raw HTML:

<p><strong>2014-04-22</strong><br /><em>Moon</em><br />Moon party!</p>

Is there a way to have plain values displayed without getting into the module code?

Link to comment
Share on other sites

Hello,

Anyone tried setting an Events field visibility to "Locked, value visible but not editable"? After I did that, I am getting raw HTML:

<p><strong>2014-04-22</strong><br /><em>Moon</em><br />Moon party!</p>

Is there a way to have plain values displayed without getting into the module code?

Perhaps just use the get method for the fields you want and format them as needed? (definitely entity encode them at least)

Link to comment
Share on other sites

I never dive in into module development yet, but how hard it is to change one of the field into page field type? Cheers

Not hard, but you need to know what you are doing :-). Changes will be required in both modules - FieldtypeEvents.module and InputfieldEvents.module. Changes needed in the former are slightly more tricky since Fieldtypes describe and create database schemas. Looking at the code, currently the field 'data' that is required by all Fieldtypes is currently being used to store the values of the 'date' field as a unix time stamp. This would probably have to move to another column in the database, e.g. 'date' so that 'data' can instead hold the id of the selected page in your page field type. Changes to the latter are relatively easier. You would need to add a column to the events HTML table. You could then use the API to add, for example, a select field:

$f = $this->modules->get('InputfieldSelect');
//more field properties as required...

$opts = wire('pages')->find('template=hosts');

foreach($opts as $opt) {
		$f->addOption($opt->id, $opt->title);	
}

From here it is just a matter of $f->render() in the table column you added (the <td>), to get something like this:

post-894-0-06334900-1398332767_thumb.png

  • Like 4
Link to comment
Share on other sites

  • 4 weeks later...

Not hard, but you need to know what you are doing :-). Changes will be required in both modules - FieldtypeEvents.module and InputfieldEvents.module. Changes needed in the former are slightly more tricky since Fieldtypes describe and create database schemas. Looking at the code, currently the field 'data' that is required by all Fieldtypes is currently being used to store the values of the 'date' field as a unix time stamp. This would probably have to move to another column in the database, e.g. 'date' so that 'data' can instead hold the id of the selected page in your page field type. Changes to the latter are relatively easier. You would need to add a column to the events HTML table. You could then use the API to add, for example, a select field:

$f = $this->modules->get('InputfieldSelect');
//more field properties as required...

$opts = wire('pages')->find('template=hosts');

foreach($opts as $opt) {
		$f->addOption($opt->id, $opt->title);	
}

From here it is just a matter of $f->render() in the table column you added (the <td>), to get something like this:

Does it work? Can U send me files? Thank you!

Link to comment
Share on other sites

  • 2 weeks later...

Hello people  :)

I need a customized version of this nice module. Repeaters are, as far as I came, not the right one for this.

My customers, not really into computer and web things, have a courses calendar for their yoga school.

Each course, has a start and end date. In this time the course will be at different days and different times, that´s the point where I couldn´t get repeaters like I think it´s easiest.

Don´t want to have separate pages for each course, as long as it´s not absolutely necessary..

I was a bit afraid about tweaking events fieldtype because I´m not really a programmer, but because I didn´t saw another way I faced my fear haha ;)

It´s not properly styled right now, but it´s almost working. Biggest thing right now is that the dateend field is not saving at all.

So I´m writing in hope that someone can help me a little with this one and maybe has some tips because I could imagine there are better ways to achieve this.

I guess it´s because of the same field names, so I probably handle the storage process a little different?

At the end I will add a jquery timepicker. If it´s not possible or usefull to have start and end time in one field, I could split them of course, but that´s for later, when I figured out how to save the data properly.

Ah the data will be filled in this calendar  :)

Cheers Can

:-[

FieldtypeCourses.zip

Link to comment
Share on other sites

Can, not directly answering your situation, but have you seen the ProFields and the Table fieldtype there? If not, take a look: https://processwire.com/talk/store/product/10-profields/

Also PageTable field that ships with core (available on dev branch) might be great alternative (it does use pages, but it's much more "lightweight" in terms of UI compared to repeaters).

  • Like 1
Link to comment
Share on other sites

Does it work? Can U send me files? Thank you!

Sorry @zlojkashtan, just seen I didn't reply to this. I didn't take this further; I only tested quickly using the code I show in my post above. Maybe what you need can be achieved in a different way? See the post before this one...or tell us exactly what your needs are.

Link to comment
Share on other sites

Thanks for your quick reply apeisa.
I know ProFields and I really like them, especially the Table fieldtype and would really love to support PW.

The thing is that I´m not really into money at the moment and it´s more like a trade.

I know the customer a long time know and they are paying a domain and webspace for a private project

Maybe I could get the ProFields a bit later..but I need this one first.

You mean page fieldtype right?

Don´t really know how to achieve it with this one? Maybe you have a hint on this for me :)

In the "create new page" from the page field its only possible to set the title of multiple pages right, so they then need to go to those pages and add all other informations?

But maybe I´m missing something  ^_^

Link to comment
Share on other sites

PageTable is new kind of fieldtype, coming in 2.5, but already available at dev branch. I think it has best parts of page and repeater fields combined.

It allows creating and editing linked pages in modal, without leaving the page you are actually editing.

  • Like 1
Link to comment
Share on other sites

Regarding your enddate not saving, there are a couple of things going on.

InputfieldCourses.module:

Change:

$course->dateend = $dateend; 

to:

$course->dateend = $input->{"{$name}_dateend"}[$cnt];

Course.php

Change:

} else if($key == 'date') {

to:

} else if($key == 'date' || $key == 'dateend') {

That will get your dateend field saving on creation.

However, the one thing I just noticed is that you can't make any changes to the course once it is saved. I haven't investigated this yet, but did you notice that also?

EDIT:

Also those checkboxes are going to have issues - you need to a checked="checked" in each input based on whether there is a 1 in the database or not, but you'll also need to take care of assigning that "1".

Now for why changes are not saving - I am still not sure exactly why and have run out of time, but a quick and dirty fix is to comment out this line in InputfieldCourse.module:

if("$courses" != "$this->value" || $numDeleted) {

I don't really know why $courses and $this->value are not being set appropriately, but they are both set to "Course" which obviously isn't much use.

Hope that all helps a bit.

  • Like 1
Link to comment
Share on other sites

gave PageTable a quick try..commented module requirement out cause I´m still on 2.4.0 but seems to not working in the end..

it´s creating the page..but displaying strange stuff after saving and not showing the created page in the appropriate table

so..how "stable" is the actual dev state? cause the page could maybe switch to productive in the next couple days

but my brain thinks it figured out, that pagetable is not really a difference to repeater..so maybe I´ll give them another chance

thank you adrian works better right now..only checkboxes are still not working..maybe I can figure this out myself with your little hint about the checked state thanks :)

Link to comment
Share on other sites

  • 3 weeks later...

Hello everybody  :lol:

hope it´s okay to start a new post, thought the other one is quite old already?! :-[

I rearranged my head and came to a slightly different solution..

Started my customized Events Fieldtype from scratch and almost everything works fine now.

Couple of minutes ago it killed all my entries where I don´t really understand why, but I think

changing this

$count = $this->sanitizer->entities($event->count); 

to this

$count = $event->count > 0 ? $this->sanitizer->entities($event->count) : ''; 

hopefully did the trick, because it´s an integer field. Wasn´t deleting my entries since then..  :-)

I´m not 100% happy with date and time pickers because enddate/-time should change based on startdate/-time, but that´s not important..maybe I´ll change them anyway?!

One main thing still not working is editing existing events, maybe someone is willing to throw an eye on the code or has an idea what it could be.

Can

Maybe I find time and mood to get into github when finished this project  ^-^

UPDATE:

Same as before, commenting out

if("$events" != "$this->value" || $numDeleted) { 

as adrian mentioned helps for the moment but I would like to understand what I´m doing wrong and how I could improve this :)

FieldtypeEvents.zip

Edited by Can
Link to comment
Share on other sites

  • 2 weeks later...

It´s me again..don't want to be annoying but I could need a little hint for my course(event) table.

my actual working events template looks like this

$mo = $di = $mi = $do = $fr = $sa = $mo1 = $di1 = $mi1 = $do1 = $fr1 = $sa1 = $mo2 = $di2 = $mi2 = $do2 = $fr2 = $sa2 = $moclass = $diclass = $miclass = $doclass = $frclass = $saclass = $mo1class = $di1class = $mi1class = $do1class = $fr1class = $sa1class = $mo2class = $di2class = $mi2class = $do2class = $fr2class = $sa2class = '';
	foreach($page->events->sort('time') as $a) { //->sort('-time')
		$time = date('G',strtotime($a->time));
		$day = strftime('%A', strtotime($a->date));
		$datestart = date('d.m.', strtotime($a->date));
		$dateend = date('d.m.', strtotime($a->dateend));

		($time > 8 && $time <= 12 && $day == 'Montag') ? $mo .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $mo .= '';
		($time > 8 && $time <= 12 && $day == 'Dienstag') ? $di .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $di .= '';
		($time > 8 && $time <= 12 && $day == 'Mittwoch') ? $mi .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $mi .= '';
		($time > 8 && $time <= 12 && $day == 'Donnerstag') ? $do .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $do .= '';
		($time > 8 && $time <= 12 && $day == 'Freitag') ? $fr .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $fr .= '';
		($time > 8 && $time <= 12 && ($day == 'Samstag' || $day == 'Sonntag')) ? $sa .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $sa .= '';
		    	
		($time > 12 && $time < 19 && $day == 'Montag') ? $mo1 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $mo1 .= '';
		($time > 12 && $time < 19 && $day == 'Dienstag') ? $di1 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $di1 .= '';
		($time > 12 && $time < 19 && $day == 'Mittwoch') ? $mi1 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $mi1 .= '';
		($time > 12 && $time < 19 && $day == 'Donnerstag') ? $do1 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $do1 .= '';
		($time > 12 && $time < 19 && $day == 'Freitag') ? $fr1 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $fr1 .= '';
		($time > 12 && $time < 19 && ($day == 'Samstag' || $day == 'Sonntag')) ? $sa1 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $sa1 .= '';
	    	
		($time >= 19 && $day == 'Montag') ? $mo2 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $mo2 .= '';
		($time >= 19 && $day == 'Dienstag') ? $di2 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $di2 .= '';
		($time >= 19 && $day == 'Mittwoch') ? $mi2 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $mi2 .= '';
		($time >= 19 && $day == 'Donnerstag') ? $do2 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $do2 .= '';
		($time >= 19 && $day == 'Freitag') ? $fr2 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $fr2 .= '';
		($time >= 19 && ($day == 'Samstag' || $day == 'Sonntag')) ? $sa2 .= "<li class='{$a->color}'><strong><u>{$a->time} - {$a->timeend}</u><br>{$a->shorty}<br>{$a->descr}</strong><br>mit {$a->teacher}<br>{$datestart}- {$dateend}<br>{$a->count} mal<br><strong>{$a->notes}</strong></li>" : $sa2 .= '';
	}

	$content .= <<< _EVENTS
		<table class="events-table table-bordered width-100"><thead><th>Montag</th><th>Dienstag</th>
				<th>Mittwoch</th><th>Donnerstag</th><th>Freitag</th><th>Samstag/Sonntag</th></thead>
		<tbody><tr><td><ul>$mo</ul></td><td><ul>$di</ul></td><td><ul>$mi</ul></td><td><ul>$do</ul></td><td><ul>$fr</ul></td><td><ul>$sa</ul></td></tr>
		<tr><td><ul>$mo1</ul></td><td><ul>$di1</ul></td><td><ul>$mi1</ul></td><td><ul>$do1</ul></td><td><ul>$fr1</ul></td><td><ul>$sa1</ul></td></tr>
		<tr><td><ul>$mo2</ul></td><td><ul>$di2</ul></td><td><ul>$mi2</ul></td><td><ul>$do2</ul></td><td><ul>$fr2</ul></td><td><ul>$sa2</ul></td></tr>
		</tbody></table>
_EVENTS;

Maybe looks a bit messy with all those shorthand code  :-[

My problem is that it's too "static" I would love to have it more dynamic so every event gets it's own <td> cell instead of a <li> within a cell

Soo I digged literally through the whole web but couldn´t figure it out.

After a little hint from a friend I thought I got it..it's drawing the table quite nice but every event get's it's own row even if both are within same time range and one is on tuesday the other on thursday..so both could be in the first row

Now I think thats due to wrong sorting.

The events are sorted by date (which is beginning of the event) in admin.

but while iterating through the events in the template the events are unordered event with foreach($page->events->sort('date') or -date

Then I figured out that the get only sorted by day (got the german format d.m.Y though) so the 3.06.2014 comes before 14.04.2014

My newer code looks like this

$i = 1;
$days = array('Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag/Sonntag');
$content .= "<table><thead><tr>";
foreach($days as $d) $content .= "<th>$d</th>";
$content .= "</tr></thead><tbody>";
foreach($page->events->sort('-time') as $e) {
	$time = date('G',strtotime($e->time));
	$day = strftime('%A', strtotime($e->date));
	$datestart = date('d.m.', strtotime($e->date));
	$dateend = date('d.m.', strtotime($e->dateend));

   	if($i % 6 == 1) $content .= "<tr>";
	if($time > 8 && $time <= 12) {
   		foreach($days as $d) {

			  if($d == $day) {
			  	  $content .= "<td>{$e->date}-{$day}-{$e->time}<strong>{$e->sort}</strong></td>";
			  }
			  else {
			  	$content .= "<td></td>";
			  }

			  $i++;

	  	}
	}
	
	if($i % 6 == 1) $content .= "</tr>";
}
$content .= "</tbody></table>";

It's just a little stripped, the full code got 3 of those if($time > 8 && $time <= 12) with different time ranges of course

Just noticed that the sorting is not the the only reason, the events get own rows anyways but I don't know why?! ???

Hope could explain myself properly enough.

Hopefully someone could throw a little hint on this.

By the way, haven't figured out why I needed to disable if("$events" != "$this->value" || $numDeleted)  as mentioned to get editable events but it´s working quite nice right now so it's not really important..

UPDATE: okay, got the sorting working. changed the time fields so they save input as unix timestamp like date field so it's better sortable :-)

Still don't get more than 1 event in the same row :/

Edited by Can
Link to comment
Share on other sites

  • 2 months later...

just a small question on this....i'd like to automatic delete expired events on the template with the API.

Since i trashed my event page with the second try i think i could ask here... ;)

foreach($page->events as $event) {

//setup $now with time() and $edate is my eventdate....

	//deleting entries
	    if($now > $edate) {
	$page->events->trash($event);
}

//output all events goes here

so far i've found that trash() is not possible here....how can i remove an item via API?

regards mr-fan

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...