function import($filename, $rawtext = "")
 {
     @ini_set("max_execution_time", 600);
     echo JText::sprintf("Importing events from ical file %s", $filename) . "<br/>";
     $cfg =& JEVConfig::getInstance();
     $option = JEV_COM_COMPONENT;
     // resultant data goes here
     if ($filename != "") {
         $file = $filename;
         if (!@file_exists($file)) {
             $file = JPATH_SITE . "/components/{$option}/" . $filename;
         }
         if (!file_exists($file)) {
             echo "I hope this is a URL!!<br/>";
             $file = $filename;
         }
         // get name
         $isFile = false;
         if (isset($_FILES['upload']) && is_array($_FILES['upload'])) {
             $uploadfile = $_FILES['upload'];
             // MSIE sets a mime-type of application/octet-stream
             if ($uploadfile['size'] != 0 && ($uploadfile['type'] == "text/calendar" || $uploadfile['type'] == "text/csv" || $uploadfile['type'] == "application/octet-stream" || $uploadfile['type'] == "text/html")) {
                 $this->srcURL = $uploadfile['name'];
                 $isFile = true;
             }
         }
         if ($this->srcURL == "") {
             $this->srcURL = $file;
         }
         // $this->rawData = iconv("ISO-8859-1","UTF-8",file_get_contents($file));
         if (!$isFile && is_callable("curl_exec")) {
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_URL, ($isFile ? "file://" : "") . $file);
             curl_setopt($ch, CURLOPT_VERBOSE, 1);
             curl_setopt($ch, CURLOPT_POST, 0);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             $this->rawData = curl_exec($ch);
             curl_close($ch);
             // try file_get_contents as a backup
             if ($isFile && $this->rawData === false) {
                 $this->rawData = @file_get_contents($file);
             }
         } else {
             $this->rawData = @file_get_contents($file);
         }
         if ($this->rawData === false) {
             // file_get_contents: no or blocked wrapper for $file
             JError::raiseNotice(0, 'file_get_contents() failed, try fsockopen');
             $parsed_url = parse_url($file);
             if ($parsed_url === false) {
                 JError::raiseWarning(0, 'url not parsed: ' . $file);
             } else {
                 if ($parsed_url['scheme'] == 'http' || $parsed_url['scheme'] == 'https' || $parsed_url['scheme'] == 'webcal') {
                     // try socked connection
                     $fsockhost = $parsed_url['host'];
                     $fsockport = 80;
                     if ($parsed_url['scheme'] == 'https') {
                         $fsockhost = 'ssl://' . $fsockhost;
                         $fsockport = 443;
                     }
                     if (array_key_exists('port', $parsed_url)) {
                         $fsockport = $parsed_url['port'];
                     }
                     $fh = @fsockopen($fsockhost, $fsockport, $errno, $errstr, 3);
                     if ($fh === false) {
                         // fsockopen: no connect
                         JError::raiseWarning(0, 'fsockopen: no connect for ' . $file . ' - ' . $errstr);
                         return false;
                     } else {
                         $fsock_path = (array_key_exists('path', $parsed_url) ? $parsed_url['path'] : '') . (array_key_exists('query', $parsed_url) ? $parsed_url['query'] : '') . (array_key_exists('fragment', $parsed_url) ? $parsed_url['fragment'] : '');
                         fputs($fh, "GET {$fsock_path} HTTP/1.0\r\n");
                         fputs($fh, "Host: " . $parsed_url['host'] . "\r\n\r\n");
                         while (!feof($fh)) {
                             $this->rawData .= fread($fh, 4096);
                         }
                         fclose($fh);
                         $this->rawData = JString::substr($this->rawData, JString::strpos($this->rawData, "\r\n\r\n") + 4);
                     }
                 }
             }
         }
         // Returns true if $string is valid UTF-8 and false otherwise.
         /*
         $isutf8 = $this->detectUTF8($this->rawData);
         if ($isutf8) {
         $this->rawData = iconv("ISO-8859-1","UTF-8",$this->rawData);
         }
         */
     } else {
         $this->srcURL = "n/a";
         $this->rawData = $rawtext;
     }
     // get rid of spurious carriage returns and spaces
     //$this->rawData = preg_replace("/[\r\n]+ ([:;])/","$1",$this->rawData);
     // simplify line feed
     $this->rawData = str_replace("\r\n", "\n", $this->rawData);
     // remove spurious lines before calendar start
     if (!JString::stristr($this->rawData, 'BEGIN:VCALENDAR')) {
         // check for CSV format
         $firstLine = JString::substr($this->rawData, 0, JString::strpos($this->rawData, "\n") + 1);
         if (JString::stristr($firstLine, 'SUMMARY') && JString::stristr($firstLine, 'DTSTART') && JString::stristr($firstLine, 'DTEND') && JString::stristr($firstLine, 'CATEGORIES') && JString::stristr($firstLine, 'TIMEZONE')) {
             $timezone = date_default_timezone_get();
             $csvTrans = new CsvToiCal($file);
             $this->rawData = $csvTrans->getRawData();
             date_default_timezone_set($timezone);
         } else {
             JError::raiseWarning(0, 'Not a valid VCALENDAR data file: ' . $this->srcURL);
             //JError::raiseWarning(0, 'Not a valid VCALENDAR or CSV data file: ' . $this->srcURL);
             // return false so that we don't remove a valid calendar because of a bad URL load!
             return false;
         }
     }
     $begin = JString::strpos($this->rawData, "BEGIN:VCALENDAR", 0);
     $this->rawData = JString::substr($this->rawData, $begin);
     //		$this->rawData = preg_replace('/^.*\n(BEGIN:VCALENDAR)/s', '$1', $this->rawData, 1);
     // unfold content lines according the unfolding procedure of rfc2445
     $this->rawData = str_replace("\n ", "", $this->rawData);
     $this->rawData = str_replace("\n\t", "", $this->rawData);
     // TODO make sure I can always ignore the second line
     // Some google calendars has spaces and carriage returns in their UIDs
     // Convert string into array for easier processing
     $this->rawData = explode("\n", $this->rawData);
     $skipuntil = null;
     foreach ($this->rawData as $vcLine) {
         //$vcLine = trim($vcLine); // trim one line
         if (!empty($vcLine)) {
             // skip unhandled block
             if ($skipuntil) {
                 if (trim($vcLine) == $skipuntil) {
                     // found end of block to skip
                     $skipuntil = null;
                 }
                 continue;
             }
             $matches = explode(":", $vcLine, 2);
             if (count($matches) == 2) {
                 list($this->key, $value) = $matches;
                 //$value = str_replace('\n', "\n", $value);
                 //$value = stripslashes($value);
                 $append = false;
                 // Treat Accordingly
                 switch ($vcLine) {
                     case "BEGIN:VTODO":
                         // start of VTODO section
                         $this->todoCount++;
                         $parent = "VTODO";
                         break;
                     case "BEGIN:VEVENT":
                         // start of VEVENT section
                         $this->eventCount++;
                         $parent = "VEVENT";
                         break;
                     case "BEGIN:VCALENDAR":
                     case "BEGIN:DAYLIGHT":
                     case "BEGIN:VTIMEZONE":
                     case "BEGIN:STANDARD":
                         $parent = $value;
                         // save tu array under value key
                         break;
                     case "END:VTODO":
                     case "END:VEVENT":
                     case "END:VCALENDAR":
                     case "END:DAYLIGHT":
                     case "END:VTIMEZONE":
                     case "END:STANDARD":
                         $parent = "VCALENDAR";
                         break;
                     default:
                         // skip unknown BEGIN/END blocks
                         if ($this->key == 'BEGIN') {
                             $skipuntil = 'END:' . $value;
                             break;
                         }
                         // Generic processing
                         $this->add_to_cal($parent, $this->key, $value, $append);
                         break;
                 }
             } else {
                 // ignore these lines go
             }
         }
     }
     // Sort the events into start date order
     // there's little point in doing this id an RRULE is present!
     //	usort($this->cal['VEVENT'], array("iCalImport","comparedates"));
     // Populate vevent class - should do this first trawl through !!
     if (array_key_exists("VEVENT", $this->cal)) {
         foreach ($this->cal["VEVENT"] as $vevent) {
             // trap for badly constructed all day events
             if (isset($vevent["DTSTARTRAW"]) && isset($vevent["DTENDRAW"]) && $vevent["DTENDRAW"] == $vevent["DTSTARTRAW"]) {
                 $vevent["DTEND"] += 86400;
             }
             $this->vevents[] = iCalEvent::iCalEventFromData($vevent);
         }
     }
     return $this;
 }
Example #2
0
 function save($array, &$queryModel, $rrule, $dryrun = false)
 {
     $cfg =& JEVConfig::getInstance();
     $db =& JFactory::getDBO();
     $user = JFactory::getUser();
     // Allow plugins to check data validity
     $dispatcher =& JDispatcher::getInstance();
     JPluginHelper::importPlugin("jevents");
     $res = $dispatcher->trigger('onBeforeSaveEvent', array(&$array, &$rrule, $dryrun));
     // TODO do error and hack checks here
     $ev_id = intval(JArrayHelper::getValue($array, "evid", 0));
     $newevent = $ev_id == 0;
     $data = array();
     // TODO add UID to edit form
     $data["UID"] = JArrayHelper::getValue($array, "uid", md5(uniqid(rand(), true)));
     $data["X-EXTRAINFO"] = JArrayHelper::getValue($array, "extra_info", "");
     $data["LOCATION"] = JArrayHelper::getValue($array, "location", "");
     $data["allDayEvent"] = JArrayHelper::getValue($array, "allDayEvent", "off");
     $data["CONTACT"] = JArrayHelper::getValue($array, "contact_info", "");
     $data["DESCRIPTION"] = JArrayHelper::getValue($array, "jevcontent", "");
     $data["publish_down"] = JArrayHelper::getValue($array, "publish_down", "2006-12-12");
     $data["publish_up"] = JArrayHelper::getValue($array, "publish_up", "2006-12-12");
     $data["SUMMARY"] = JArrayHelper::getValue($array, "title", "");
     $data["URL"] = JArrayHelper::getValue($array, "url", "");
     // If user is jevents can deleteall or has backend access then allow them to specify the creator
     $jevuser = JEVHelper::getAuthorisedUser();
     $creatorid = JRequest::getInt("jev_creatorid", 0);
     if ($creatorid > 0) {
         if (JVersion::isCompatible("1.6.0")) {
             //$access = JAccess::check($user->id, "core.deleteall","com_jevents");
             $access = $user->authorise('core.admin', 'com_jevents');
         } else {
             // Get an ACL object
             $acl =& JFactory::getACL();
             $grp = $acl->getAroGroup($user->get('id'));
             $access = $acl->is_group_child_of($grp->name, 'Public Backend');
         }
         if ($jevuser && $jevuser->candeleteall || $access) {
             $data["X-CREATEDBY"] = $creatorid;
         }
     }
     $ics_id = JArrayHelper::getValue($array, "ics_id", 0);
     if ($data["allDayEvent"] == "on") {
         $start_time = "00:00";
     } else {
         $start_time = JArrayHelper::getValue($array, "start_time", "08:00");
     }
     $publishstart = $data["publish_up"] . ' ' . $start_time . ':00';
     $data["DTSTART"] = JevDate::strtotime($publishstart);
     if ($data["allDayEvent"] == "on") {
         $end_time = "00:00";
     } else {
         $end_time = JArrayHelper::getValue($array, "end_time", "15:00");
     }
     $publishend = $data["publish_down"] . ' ' . $end_time . ':00';
     if (isset($array["noendtime"]) && $array["noendtime"]) {
         $publishend = $data["publish_down"] . ' 23:59:59';
     }
     $data["DTEND"] = JevDate::strtotime($publishend);
     // iCal for whole day uses 00:00:00 on the next day JEvents uses 23:59:59 on the same day
     list($h, $m, $s) = explode(":", $end_time . ':00');
     if ($h + $m + $s == 0 && $data["allDayEvent"] == "on" && $data["DTEND"] > $data["DTSTART"]) {
         //if (($h+$m+$s)==0 && $data["allDayEvent"]=="on" && $data["DTEND"]>=$data["DTSTART"]) {
         //$publishend = JevDate::strftime('%Y-%m-%d 23:59:59',($data["DTEND"]-86400));
         $publishend = JevDate::strftime('%Y-%m-%d 23:59:59', $data["DTEND"]);
         $data["DTEND"] = JevDate::strtotime($publishend);
     }
     $data["RRULE"] = $rrule;
     $data["MULTIDAY"] = JArrayHelper::getValue($array, "multiday", "1");
     $data["NOENDTIME"] = JArrayHelper::getValue($array, "noendtime", "0");
     $data["X-COLOR"] = JArrayHelper::getValue($array, "color", "");
     $data["LOCKEVENT"] = JArrayHelper::getValue($array, "lockevent", "0");
     // Add any custom fields into $data array
     foreach ($array as $key => $value) {
         if (strpos($key, "custom_") === 0) {
             $data[$key] = $value;
         }
     }
     $vevent = iCalEvent::iCalEventFromData($data);
     $vevent->catid = JArrayHelper::getValue($array, "catid", 0);
     if (is_array($vevent->catid)) {
         JArrayHelper::toInteger($vevent->catid);
     }
     // if catid is empty then use the catid of the ical calendar
     if (is_string($vevent->catid) && $vevent->catid <= 0 || is_array($vevent->catid) && count($vevent->catid) == 0) {
         $query = "SELECT catid FROM #__jevents_icsfile WHERE ics_id={$ics_id}";
         $db->setQuery($query);
         $vevent->catid = $db->loadResult();
     }
     $vevent->access = intval(JArrayHelper::getValue($array, "access", 0));
     if (!JVersion::isCompatible("1.6.0")) {
         $vevent->access = $vevent->access > $user->aid ? $user->aid : $vevent->access;
     }
     $vevent->state = intval(JArrayHelper::getValue($array, "state", 0));
     // Shouldn't really do this like this
     $vevent->_detail->priority = intval(JArrayHelper::getValue($array, "priority", 0));
     // FRONT END AUTO PUBLISHING CODE
     $frontendPublish = JEVHelper::isEventPublisher();
     if (!$frontendPublish) {
         $frontendPublish = JEVHelper::canPublishOwnEvents($ev_id);
     }
     // Always unpublish if no Publisher otherwise publish automatically (for new events)
     // Should we always notify of new events
     $notifyAdmin = $cfg->get("com_notifyallevents", 0);
     if (!JFactory::getApplication()->isAdmin()) {
         if ($frontendPublish && $ev_id == 0) {
             $vevent->state = 1;
         } else {
             if (!$frontendPublish) {
                 $vevent->state = 0;
                 // In this case we send a notification email to admin
                 $notifyAdmin = true;
             }
         }
     }
     $vevent->icsid = $ics_id;
     if ($ev_id > 0) {
         $vevent->ev_id = $ev_id;
     }
     $rp_id = intval(JArrayHelper::getValue($array, "rp_id", 0));
     if ($rp_id > 0) {
         // I should be able to do this in one operation but that can come later
         $testevent = $queryModel->listEventsById(intval($rp_id), 1, "icaldb");
         if (!JEVHelper::canEditEvent($testevent)) {
             JError::raiseError(403, JText::_('ALERTNOTAUTH'));
         }
     }
     $db =& JFactory::getDBO();
     $success = true;
     //echo "class = ".get_class($vevent);
     if (!$dryrun) {
         if (!$vevent->store()) {
             echo $db->getErrorMsg() . "<br/>";
             $success = false;
             JError::raiseWarning(101, JText::_('COULD_NOT_SAVE_EVENT_'));
         }
     } else {
         // need a value for eventid to pretend we have saved the event so we can get the repetitions
         if (!isset($vevent->ev_id)) {
             $vevent->ev_id = 0;
         }
         $vevent->rrule->eventid = $vevent->ev_id;
     }
     // Only update the repetitions if the event edit says the reptitions will have changed or a new event
     if ($newevent || JRequest::getInt("updaterepeats", 1)) {
         $repetitions = $vevent->getRepetitions(true);
         if (!$dryrun) {
             if (!$vevent->storeRepetitions()) {
                 echo $db->getErrorMsg() . "<br/>";
                 $success = false;
                 JError::raiseWarning(101, JText::_('COULD_NOT_SAVE_REPETITIONS'));
             }
         }
     }
     $res = $dispatcher->trigger('onAfterSaveEvent', array(&$vevent, $dryrun));
     if ($dryrun) {
         return $vevent;
     }
     // If not authorised to publish in the frontend then notify the administrator
     if (!$dryrun && $success && $notifyAdmin && !JFactory::getApplication()->isAdmin()) {
         JLoader::register('JEventsCategory', JEV_ADMINPATH . "/libraries/categoryClass.php");
         $cat = new JEventsCategory($db);
         $cat->load($vevent->catid);
         $adminuser = $cat->getAdminUser();
         $adminEmail = $adminuser->email;
         $config = new JConfig();
         $sitename = $config->sitename;
         $subject = JText::_('JEV_MAIL_ADDED') . ' ' . $sitename;
         $subject = $vevent->state == '1' ? '[Info] ' . $subject : '[Approval] ' . $subject;
         $Itemid = JEVHelper::getItemid();
         // reload the event to get the reptition ids
         $evid = intval($vevent->ev_id);
         $testevent = $queryModel->getEventById($evid, 1, "icaldb");
         $rp_id = $testevent->rp_id();
         list($year, $month, $day) = JEVHelper::getYMD();
         //http://joomlacode1.5svn/index.php?option=com_jevents&task=icalevent.edit&evid=1&Itemid=68&rp_id=72&year=2008&month=09&day=10&lang=cy
         $uri =& JURI::getInstance(JURI::base());
         $root = $uri->toString(array('scheme', 'host', 'port'));
         $modifylink = '<a href="' . $root . JRoute::_('index.php?option=' . JEV_COM_COMPONENT . '&task=icalevent.edit&evid=' . $evid . '&rp_id=' . $rp_id . '&Itemid=' . $Itemid . "&year={$year}&month={$month}&day={$day}") . '"><b>' . JText::_('JEV_MODIFY') . '</b></a>' . "\n";
         $viewlink = '<a href="' . $root . JRoute::_('index.php?option=' . JEV_COM_COMPONENT . '&task=icalrepeat.detail&evid=' . $rp_id . '&Itemid=' . $Itemid . "&year={$year}&month={$month}&day={$day}&login=1") . '"><b>' . JText::_('JEV_VIEW') . '</b></a>' . "\n";
         $created_by = $user->name;
         if ($created_by == null) {
             $created_by = "Anonymous";
             if (JRequest::getString("custom_anonusername", "") != "") {
                 $created_by = JRequest::getString("custom_anonusername", "") . " (" . JRequest::getString("custom_anonemail", "") . ")";
             }
         }
         JEV_CommonFunctions::sendAdminMail($sitename, $adminEmail, $subject, $testevent->title(), $testevent->content(), $created_by, JURI::root(), $modifylink, $viewlink);
     }
     if ($success) {
         return $vevent;
     }
     return $success;
 }
Example #3
0
 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;
 }
 function edit()
 {
     // get the view
     $this->view =& $this->getView("icalevent", "html");
     $cid = JRequest::getVar('cid', array(0));
     JArrayHelper::toInteger($cid);
     if (is_array($cid) && count($cid) > 0) {
         $id = $cid[0];
     } else {
         $id = 0;
     }
     // front end passes the id as evid
     if ($id == 0) {
         $id = JRequest::getInt("evid", 0);
     }
     if (!JEVHelper::isEventCreator()) {
         JError::raiseError(403, JText::_('ALERTNOTAUTH'));
     }
     $repeatId = 0;
     $db = JFactory::getDBO();
     // iCal agid uses GUID or UUID as identifier
     if ($id > 0) {
         if ($repeatId == 0) {
             // this version gives us a repeat not an event so
             //$row = $this->queryModel->getEventById($id, true, "icaldb");
             $vevent = $this->dataModel->queryModel->getVEventById($id);
             if (!$vevent) {
                 $Itemid = JRequest::getInt("Itemid");
                 JFactory::getApplication()->redirect(JRoute::_("index.php?option=" . JEV_COM_COMPONENT . "&Itemid={$Itemid}", false), JText::_("JEV_SORRY_UPDATED"));
             }
             $row = new jIcalEventDB($vevent);
             $row->fixDtstart();
         } else {
             $row = $this->queryModel->listEventsById($repeatId, true, "icaldb");
         }
         if (!JEVHelper::canEditEvent($row)) {
             JError::raiseError(403, JText::_('ALERTNOTAUTH'));
         }
     } else {
         $vevent = new iCalEvent($db);
         $vevent->set("freq", "DAILY");
         $vevent->set("description", "");
         $vevent->set("summary", "");
         list($year, $month, $day) = JEVHelper::getYMD();
         $params = JComponentHelper::getParams(JEV_COM_COMPONENT);
         $defaultstarttime = $params->get("defaultstarttime", "08:00");
         $defaultendtime = $params->get("defaultendtime", "17:00");
         list($starthour, $startmin) = explode(":", $defaultstarttime);
         list($endhour, $endmin) = explode(":", $defaultendtime);
         $vevent->set("dtstart", JevDate::mktime($starthour, $startmin, 0, $month, $day, $year));
         $vevent->set("dtend", JevDate::mktime($endhour, $endmin, 0, $month, $day, $year));
         $row = new jIcalEventDB($vevent);
         // TODO - move this to class!!
         // populate with meaningful initial values
         $row->starttime($defaultstarttime);
         $row->endtime($defaultendtime);
     }
     /*
     $db =& JFactory::getDBO();
     // get list of groups
     $query = "SELECT id AS value, name AS text"
     . "\n FROM #__groups"
     . "\n ORDER BY id"	;
     $db->setQuery( $query );
     $groups = $db->loadObjectList();
     
     // build the html select list
     $glist = JHTML::_('select.genericlist', $groups, 'access', 'class="inputbox" size="1"',	'value', 'text', intval( $row->access() ) );
     */
     $glist = JEventsHTML::buildAccessSelect(intval($row->access()), 'class="inputbox" size="1"');
     // get all the raw native calendars
     $nativeCals = $this->dataModel->queryModel->getNativeIcalendars();
     // Strip this list down based on user permissions
     $jevuser =& JEVHelper::getAuthorisedUser();
     if ($jevuser && $jevuser->calendars != "" && $jevuser->calendars != "all") {
         $cals = array_keys($nativeCals);
         $allowedcals = explode("|", $jevuser->calendars);
         foreach ($cals as $calid) {
             if (!in_array($calid, $allowedcals)) {
                 unset($nativeCals[$calid]);
             }
         }
     }
     // Are we allowed to edit events within a URL based iCal
     $params = JComponentHelper::getParams(JEV_COM_COMPONENT);
     if ($params->get("allowedit", 0) && $row->icsid() > 0) {
         $calsql = 'SELECT * FROM #__jevents_icsfile WHERE ics_id=' . intval($row->icsid());
         $db->setQuery($calsql);
         $cal = $db->loadObject();
         if ($cal && $cal->icaltype == 0) {
             $nativeCals[$cal->ics_id] = $cal;
             $this->view->assign("offerlock", 1);
         }
     }
     $excats = "0";
     if ($jevuser && $jevuser->categories != "" && $jevuser->categories != "all") {
         // Find which categories to exclude
         if (JVersion::isCompatible("1.6.0")) {
             $catsql = 'SELECT id  FROM #__categories WHERE id NOT IN (' . str_replace("|", ",", $jevuser->categories) . ') AND extension="com_jevents"';
         } else {
             $catsql = 'SELECT id  FROM #__categories WHERE id NOT IN (' . str_replace("|", ",", $jevuser->categories) . ') AND section="com_jevents"';
         }
         $db->setQuery($catsql);
         $excats = implode(",", $db->loadResultArray());
     }
     // only offer a choice of native calendars if it exists!
     if (count($nativeCals) > 1) {
         $icalList = array();
         $icalList[] = JHTML::_('select.option', '0', JText::_('JEV_EVENT_CHOOSE_ICAL'), 'ics_id', 'label');
         $icalList = array_merge($icalList, $nativeCals);
         $clist = JHTML::_('select.genericlist', $icalList, 'ics_id', " onchange='preselectCategory(this);'", 'ics_id', 'label', $row->icsid());
         $this->view->assign('clistChoice', true);
         $this->view->assign('defaultCat', 0);
     } else {
         if (count($nativeCals) == 0 || !is_array($nativeCals)) {
             JError::raiseWarning(870, JText::_('INVALID_CALENDAR_STRUCTURE'));
         }
         $icsid = $row->icsid() > 0 ? $row->icsid() : current($nativeCals)->ics_id;
         $clist = '<input type="hidden" name="ics_id" value="' . $icsid . '" />';
         $this->view->assign('clistChoice', false);
         $params = JComponentHelper::getParams(JEV_COM_COMPONENT);
         if ($params->get("defaultcat", false)) {
             $this->view->assign('defaultCat', current($nativeCals)->catid);
         } else {
             $this->view->assign('defaultCat', 0);
         }
     }
     // Set the layout
     $this->view->setLayout('edit');
     $this->view->assign('editCopy', $this->editCopy);
     $this->view->assign('id', $id);
     $this->view->assign('row', $row);
     $this->view->assign('excats', $excats);
     $this->view->assign('nativeCals', $nativeCals);
     $this->view->assign('clist', $clist);
     $this->view->assign('repeatId', $repeatId);
     $this->view->assign('glist', $glist);
     // only those who can publish globally can set priority field
     if (JEVHelper::isEventPublisher(true)) {
         $list = array();
         for ($i = 0; $i < 10; $i++) {
             $list[] = JHTML::_('select.option', $i, $i, 'val', 'text');
         }
         $priorities = JHTML::_('select.genericlist', $list, 'priority', "", 'val', 'text', $row->priority());
         $this->view->assign('setPriority', true);
         $this->view->assign('priority', $priorities);
     } else {
         $this->view->assign('setPriority', false);
     }
     $this->view->assignRef('dataModel', $this->dataModel);
     // for Admin interface only
     $this->view->assign('with_unpublished_cat', JFactory::getApplication()->isAdmin());
     $this->view->display();
 }
 /**
  * Pseudo Constructor
  *
  * @param iCal Event parsed from ICS file as an array $ice
  * @return n/a
  */
 function iCalEventFromDB($icalrowAsArray)
 {
     $db =& JFactory::getDBO();
     $temp = new iCalEvent($db);
     foreach ($icalrowAsArray as $key => $val) {
         $temp->{$key} = $val;
     }
     if ($temp->freq != "") {
         $temp->rrule = iCalRRule::iCalRRuleFromDB($icalrowAsArray);
     }
     $temp->setupEventDetailFromDB($icalrowAsArray);
     return $temp;
 }