function query_events($user, $want_repeated, $date_filter, $cat_id = '')
{
    global $login;
    global $layers;
    $result = array();
    $layers_byuser = array();
    $sql = "SELECT webcal_entry.cal_name, webcal_entry.cal_description, " . "webcal_entry.cal_date, webcal_entry.cal_time, " . "webcal_entry.cal_id, webcal_entry.cal_ext_for_id, " . "webcal_entry.cal_priority, " . "webcal_entry.cal_access, webcal_entry.cal_duration, " . "webcal_entry_user.cal_status, " . "webcal_entry_user.cal_login ";
    if ($want_repeated) {
        $sql .= ", " . "webcal_entry_repeats.cal_type, webcal_entry_repeats.cal_end, " . "webcal_entry_repeats.cal_frequency, webcal_entry_repeats.cal_days " . "FROM webcal_entry, webcal_entry_repeats, webcal_entry_user " . "WHERE webcal_entry.cal_id = webcal_entry_repeats.cal_id AND ";
    } else {
        $sql .= "FROM webcal_entry, webcal_entry_user WHERE ";
    }
    $sql .= "webcal_entry.cal_id = webcal_entry_user.cal_id " . "AND webcal_entry_user.cal_status IN ('A','W') ";
    if ($cat_id != '') {
        $sql .= "AND webcal_entry_user.cal_category LIKE '{$cat_id}' ";
    }
    if (strlen($user) > 0) {
        $sql .= "AND (webcal_entry_user.cal_login = '******' ";
    }
    if ($user == $login && strlen($user) > 0) {
        for ($index = 0; $index < sizeof($layers); $index++) {
            $layeruser = $layers[$index]['cal_layeruser'];
            $sql .= "OR webcal_entry_user.cal_login = '******' ";
            // while we are parsing the whole layers array, build ourselves
            // a new array that will help when we have to check for dups
            $layers_byuser["{$layeruser}"] = $layers[$index]['cal_dups'];
        }
    }
    if (strlen($user) > 0) {
        $sql .= ") ";
    }
    $sql .= $date_filter;
    // now order the results by time and by entry id.
    $sql .= " ORDER BY webcal_entry.cal_time, webcal_entry.cal_id";
    //echo "<B>SQL:</B> $sql<P>";
    $res = dbi_query($sql);
    if ($res) {
        $i = 0;
        $checkdup_id = -1;
        $first_i_this_id = -1;
        while ($row = dbi_fetch_row($res)) {
            if ($row[8] == 'R' || $row[8] == 'D') {
                continue;
                // don't show rejected/deleted ones
            }
            $item = array("cal_name" => $row[0], "cal_description" => $row[1], "cal_date" => $row[2], "cal_time" => $row[3], "cal_id" => $row[4], "cal_ext_for_id" => $row[5], "cal_priority" => $row[6], "cal_access" => $row[7], "cal_duration" => $row[8], "cal_status" => $row[9], "cal_login" => $row[10], "cal_exceptions" => array());
            if ($want_repeated && !empty($row[11])) {
                $item['cal_type'] = empty($row[11]) ? "" : $row[11];
                $item['cal_end'] = empty($row[12]) ? "" : $row[12];
                $item['cal_frequency'] = empty($row[13]) ? "" : $row[13];
                $item['cal_days'] = empty($row[14]) ? "" : $row[14];
            }
            if ($item['cal_id'] != $checkdup_id) {
                $checkdup_id = $item['cal_id'];
                $first_i_this_id = $i;
            }
            if ($item['cal_login'] == $user) {
                // Insert this one before all other ones with this ID.
                my_array_splice($result, $first_i_this_id, 0, array($item));
                $i++;
                if ($first_i_this_id + 1 < $i) {
                    // There's another one with the same ID as the one we inserted.
                    // Check for dup and if so, delete it.
                    $other_item = $result[$first_i_this_id + 1];
                    if ($layers_byuser[$other_item['cal_login']] == 'N') {
                        // NOTE: array_splice requires PHP4
                        my_array_splice($result, $first_i_this_id + 1, 1, "");
                        $i--;
                    }
                }
            } else {
                if ($i == $first_i_this_id || !empty($layers_byuser[$item['cal_login']]) && $layers_byuser[$item['cal_login']] != 'N') {
                    // This item either is the first one with its ID, or allows dups.
                    // Add it to the end of the array.
                    $result[$i++] = $item;
                }
            }
        }
        dbi_free_result($res);
    }
    // Now load event exceptions and store as array in 'cal_exceptions' field
    if ($want_repeated) {
        for ($i = 0; $i < count($result); $i++) {
            if (!empty($result[$i]['cal_id'])) {
                $res = dbi_query("SELECT cal_date FROM webcal_entry_repeats_not " . "WHERE cal_id = " . $result[$i]['cal_id']);
                while ($row = dbi_fetch_row($res)) {
                    $result[$i]['cal_exceptions'][] = $row[0];
                }
            }
        }
    }
    return $result;
}
function query_events($user, $want_repeated, $date_filter, $cat_id = '')
{
    global $login;
    global $layers, $public_access_default_visible;
    $result = array();
    $layers_byuser = array();
    $sql = "SELECT webcal_entry.cal_name, webcal_entry.cal_description, " . "webcal_entry.cal_date, webcal_entry.cal_time, " . "webcal_entry.cal_id, webcal_entry.cal_ext_for_id, " . "webcal_entry.cal_priority, " . "webcal_entry.cal_access, webcal_entry.cal_duration, " . "webcal_entry_user.cal_status, " . "webcal_entry_user.cal_category, " . "webcal_entry_user.cal_login ";
    if ($want_repeated) {
        $sql .= ", " . "webcal_entry_repeats.cal_type, webcal_entry_repeats.cal_end, " . "webcal_entry_repeats.cal_frequency, webcal_entry_repeats.cal_days " . "FROM webcal_entry, webcal_entry_repeats, webcal_entry_user " . "WHERE webcal_entry.cal_id = webcal_entry_repeats.cal_id AND ";
    } else {
        $sql .= "FROM webcal_entry, webcal_entry_user WHERE ";
    }
    $sql .= "webcal_entry.cal_id = webcal_entry_user.cal_id " . "AND webcal_entry_user.cal_status IN ('A','W') ";
    if ($cat_id != '') {
        $sql .= "AND webcal_entry_user.cal_category LIKE '{$cat_id}' ";
    }
    if (strlen($user) > 0) {
        $sql .= "AND (webcal_entry_user.cal_login = '******' ";
    }
    if ($user == $login && strlen($user) > 0) {
        if ($layers) {
            foreach ($layers as $layer) {
                $layeruser = $layer['cal_layeruser'];
                $sql .= "OR webcal_entry_user.cal_login = '******' ";
                // while we are parsing the whole layers array, build ourselves
                // a new array that will help when we have to check for dups
                $layers_byuser["{$layeruser}"] = $layer['cal_dups'];
            }
        }
    }
    if ($user == $login && strlen($user) && $public_access_default_visible == 'Y') {
        $sql .= "OR webcal_entry_user.cal_login = '******' ";
    }
    if (strlen($user) > 0) {
        $sql .= ") ";
    }
    $sql .= $date_filter;
    // now order the results by time and by entry id.
    $sql .= " ORDER BY webcal_entry.cal_time, webcal_entry.cal_id";
    //echo "<strong>SQL:</strong> $sql<br />\n";
    $res = dbi_query($sql, true, true, " function <b>query_events</b>");
    if ($res) {
        $i = 0;
        $checkdup_id = -1;
        $first_i_this_id = -1;
        while ($row = dbi_fetch_row($res)) {
            if ($row[9] == 'R' || $row[9] == 'D') {
                continue;
                // don't show rejected/deleted ones
            }
            $item = array("cal_name" => $row[0], "cal_description" => $row[1], "cal_date" => $row[2], "cal_time" => $row[3], "cal_id" => $row[4], "cal_ext_for_id" => $row[5], "cal_priority" => $row[6], "cal_access" => $row[7], "cal_duration" => $row[8], "cal_status" => $row[9], "cal_category" => $row[10], "cal_picto" => get_cat_picto($row[10]), "cal_login" => $row[11], "cal_exceptions" => array());
            if ($want_repeated && !empty($row[12])) {
                $item['cal_type'] = empty($row[12]) ? "" : $row[12];
                $item['cal_end'] = empty($row[13]) ? "" : $row[13];
                $item['cal_frequency'] = empty($row[14]) ? "" : $row[14];
                $item['cal_days'] = empty($row[15]) ? "" : $row[15];
            }
            if ($item['cal_id'] != $checkdup_id) {
                $checkdup_id = $item['cal_id'];
                $first_i_this_id = $i;
            }
            if ($item['cal_login'] == $user) {
                // Insert this one before all other ones with this ID.
                my_array_splice($result, $first_i_this_id, 0, array($item));
                $i++;
                if ($first_i_this_id + 1 < $i) {
                    // There's another one with the same ID as the one we inserted.
                    // Check for dup and if so, delete it.
                    $other_item = $result[$first_i_this_id + 1];
                    if ($layers_byuser[$other_item['cal_login']] == 'N') {
                        // NOTE: array_splice requires PHP4
                        my_array_splice($result, $first_i_this_id + 1, 1, "");
                        $i--;
                    }
                }
            } else {
                if ($i == $first_i_this_id || !empty($layers_byuser[$item['cal_login']]) && $layers_byuser[$item['cal_login']] != 'N') {
                    // This item either is the first one with its ID, or allows dups.
                    // Add it to the end of the array.
                    $result[$i++] = $item;
                }
            }
        }
        dbi_free_result($res);
    }
    // Now load event exceptions and store as array in 'cal_exceptions' field
    if ($want_repeated) {
        for ($i = 0; $i < count($result); $i++) {
            if (!empty($result[$i]['cal_id'])) {
                $res = dbi_query("SELECT cal_date FROM webcal_entry_repeats_not " . "WHERE cal_id = " . $result[$i]['cal_id'], true, true, " function <b>query_events <u>exceptions</u></b>");
                while ($row = dbi_fetch_row($res)) {
                    $result[$i]['cal_exceptions'][] = $row[0];
                }
            }
            if (!empty($result[$i]['cal_id'])) {
                $cal_idIN .= $result[$i]['cal_id'] . ",";
            }
        }
        // fin boucle sur tableau
        if (!empty($cal_idIN)) {
            $cal_idIN = substr($cal_idIN, 0, strlen($cal_idIN) - 1);
            // enlève dernière virgule
            $cal_idIN = "(" . $cal_idIN . ")";
            $res = dbi_query("SELECT cal_date FROM webcal_entry_repeats_not " . "WHERE cal_id IN " . $cal_idIN, true, true, " function <b>query_events <u>exceptions</u></b>");
            while ($row = dbi_fetch_row($res)) {
                $result[$i]['cal_exceptions'][] = $row[0];
            }
        }
    }
    //$_SESSION['req'] .= "Resultat renvoye : ".retvar($result,"yes",false)."<br>\n";
    return $result;
}