Mainos / Advertisement:

Varauskalenteri verkkosivuille

Kohteesta Taisto
Siirry navigaatioon Siirry hakuun

{{#allow-groups:user}}

nano /var/www/kalenteri.php

<style type="text/css">
.palkki_varasto {
       width:1600px;
       height:280px;
       position:relative;
       border-width:1px 1px 0 1px;
       border-style:solid;
       background-color:antiquewhite;
}
a {
       padding-top:12px;
       font-weight:bold;
       text-decoration:none;
}
a:link {
       color:black;
}
.palkki-pala {
       position:absolute;
       height:100%;
       background-color:burlywood;
       border:black 1px;
       border-style:solid;
       text-align:center;
}
.varattu {
       background-color:dimgrey;
}
.varattu:hover {
       background-color:dimgrey;
}
.pitka_palkki {
       height:39px;
       width:100%;
       position:relative;
       border-width:0 0 1px 0;
       border-style:solid;
}
a:hover {
       background-color:antiquewhite;
}
</style>
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
session_start();
function tuntia_sitten($tunnit) {
       return date(DATE_ATOM,mktime(date('H')-$tunnit));
}
include_once realpath(dirname(__FILE__) . '/google-api-php-client/examples/temp$
require_once realpath(dirname(__FILE__) . '/google-api-php-client/autoload.php'$
function valmistele() {
$client_id = SENSUROITU$
$service_account_name = SENSUROITU$
$key_file_location = '/var/www/key.p12'; //key.p12
$client = new Google_Client();
$client->setApplicationName("Kalenteri_Library_Examples");
$service = new Google_Service_Calendar($client);
/************************************************
 If we have an access token, we can carry on.
 Otherwise, we'll get one with the help of an
 assertion credential. In other examples the list
 of scopes was managed by the Client, but here
 we have to list them manually. We also supply
 the service account
************************************************/
if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials($service_account_name,
                                       'https://www.googleapis.com/auth/calend$
                                       $key);
                                       //'notasecret',
                                       //'http://oauth.net/grant_type/jwt/1.0/$
                                       //'GMAILOSOITE');
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
 $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
return $service;
}
$palvelu = valmistele();
$kalenteri_id = 'KALENTERI-ID';
 //echo $_SESSION['service_token'];
if(isset($_GET['y'])) {
       $aika_leima=$_GET['y'];
       $aika_alku=date(DATE_ATOM,$aika_leima);
       $aika_loppu=date(DATE_ATOM,strtotime('+1 hour',$aika_leima));
       $aika_alku_objekti=new Google_Service_Calendar_EventDateTime();
       $aika_alku_objekti->dateTime=$aika_alku;       
       $aika_loppu_objekti=new Google_Service_Calendar_EventDateTime();
       $aika_loppu_objekti->dateTime=$aika_loppu;        
       $tapahtuma=new Google_Service_Calendar_Event();        
       $tapahtuma->setStart($aika_alku_objekti);
       $tapahtuma->setEnd($aika_loppu_objekti);
       $lisatty_tapahtuma=$palvelu->events->insert($kalenteri_id,$tapahtuma);
       //print_r($lisatty_tapahtuma);
}
 //while($tapahtumat = $palvelu->events->listEvents($kalenteri_id)) {
$tapahtumat = $palvelu->events->listEvents($kalenteri_id);

echo '

';
  //var_dump($tapahtumat);
  echo '

';

$nyt=date(DATE_ATOM);
$varatut_ajat_kysely=new Google_Service_Calendar_FreeBusyRequest();
$kysytty_kalenteri=new Google_Service_Calendar_FreeBusyRequestItem();
$kysytty_kalenteri->id=$kalenteri_id;
$maanantai_aikaleima=strtotime('monday this week');
$sunnuntai_aikaleima=strtotime('monday next week'); //strtotime('tuesday next w$
$kysely_alku_aika=date(DATE_ATOM,$maanantai_aikaleima);
$kysely_loppu_aika=date(DATE_ATOM,$sunnuntai_aikaleima);
$varatut_ajat_kysely->timeMin=$kysely_alku_aika;
$varatut_ajat_kysely->timeMax=$kysely_loppu_aika;
$varatut_ajat_kysely->timeZone='Europe/Helsinki';
$varatut_ajat_kysely->items=array($kysytty_kalenteri);
$vapaat = $palvelu->freebusy->query($varatut_ajat_kysely);
$busy_kalenteri_objekti=$vapaat->getCalendars()[$kalenteri_id];

echo '

';
  //print_r($varatut_ajat_kysely);
  //print_r($vapaat);
 echo '

';

$paivan_pituus=86400;
$varattu_taulu=array();
 //var_dump($busy_kalenteri_objekti->getBusy());
foreach($busy_kalenteri_objekti->getBusy() as $varattu) {
       $alku_aika=strtotime($varattu->getStart());
       $loppu_aika=strtotime($varattu->getEnd());
       /*
       $alku_klo=date('H:i:s',$alku_aika);
       $loppu_klo=date('H:i:s',$loppu_aika);        
       $paiva_numero_alku=date('d',$alku_aika);
       $paiva_numero_loppu=date('d',$loppu_aika);
       echo 'alku aika:' . $varattu->getStart() . ' loppu aika : ' . $varattu-$       
       if($paiva_numero_alku != $paiva_numero_loppu) {
               $varattu_taulu[$paiva_numero_alku][] = array('alku' => $alku_kl$                                                                               $
               $varattu_taulu[$paiva_numero_loppu][] = array('alku' => '00:00:$                                                                               $
               for(;++$paiva_numero_alku<$paiva_numero_loppu;$paiva_numero_alk$
               {
                       $varattu_taulu[$paiva_numero_alku][] = array('alku' => $
                                                                              $
               }
       } else {
               $varattu_taulu[] = array('alku' => $alku_aika,
                                                               'loppu' => $lop$
       //}
       } else {
               $varattu_taulu[$paiva_numero_alku][] = array('alku' => $varattu$
               $varattu_taulu[$paiva_numero_loppu][] = array('loppu' => $varat$
       //}*/
       $varattu_taulu[]=array('alku' => $alku_aika, 'loppu' => $loppu_aika);
}
 //$varattu_taulu=array_map('strtotime',$busy_kalenteri_objekti->getBusy());
$vapaat_taulu=array_fill(1,7,array());
 //print_r($varattu_taulu);
 //print_r($vapaat_taulu);
 /*foreach($vapaat_taulu as $nro => $paiva) {
       //var_dump(isset($varattu_taulu[ $nro ]));
       if(!isset($varattu_taulu[ $nro ])) {
               $paiva = array('alku' => '00:00:00', 
                                                                       'loppu' => '24:00:00');
       }
}
foreach($varattu_taulu as $paivanro => $paiva) {
        //print_r($paiva);
       foreach($paiva as $i => $yks) {
               echo $i . ' ';
               if($yks['alku'] == '00:00:00') {
                       $vapaat_taulu[$paivanro][] = array('alku' => $yks['loppu'],
                                                                                               'loppu' => $paiva[$i+1]['alku']);
               } else {
                       $vapaat_taulu[$paivanro][] = array('alku' => $paiva[$i-1]['loppu'],
                                                                                               'loppu' => $paiva[$i]['alku']);
               }
       }
}*/
 //var_dump($vapaat_taulu);

echo '

';
$tunteja_vuorokaudessa=24;
$paivia_viikossa=7;
$teksti = ;
for($paiva_nro=0;$paiva_nro<$paivia_viikossa;$paiva_nro++) {
       $paiva=strtotime('+' . $paiva_nro . 'day',strtotime('monday this week'));
echo '
';
       for($tunti_nro=0; $tunti_nro < $tunteja_vuorokaudessa; $tunti_nro++) {
               $jakson_aika_leima=strtotime('+' . $tunti_nro . 'hour',$paiva);
               foreach($varattu_taulu as $ajan_jakso) {
                       //print_r($ajan_jakso);
                       if($jakson_aika_leima >= $ajan_jakso['alku'] && $jakson_aika_leima < $ajan_jakso['loppu']) {
                               $teksti=;
                               $tyyli='palkki-pala varattu';
                               break;
                       } else {
                               $tyyli='palkki-pala';
                               $teksti = 'varaa';
                       }
               }
                echo '<a href="?y=' . $jakson_aika_leima . '" class="' . $tyyli . '" style="left: ' . $tunti_nro/$tunteja_vuorokaudessa*100 .   '%;width:67px">' . $teksti . '</a>';
       }
echo '
';
}
echo '

';

 //echo pageFooter(__FILE__);


Kiitokse

Koodin väkersi Elias H.

Mainos / Advertisement: