public function calendar($args) { // @todo Validate args. $yearNum = isset($args['year']) ? $args['year'] : date('Y'); $monthNum = isset($args['month']) ? $args['month'] : date('m'); $template = new \WordPress\Tabulate\Template('calendar.html'); $table = $this->get_table($args['table']); $template->table = $table; $template->action = 'calendar'; $template->record = $table->get_default_record(); $factory = new \CalendR\Calendar(); $template->weekdays = $factory->getWeek(new \DateTime('Monday this week')); $month = $factory->getMonth(new \DateTime($yearNum . '-' . $monthNum . '-01')); $template->month = $month; $records = array(); foreach ($table->get_columns('date') as $dateCol) { $dateColName = $dateCol->get_name(); // Filter to the just the requested month. $table->add_filter($dateColName, '>=', $month->getBegin()->format('Y-m-d')); $table->add_filter($dateColName, '<=', $month->getEnd()->format('Y-m-d')); foreach ($table->get_records() as $rec) { $dateVal = $rec->{$dateColName}(); // Initialise the day's list of records. if (!isset($records[$dateVal])) { $records[$dateVal] = array(); } // Add this record to the day's list. $records[$dateVal][] = $rec; } } // $records is grouped by date, with each item in a single date being // an array like: ['record'=>Record, 'column'=>$name_of_date_column] $template->records = $records; return $template->render(); }