-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto-do-list-Code-Challenge-Jonah-Larson-2017.php
More file actions
112 lines (100 loc) · 4.45 KB
/
to-do-list-Code-Challenge-Jonah-Larson-2017.php
File metadata and controls
112 lines (100 loc) · 4.45 KB
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
<?php
/*
Plugin Name: To Do List Code Challenge Jonah Larson 2017
Author: Jonah Larson
Description: A To Do List plugin desigin to track and inform users on a list of to-do objectives
Version: 1.0
License: GPL
*/
function tdlcc_install()
{
global $wpdb;
$table_name = 'wp_todoitems';
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
//table not in database. Create new table
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
title text NOT NULL,
description text NOT NULL,
isCompleted boolean NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
else{
}
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'tdlcc_install' );
wp_enqueue_style( 'style', plugins_url( '/admin/css/ToDo.css', __FILE__ ), get_stylesheet_uri());
function tdlcc_shortcodes_init()
{
function tdlcc_shortcode($atts = [], $content = null)
{
// do something to $content
$header = '<h1> To Do List</h1>';
//sets up the textboxes and add option
$titleTextArea = '<textarea rows="1" cols="20" id="wptd_title" onfocus="clearContents(this);">Title for your new To Do Items</textarea>';
$descriptionTextArea = '<textarea rows="1" cols="20" id="wptd_description" onfocus="clearContents(this);">To Do description</textarea><div id="addToDo">Add Item<span class="dashicons dashicons-plus custom-dashicon"></span></div>';
//sets up the angular app
$angularapphtml = '<div data-ng-app="myApp" data-ng-controller="itemCtrl"><table>';
//active to do items come here
$angularapphtml .= '<tr ng-repeat="item in items" data-ng-if="item.isCompleted == 0"><td><h4>{{item.title}}</h4><h5>{{item.description}}</h5></td><td><input type="radio" id="item{{item.id}}"></input></td></tr>';
//inactive to do items come here
$angularapphtml .= '<tr ng-repeat="item in items" data-ng-if="item.isCompleted == 1"><td class="completed"><h4>{{item.title}}</h4><h5>{{item.description}}</h5></td><td></td></tr>';
$angularapphtml .= '</table></div>';
$content = $header . $titleTextArea . $descriptionTextArea . '<form id="checkboxes">' . $angularapphtml . '</form>';
// always return
return $content;
}
add_shortcode('tdlcc', 'tdlcc_shortcode');
}
function ajaxUpdate() {
global $wpdb;
$table_name = 'wp_todoitems';
//updates the ToDo item in the database
$wpdb->update( $table_name, array( 'isCompleted' => 1), array( 'id' => $_POST['item'] ), array('%d'), array('%s'));
die();
};
add_action('wp_ajax_nopriv_ajaxUpdate', 'ajaxUpdate');
add_action('wp_ajax_ajaxUpdate', 'ajaxUpdate');
function ajaxConversion() {
global $wpdb;
$table_name = 'wp_todoitems';
//adds a new to do item to the database
$wpdb->insert( $table_name, array( 'title' => $_POST['title'], 'description' => $_POST['description'] ) );
die();
};
add_action('wp_ajax_nopriv_ajaxConversion', 'ajaxConversion');
add_action('wp_ajax_ajaxConversion', 'ajaxConversion');
function updateToDo(){
global $wpdb;
$items = $wpdb->get_results("SELECT * FROM wp_todoitems ORDER BY isCompleted, id DESC;");
$jsonArray = array();
foreach($items as $item){
//creates an array object formatted for my angular controller
$jsonArray[] = array('title' => $item->title,
'description' =>$item->description,
'isCompleted' => $item->isCompleted,
'id'=> $item->id);
}
//json encode and return finished array of angular items
echo json_encode($jsonArray);
die;
}
add_action('wp_ajax_nopriv_updateToDo', 'updateToDo');
add_action('wp_ajax_updateToDo', 'updateToDo');
//jquery and angular scripts
wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js');
wp_enqueue_script('angular', 'http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js', array(), null, false);
//my regular java script and my angular script
wp_enqueue_script('ToDo', plugins_url('/admin/js/ToDo.js', __FILE__));
wp_enqueue_script('ToDoAngular', plugins_url('/admin/js/AngularApp/displayMatchModule.js', __FILE__));
//allowing both scripts to access the ajax_object.ajax_url
wp_localize_script( 'ToDo', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
wp_localize_script( 'ToDoAngular', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
//short code initializer
add_action('init', 'tdlcc_shortcodes_init');
?>