2017 © Pedro Peláez
 

library stupidly-simple-calendar

An structured calendar generator in array, object or json format

image

alrik11es/stupidly-simple-calendar

An structured calendar generator in array, object or json format

  • Friday, May 19, 2017
  • by alrik11es
  • Repository
  • 1 Watchers
  • 10 Stars
  • 46 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 7 Versions
  • 2 % Grown

The README.md

Calendar Build Status

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

Calendar is a PHP calendar structure generator in array, object or JSON format., (*2)

Motivation

Do you remember those long... long... hours trying to create a rendered calendar? Think about this, the main problem is that you have to generate the structure to do the rendering in your template engine, no matter what it is. So the thing here is I don't wanna give you a fully rendered calendar just the structure you need to do the render. How do you do the render... it's all on you., (*3)

Take a look to this example render:, (*4)

Calendar example, (*5)

Installation

Requirements

  • Any flavour of PHP 5.6+ should do
  • [optional] PHPUnit to execute the test suite

With Composer

The easiest way to install the calendar is via composer. Create the following composer.json file and run the php composer.phar install command to install it., (*6)

{
    "require": {
        "alrik11es/calendar": "0.1.*"
    }
}

No composer

Git clone this repo where you want and include/require all the src folder into your project., (*7)

How it works

Lets get into business. Set up a calendar from today to 6 months into the future:, (*8)

$cal = new \SSC\Calendar();
$structure = $cal->getCalendar();

print_r($structure);

The output then is like the next one:, (*9)

Array
(
[2014] => Array
    (
    [type] => year
    [value] => 2014
    [data] =>
    [elements] => Array
        (
        [3] => Array
            (
            [type] => quarter
            [value] => 3
            [data] =>
            [elements] => Array
                (
                [8] => Array
                    (
                    [type] => month
                    [value] => 8
                    [data] =>
                    [elements] => Array
                        (
                        [31] => Array
                            (
                            [type] => week
                            [value] => 31
                            [data] =>
                            [elements] => Array
                                (
                                [1] => Array
                                    (
                                    [type] => day
                                    [value] => 1
                                    [data] => 
                                    [weekday] => 5
                                    )

Then you just have to take it and render in your Twig or whatever... (Next one is the spanish way, but you can change to whatever you want), (*10)

<?php foreach($structure as $year): ?>
    <?php foreach($year['elements'] as $quarter): ?>
        <?php foreach($quarter['elements'] as $month): ?>
            <div>
                <?php echo $year['value']; ?> - <?php echo $month['value']; ?>
                <table>
                    <tr>
                        <th>Mon</th>
                        <th>Tue</th>
                        <th>Wed</th>
                        <th>Thu</th>
                        <th>Fra</th>
                        <th>Sun</th>
                        <th>Sat</th>
                    </tr>
                    <?php foreach($month['elements'] as $week): ?>
                        <tr>
                            <?php foreach(array(1,2,3,4,5,6,0) as $weekday): ?>
                                <td>
                                <?php foreach($week['elements'] as $day): ?>
                                    <?php if($day['weekday'] == $weekday): ?>
                                        <?php echo $day['value'];?>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                                </td>
                            <?php endforeach; ?>
                        </tr>
                    <?php endforeach; ?>
                </table>
            </div>
        <?php endforeach; ?>
    <?php endforeach; ?>
<?php endforeach; ?>

Bam! Calendar!

Oh BTW I can give you a Twig example Just for the record :P, (*11)

$cal = new \SSC\Calendar();
$cal->getConfig()->setInterval(new \DateInterval('P12M'));
$cal->day_callback = function($date){
    $day = new \stdClass();
    $day->has_passed = $date->getTimestamp()<time();
    return $day;
};

// Spanish months (There is tons of ways of doing this but...)
$context['months_es'] = array(
    1=>'Enero', 'Febrero', 'Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'
);
// The week days order. In spanish calendar this is different than in english.
$context['week_days'] = array(1,2,3,4,5,6,0);
// The calendar structure...
$context['cal'] = $cal->getCalendarStructure();

Yeah, the Twig file..., (*12)

{% for year in cal %}
    {# You could add here the years #}
    {% for quarter in year.elements %}
        {% for month in quarter.elements %}


{% for week in month.elements %} {# You could add here the week number #} {% for week_day in week_days%} {% endfor %} {% endfor %}
{{ year.value }} - {{ months_es[month.value] }}
L M X J V S D
{% for day in week.elements %} {% if day.weekday == week_day %}
{{ day.value }}
{% endif %} {% endfor %}
{% endfor %} {% endfor %} {% endfor %}

Vue.js

With the boom of this kind of libraries just like angular or react. I give you an example of how a render should look like. Obviously you will need to make the API part. But at least this is a good starting point., (*13)

<template>

    <div>
        <div v-for="year in calendar">
            <!--<div>{{year.value}}</div>-->
            <div v-for="quarter in year.elements">
                <div v-for="month in quarter.elements" class="month">
                    {{ year.value }} - {{ month.value }}
                    <table>
                        <tr>
                            <th>Mon</th>
                            <th>Tue</th>
                            <th>Wed</th>
                            <th>Thu</th>
                            <th>Fra</th>
                            <th>Sun</th>
                            <th>Sat</th>
                        </tr>
                        <tr v-for="week in month.elements">
                            <td v-for="weekday in [1,2,3,4,5,6,0]">
                                <div v-for="day in week.elements">
                                    <span v-if="day.weekday == weekday">
                                        {{ day.value }}
                                    </span>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

</template>








<style scoped lang="sass">
    .month{
        float: left;
        margin: 10px;
    }
</style>

The Versions

19/05 2017

1.0.x-dev

1.0.9999999.9999999-dev

An structured calendar generator in array, object or json format

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

calendar generator

19/05 2017

1.0.0

1.0.0.0

An structured calendar generator in array, object or json format

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

calendar generator

19/05 2017

dev-master

9999999-dev

An structured calendar generator in array, object or json format

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

calendar generator

18/04 2017

0.1.2

0.1.2.0

An structured calendar generator in array, object or json format

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

calendar generator

24/11 2014

0.1.1

0.1.1.0

An structured calendar generator in array, object or json format

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

calendar generator ssc

10/08 2014

0.1

0.1.0.0

An structured calendar generator in array, object or json format

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

calendar generator ssc

10/08 2014

0.1-RC1

0.1.0.0-RC1

An structured calendar generator in array, object or json format

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

calendar generator ssc