Skip to content

Instantly share code, notes, and snippets.

@ryo-utsunomiya
Last active February 20, 2016 01:01
Show Gist options
  • Select an option

  • Save ryo-utsunomiya/8002f3adb1a448172339 to your computer and use it in GitHub Desktop.

Select an option

Save ryo-utsunomiya/8002f3adb1a448172339 to your computer and use it in GitHub Desktop.

Revisions

  1. ryo-utsunomiya revised this gist Feb 20, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion todoist_template_editor.php
    Original file line number Diff line number Diff line change
    @@ -86,7 +86,7 @@ public function addSeries($taskName, $times = 1)
    throw new \InvalidArgumentException();
    }

    for ($i = 0; $i < $times; $i++) {
    for ($i = 1; $i <= $times; $i++) {
    $this->addTask(sprintf('%s %d/%d', $taskName, $i, $times));
    }
    }
  2. ryo-utsunomiya revised this gist Jan 30, 2016. 1 changed file with 9 additions and 5 deletions.
    14 changes: 9 additions & 5 deletions todoist_template_editor.php
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,16 @@
    //
    // todoist_template_editor.php
    //
    // Usage:
    // $ php todoist_template_editor.php 'do something' 10
    // Usage: $ php todoist_template_editor.php 'do something' 10
    // => output todoist template 'do something.csv'.

    if (!isset($argv[1])) {
    echo 'Usage: $ php todoist_template_editor.php \'do something\' 10' . PHP_EOL;
    exit(1);
    }

    $taskName = (string)$argv[1];
    $times = (int)$argv[2];
    $times = isset($argv[2]) ? (int)$argv[2] : 1;

    $editor = new TodoistTemplateEditor('Utsunomiya (4971726)');
    $editor->addSeries($taskName, $times);
    @@ -76,9 +80,9 @@ public function addTask($content, $priority = 4)
    * @param string $taskName
    * @param int $times
    */
    public function addSeries($taskName, $times)
    public function addSeries($taskName, $times = 1)
    {
    if ($times <= 0) {
    if ($times < 1) {
    throw new \InvalidArgumentException();
    }

  3. ryo-utsunomiya created this gist Jan 30, 2016.
    117 changes: 117 additions & 0 deletions todoist_template_editor.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,117 @@
    #!/usr/bin/env php
    <?php

    //
    // todoist_template_editor.php
    //
    // Usage:
    // $ php todoist_template_editor.php 'do something' 10
    // => output todoist template 'do something.csv'.

    $taskName = (string)$argv[1];
    $times = (int)$argv[2];

    $editor = new TodoistTemplateEditor('Utsunomiya (4971726)');
    $editor->addSeries($taskName, $times);
    $editor->write($taskName . '.csv');

    class TodoistTemplateEditor
    {
    /**
    * @var string
    */
    private $author;

    /**
    * @var array
    */
    private $table;

    /**
    * Constructor.
    *
    * @param string $author Author of task.
    */
    public function __construct($author)
    {
    $this->author = $author;
    $this->table = [
    [
    'TYPE',
    'CONTENT',
    'PRIORITY',
    'INDENT',
    'AUTHOR',
    'RESPONSIBLE',
    'DATE',
    'DATE_LANG'
    ],
    ];
    }

    /**
    * Add a task.
    *
    * @param string $content
    * @param int $priority
    */
    public function addTask($content, $priority = 4)
    {
    $line = [
    'task',
    (string)$content,
    $priority,
    1, // indent
    $this->author, // author
    '', // responsible
    '', // date
    'en', // date_lang
    ];
    $this->table[] = $line;
    }

    /**
    * Add series of task.
    *
    * @param string $taskName
    * @param int $times
    */
    public function addSeries($taskName, $times)
    {
    if ($times <= 0) {
    throw new \InvalidArgumentException();
    }

    for ($i = 0; $i < $times; $i++) {
    $this->addTask(sprintf('%s %d/%d', $taskName, $i, $times));
    }
    }

    /**
    * Write table content to CSV file.
    *
    * @param string $filePath Output csv file path.
    */
    public function write($filePath)
    {
    $fp = fopen($filePath, 'w');

    if (!$fp) {
    throw new \RuntimeException();
    }

    foreach ($this->table as $line) {
    fputcsv($fp, $line);
    }

    fclose($fp);
    }

    /**
    * Show all table data.
    */
    public function dump()
    {
    var_dump($this->table);
    }
    }