public
Last active

A Simple ToDo App Using PHP and MongoDB

  • Download Gist
mongo.todo.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#!/usr/bin/env php
<?php
 
error_reporting( E_ERROR | E_PARSE );
 
class ToDo {
 
protected $argv = array();
 
public function __construct ( $argv ) {
$this->argv = $argv;
try {
$this->mongo_connection = $mongo = new Mongo();
$this->mongo_db = $this->mongo_connection->todo;
}
catch ( Exception $e ) {
throw new Exception ( "Oops! Error connecting to MongoDB: " . $e->getMessage() );
}
}
 
protected function help ( $error = null ) {
if( ! is_null( $error ) ) {
print "$error\n";
print "\n";
}
 
$out = <<<EOF
usage: {$this->argv[0]} <method>
 
== Methods ==
 
<none> list all incomplete tasks sorted by priority then chronologically
help show this help
next list all incomplete tasks that are high priority
done list all complete tasks chronologically
high <argument> add high priority task called <argument>
low <argument> add low priority task called <argument>
finish <argument> complete task called <argument>
dont <argument> delete unfinished task called <argument>
 
EOF;
print $out;
}
 
public function run () {
try {
if ( 1 >= count( $this->argv ) ) {
$this->show(
array( "complete" => false ),
array( "level" => 1, "added" => 1 )
);
}
else {
$argument = strtolower( $this->argv[1] );
if ( "help" == $argument )
$this->help();
else if ( "next" == $argument )
$this->show(
array( "level" => "high", "complete" => false ),
array( "level" => 1, "added" => 1 )
);
else if ( "done" == $argument )
$this->show(
array( "complete" => array( '$ne' => false ) ),
array( 'completed' => 1 )
);
else if ( "high" == $argument )
$this->add( "high", $this->argv[2] );
else if ( "low" == $argument )
$this->add( "low", $this->argv[2] );
else if ( "finish" == $argument )
$this->complete( true, $this->argv[2] );
else if ( "dont" == $argument )
$this->complete( false, $this->argv[2] );
else
throw new Exception( "Bad Method" );
}
}
catch ( Exception $e ) {
$this->help( $e->getMessage() );
}
}
 
protected function show ( $params = null, $sort = null ) {
$cursor = $this->mongo_db->todos->find( $params );
if( ! is_null( $sort ) )
$cursor->sort( $sort );
foreach ( $cursor as $row )
print $row['task'] . "\n";
}
 
protected function add ( $level, $item ) {
if ( 2 >= count( $this->argv ) )
throw new Exception( "missing argument" );
$this->mongo_db->todos->insert( array(
"task" => $item,
"level" => $level,
"complete" => false,
"added" => time()
) );
}
 
protected function complete ( $finish, $item ) {
if ( 2 >= count( $this->argv ) )
throw new Exception( "missing argument" );
$document = $this->mongo_db->todos->findOne( array( "complete" => false, "task" => $item ) );
if ( is_null( $document ) )
print "No Matching ToDo Found\n";
else {
if ( $finish ) {
$document['complete'] = time();
$this->mongo_db->todos->save( $document );
}
else {
$this->mongo_db->todos->remove( $document );
}
}
}
 
} // Class ToDo
 
if( 'cli' == PHP_SAPI ) {
try {
$app = new ToDo( $argv );
$app->run();
}
catch( Exception $e ) {
print $e->getMessage() . "\n";
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.