Jump to content

How to set fieldtype options array manually?


Juergen
 Share

Recommended Posts

Hello @ all,

I have created a module which creates pages of dates depending of your choice (event calendar like).

Today I am struggeling with problem concerning the manual creation of an options fieldtype array.

My options fieldtype is called weekdaysevent and it is set to multiple - so you can check one or more.Screenshot_7.jpg

It contains 7 options (for every weekday one)

In the backend you can choose between different repeating types (daily, weekly, monthly, yearly)

If "daily" is selected all days should be in the fieldtype array, if all others are selected only the selected one should be in the array.

I have tried it the following way:

              if($page->repeattype->value == "D"){ //D is for the daily repeat
                $page->weekdaysevent = array(1,2,3,4,5,6,7);
                }
                $weekdays             = $page->weekdaysevent;

If daily is choosen, then all days (1-7) are in the array. If anotherone (fe. monthly) is choosen, then get the weekdays directly from the field.

Unfortunately this doesnt work. I always get the following error message after pressing the save button:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'probe-termin-2016-10-04-5664' for key 'name_parent_id'

This only happens if I choose daily and $page->weekdaysevent will be taken from inside the if statement. If I choose monthly or another all is working fine. I dont know why the if statement should be responsible for a dublicate entry.

Can someone help me out?

Link to comment
Share on other sites

Thanks! After trying some different changes it seems that the problem is situated in the interval date calculation and not in the array.

                $daysarray = array(
                    "",
                    "monday",
                    "tuesday",
                    "wednesday",
                    "thursday",
                    "friday",
                    "saturday",
                    "sunday"
                );
                for ($i = 1; $i < 8; $i++) {
                    if (${"dow$i"}) {
                        ${"dow$i"}   = $daysarray[$i]; //get checked days name
                        ${"start$i"} = new DateTime($startdate); //create start dates for every weekday
                        ${"start$i"}->modify(${"dow$i"}); //get the first occurence of each day
                        ${"interval$i"} = new DateInterval("P{$step}{$unit}"); //create intervals for each weekday
                        ${"period$i"}   = new DatePeriod(${"start$i"}, ${"interval$i"}, $end); //create periods for each weekday    
                        foreach (${"period$i"} as $date) {
                            $date          = $date->format('Y-m-d H:i');
                            $starttime     = strtotime($eventstarttime); //add time in seconds to date
                            $date          = (strtotime($date)) + $eventstartseconds;
                            ${"array$i"}[] = $date;
                        }
                    } else {
                        ${"array$i"}[] = "";
                    }
                }

This line of code seems to make the problems.

${"interval$i"} = new DateInterval("P{$step}{$unit}"); //create intervals for each weekday

If the DateInterval is "P1D" (daily) I get the error message, if it is for example "P1W" (weekly) everthing works. It seems that the daily repeat interval makes troubles.

Link to comment
Share on other sites

OK I solved it:

if($page->repeattype->value == "D"){
                  $step = "1";
                  $unit = "W";
                } else {
                $step                 = $page->steps; //Number for the interval
                $unit                 = $page->repeattype->value;
                }

If it is a daily repeat set the steps and the interval automatically to 1 and W. Otherwise take the values from the input fields.

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
 Share

  • Recently Browsing   0 members

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