function fixExceptions() { $db = JFactory::getDBO(); $db->setQuery("SELECT * FROM #__jevents_exception where exception_type=1 AND (oldstartrepeat='0000-00-00 00:00:00' OR oldstartrepeat is null) ORDER BY eventid ASC, startrepeat asc"); //$db->setQuery("SELECT * FROM #__jevents_exception where exception_type=1 ORDER BY eventid ASC, startrepeat asc"); $rows = $db->loadObjectList("rp_id"); echo $db->getErrorMsg(); $eventexceptions = array(); foreach ($rows as $row) { if (!array_key_exists($row->eventid, $eventexceptions)) { $eventexceptions[$row->eventid] = array(); } $eventexceptions[$row->eventid][$row->rp_id] = $row; } $this->dataModel = new JEventsDataModel("JEventsAdminDBModel"); foreach ($eventexceptions as $eventid => $exceptions) { //echo "<hr/>processing event $eventid<br/>"; $db->setQuery("SELECT * FROM #__jevents_exception where exception_type=0 and eventid={$eventid} ORDER BY eventid ASC, startrepeat asc"); $deletedexceptions = $db->loadObjectList("rp_id"); $vevent = $this->dataModel->queryModel->getVEventById($eventid); // skip any orphans if (!$vevent) { // double check it doesn't exist then remove it $db->setQuery("SELECT ev.* FROM #__jevents_vevent as ev WHERE ev.ev_id = '{$eventid}'"); $row = $db->loadObject(); continue; } $event = new jIcalEventDB($vevent); if (!$event) { // we have a problem continue; } $array = get_object_vars($vevent); foreach ($array as $k => $v) { $array[strtoupper($k)] = $v; } $icalevent = iCalEvent::iCalEventFromDB($array); // fix the rrule data $icalevent->rrule->eventid = $eventid; if ($icalevent->rrule->until == 0) { $icalevent->rrule->until = ""; } ob_start(); $generatedrepetitions = $icalevent->getRepetitions(true); ob_get_clean(); // Now put in the pseudo repeat ids $icalevent = $this->dataModel->queryModel->getEventById($eventid, 1, 'icaldb'); $firstrepeat = $icalevent->getOriginalFirstRepeat(); for ($r = 0; $r < count($generatedrepetitions); $r++) { $generatedrepetitions[$r]->pseudo_rp_id = $firstrepeat->rp_id() + $r; } // get the current repeats (will not include deleted ones) $db->setQuery("Select rpt.* from #__jevents_repetition as rpt where rpt.eventid = {$eventid} order by rpt.rp_id asc"); $currentreprows = $db->loadObjectList("rp_id"); $rpids = array_merge(array_keys($currentreprows), array_keys($deletedexceptions)); sort($rpids); $currentrepetitions = array(); $rindex = 0; foreach ($rpids as $rid) { if (!array_key_exists($rid, $currentreprows)) { $rindex += 1; continue; } else { $currentrepetitions[$rindex] = $currentreprows[$rid]; } $rindex += 1; } if (count($currentrepetitions) > 0) { if (count($generatedrepetitions) > 0) { // The repetitions should be in the same sequence $countcurrent = count($currentrepetitions); $countgenerated = count($generatedrepetitions); foreach ($currentrepetitions as $c => $current) { foreach ($generatedrepetitions as $g => $generated) { if ($current->startrepeat == $generated->startrepeat) { // now set the oldstartrepeat field if this is an exception //echo "matched $current->startrepeat rpid=" . $current->rp_id . " pseudo rp_id= " . $generated->pseudo_rp_id . "<br/>"; if (array_key_exists($current->rp_id, $exceptions)) { $db->setQuery("Update #__jevents_exception set oldstartrepeat=" . $db->Quote($current->startrepeat) . " WHERE rp_id=" . $current->rp_id); $db->query(); unset($eventexceptions[$eventid][$current->rp_id]); unset($exceptions[$current->rp_id]); } unset($currentrepetitions[$c]); unset($generatedrepetitions[$g]); } } } // We have now dealt with the exceptions with matching dates (the easy ones!) // we have no more exceptions to look through so continue if (count($generatedrepetitions) == 0) { continue; } // This won't deal with scenario where a repeat has been moved and then deleted! if (count($deletedexceptions) > 0) { foreach ($deletedexceptions as $delrpid => $delex) { foreach ($generatedrepetitions as $g => $generated) { if ($generated->startrepeat == $delex->startrepeat) { unset($generatedrepetitions[$g]); unset($deletedexceptions[$delrpid]); } } } } // now match them by pseudo rp_id if (count($currentrepetitions) > 0) { //echo "Still more to process<br/>"; foreach ($currentrepetitions as $c => $current) { foreach ($generatedrepetitions as $g => $generated) { if ($current->rp_id == $generated->pseudo_rp_id) { //echo "matched $current->startrepeat rpid=" . $current->rp_id . " pseudo rp_id= " . $generated->pseudo_rp_id . "<br/>"; if (array_key_exists($current->rp_id, $exceptions)) { $db->setQuery("Update #__jevents_exception set oldstartrepeat=" . $db->Quote($current->startrepeat) . " WHERE rp_id=" . $current->rp_id); $db->query(); unset($eventexceptions[$eventid][$current->rp_id]); unset($exceptions[$current->rp_id]); } unset($currentrepetitions[$c]); unset($generatedrepetitions[$g]); } } } } if (count($deletedexceptions) == 0 && count($generatedrepetitions) == count($currentrepetitions)) { $countcurrent = count($currentrepetitions); $gplus = 0; foreach ($currentrepetitions as $c => $current) { if (!array_key_exists($c, $generatedrepetitions)) { $x = 1; } if (array_key_exists($current->rp_id, $exceptions)) { $generated = $generatedrepetitions[$c]; $db->setQuery("Update #__jevents_exception set oldstartrepeat=" . $db->Quote($generated->startrepeat) . " WHERE rp_id=" . $current->rp_id); $db->query(); unset($eventexceptions[$eventid][$current->rp_id]); unset($exceptions[$current->rp_id]); } unset($currentrepetitions[$c]); unset($generatedrepetitions[$c]); } } foreach ($exceptions as $rpid => $exception) { $matched = false; foreach ($generatedrepetitions as $generatedrepetition) { if ($generatedrepetition->startrepeat == $exception->startrepeat) { } } } foreach ($currentrepetitions as $rep) { if (array_key_exists($rep->rp_id, $exceptions)) { $db->setQuery("Update #__jevents_exception set oldstartrepeat=" . $db->Quote($rep->startrepeat) . " WHERE rp_id=" . $rep->rp_id); $db->query(); //echo $rep->startrepeat . " " . $rep->rp_id . "<Br/>"; } } } else { echo "no repeats?<br/>"; } } } echo "all done"; return; }