function wordpress_admin_init()
 {
     $page = isset($_GET['page']) ? $_GET['page'] : '';
     if ($page == 'project_tasks' || $page == 'project_tasks_processes') {
         project_tasks_general::load_scripts_and_styles();
     }
 }
 function wordpress_init()
 {
     global $wp_admin_bar;
     if (!empty($wp_admin_bar)) {
         // enqueue scripts used then adminbar is on
         wp_enqueue_script('jquery');
         project_tasks_general::load_scripts_and_styles();
         add_action('wp_head', array($this, 'set_params_for_public_javascript'));
     }
 }
 function save_task_form()
 {
     global $project_tasks;
     global $wpdb;
     $current_user = wp_get_current_user();
     $task_id = isset($_POST['task_id']) ? $_POST['task_id'] : 0;
     $num_targets = isset($_POST['num_targets']) ? intval($_POST['num_targets']) : 0;
     $num_subtasks = isset($_POST['num_subtasks']) ? intval($_POST['num_subtasks']) : 0;
     $ignore_targets = isset($_POST['ignore_targets']) ? true : false;
     $do_update = false;
     $task_log = '';
     $new_log_entry_intro = date('d-m-Y') . ': ' . $current_user->display_name;
     $db_task_item_columns = array('created_date' => time(), 'last_action_date' => time(), 'creator' => get_current_user_id(), 'parent_task' => isset($_POST['parent_task']) ? $_POST['parent_task'] : '0', 'assigned_to' => isset($_POST['task_taskassignedto']) ? $_POST['task_taskassignedto'] : '', 'priority' => isset($_POST['task_priority']) ? $_POST['task_priority'] : '5', 'due_date' => isset($_POST['task_duedate']) ? trim($_POST['task_duedate']) : '', 'progress' => isset($_POST['task_progress']) ? trim($_POST['task_progress']) : '', 'type' => isset($_POST['task_tasktype']) ? $_POST['task_tasktype'] : '', 'title' => isset($_POST['task_title']) ? $_POST['task_title'] : '', 'description' => isset($_POST['task_description']) ? $_POST['task_description'] : '', 'notes' => isset($_POST['task_notes']) ? $_POST['task_notes'] : '', 'status' => isset($_POST['task_status']) ? $_POST['task_status'] : '', 'log' => '');
     //$db_task_item_columns_format = array ( '%d', '%d', '%d', '%d', '%d','%d', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s' );
     // Some validation and checks
     project_tasks_general::validate_field($db_task_item_columns['title'], 'mandatory', '- You must write something in the title field.');
     project_tasks_general::validate_field($db_task_item_columns['progress'], 'number', '- Progress must be a number.');
     if ($db_task_item_columns['due_date'] == 'DD-MM-YYYY') {
         $db_task_item_columns['due_date'] = '0';
     } else {
         if ($db_task_item_columns['due_date'] != '') {
             project_tasks_general::validate_field($db_task_item_columns['due_date'], 'date', '- Due date must be a valid date (DD-MM-YYYY).');
             $db_task_item_columns['due_date'] = strtotime($db_task_item_columns['due_date']);
             // Translate due date into a unix time stamp
         }
     }
     // insert the task item into database (%s as string; %d as decimal number and %f as float)
     if ($task_id == 0) {
         $db_task_item_columns['log'] = $new_log_entry_intro . ' created this task';
         $wpdb->insert($project_tasks->data->tasks_table_name, $db_task_item_columns, array('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s'));
         $task_id = $wpdb->insert_id;
         // or get the previous saved task and compare what has been changed to write to the log
     } else {
         $do_update = true;
         $task = $wpdb->get_results('SELECT * FROM ' . $project_tasks->data->tasks_table_name . ' WHERE ID = ' . $task_id);
         if (isset($task[0])) {
             if ($task[0]->assigned_to != $db_task_item_columns['assigned_to']) {
                 $task_log .= '\\n' . $new_log_entry_intro . ' changed "assigned to" to ' . $db_task_item_columns['assigned_to'] == '0' ? 'NOT ASSIGNED' : project_tasks_general::find_name_in_array($project_tasks->data->get_users(), $db_task_item_columns['assigned_to']);
             }
             if ($task[0]->type != $db_task_item_columns['type']) {
                 $task_log .= '\\n' . $new_log_entry_intro . ' changed "type" to ' . $db_task_item_columns['type'];
             }
             if ($task[0]->title != $db_task_item_columns['title']) {
                 $task_log .= '\\n' . $new_log_entry_intro . ' changed "title" to "' . $db_task_item_columns['title'] . '"';
             }
             if ($task[0]->description != $db_task_item_columns['description']) {
                 $task_log .= '\\n' . $new_log_entry_intro . ' changed "description"';
             }
             if ($task[0]->notes != $db_task_item_columns['notes']) {
                 $task_log .= '\\n' . $new_log_entry_intro . ' changed "notes"';
             }
             if ($task[0]->due_date != $db_task_item_columns['due_date']) {
                 $task_log .= '\\n' . $new_log_entry_intro . ' changed "due date" to ' . date('d/m/Y', $db_task_item_columns['due_date']);
             }
             if ($task[0]->progress != $db_task_item_columns['progress']) {
                 $task_log .= '\\n' . $new_log_entry_intro . ' changed "progress" to ' . $db_task_item_columns['progress'];
             }
             if ($task[0]->status != $db_task_item_columns['status']) {
                 $task_log .= '\\n' . $new_log_entry_intro . ' changed "status" to ' . $db_task_item_columns['status'];
             }
             if ($task[0]->priority != $db_task_item_columns['priority']) {
                 $task_log .= '\\n' . $new_log_entry_intro . ' changed "priority" to ' . $db_task_item_columns['priority'];
             }
             if ($task[0]->parent_task != $db_task_item_columns['parent_task']) {
                 if ($db_task_item_columns['parent_task'] == '0') {
                     $task_log .= '\\n' . $new_log_entry_intro . ' removed connection to parent task';
                 } else {
                     $task_log .= '\\n' . $new_log_entry_intro . ' set this task as a child task to task no ' . $db_task_item_columns['parent_task'];
                 }
             }
         }
     }
     // loop thru every target and save it to the database
     if ($ignore_targets == false) {
         for ($itarget = 1; $itarget <= $num_targets; $itarget++) {
             $targets_type = isset($_POST['spt_target_' . $itarget . '_type']) ? $_POST['spt_target_' . $itarget . '_type'] : '';
             $targets_id = isset($_POST['spt_target_' . $itarget . '_id']) ? $_POST['spt_target_' . $itarget . '_id'] : '';
             $targets_checked = isset($_POST['spt_target_' . $itarget . '_selected']) ? $_POST['spt_target_' . $itarget . '_selected'] : '0';
             $targets_item_name = isset($_POST['spt_target_' . $itarget . '_name']) ? $_POST['spt_target_' . $itarget . '_name'] : '';
             //echo '<br>$targets_type: ' . $targets_type . ', $targets_id: ' . $targets_id . ', $targets_checked: ' . $targets_checked . ', $targets_item_name: ' . $targets_item_name;
             // check wheather the connection between task and target allready exists in the database
             $target_exists = $wpdb->get_var('SELECT COUNT(*) FROM ' . $project_tasks->data->task_relation_table_name . ' WHERE task=' . $task_id . ' AND target_type=\'' . $targets_type . '\' AND target_id=\'' . $targets_id . '\';');
             // insert or update database
             if ($targets_checked == '1') {
                 // prepare data for target
                 $db_task_target_columns = array('task' => $task_id, 'target_type' => $targets_type, 'target_id' => $targets_id);
                 $db_task_target_columns_format = array('%d', '%s', '%s');
                 if ($target_exists) {
                     // Update database, NOT IMPLEMENTED NOW BECAUSE THERE ARE NO FIELDS TO UPDATE
                 } else {
                     $wpdb->insert($project_tasks->data->task_relation_table_name, $db_task_target_columns, $db_task_target_columns_format);
                     $task_log .= '\\n' . $new_log_entry_intro . ' added target ' . project_tasks_general::find_name_in_array($project_tasks->data->get_targets(), $targets_type, 'target_type') . ' ' . $targets_item_name;
                 }
                 // delete target from database
             } else {
                 if ($target_exists) {
                     $rows_affected = $wpdb->query('DELETE FROM ' . $project_tasks->data->task_relation_table_name . ' WHERE task=' . $task_id . ' AND target_type=\'' . $targets_type . '\' AND target_id=\'' . $targets_id . '\'');
                     if ($rows_affected) {
                         $task_log .= '\\n' . $new_log_entry_intro . ' removed target ' . project_tasks_general::find_name_in_array($project_tasks->data->get_targets(), $targets_type, 'target_type') . ' ' . $targets_item_name;
                     }
                 }
             }
         }
     }
     // or, update the task item in the database ( and don't update when the task was created and who was the creator )
     if ($do_update == true) {
         unset($db_task_item_columns['created_date']);
         unset($db_task_item_columns['creator']);
         $db_task_item_columns['log'] = $wpdb->get_var('SELECT log FROM ' . $project_tasks->data->tasks_table_name . ' WHERE id=' . $task_id . ';') . $task_log;
         $wpdb->update($project_tasks->data->tasks_table_name, $db_task_item_columns, array('id' => $task_id), array('%d', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s'), array('%d'));
     }
     echo 'OK';
 }
        function render_list($args = null)
        {
            $defaults = array('post_id' => isset($_POST['post_id']) ? $_POST['post_id'] : '0', 'parent_task' => '0', 'show_subtasks' => '1', 'show_controls' => '1', 'list_id' => 'project_tasks_list', 'num_tasks_per_page' => isset($_POST['num_tasks_per_page']) ? $_POST['num_tasks_per_page'] : '20', 'page_num' => isset($_POST['page_num']) ? $_POST['page_num'] : '1', 'show_only_type' => isset($_COOKIE['show_only_type']) && $_COOKIE['show_only_type'] != '' ? $_COOKIE['show_only_type'] : 'Everything', 'show_only_mine' => isset($_COOKIE['show_only_mine']) && $_COOKIE['show_only_mine'] != '' ? $_COOKIE['show_only_mine'] : '0', 'show_only_open' => isset($_COOKIE['show_only_open']) && $_COOKIE['show_only_open'] != '' ? $_COOKIE['show_only_open'] : '0', 'columns' => array('title', 'targets', 'priority', 'creator', 'assigned_to', 'progress', 'status', 'due_date'));
            $r = wp_parse_args($args, $defaults);
            global $project_tasks;
            global $wpdb;
            $num_columns = count($r['columns']);
            $offset = ($r['page_num'] - 1) * $r['num_tasks_per_page'];
            $users = $project_tasks->data->get_users();
            $all_targets = $project_tasks->data->get_targets();
            $all_status = $project_tasks->data->get_status();
            $task_types = $project_tasks->data->get_task_types();
            $the_list = array();
            $this_list_ids = '';
            ?>
			
			<div id="<?php 
            echo $r['list_id'];
            ?>
" class="project_tasks_list">
				<table cellpadding="0" cellspacing="0">
					<thead>
						<tr><?php 
            foreach ($r['columns'] as $column) {
                switch ($column) {
                    case 'title':
                        echo '<td class="spt_title">';
                        if ($r['show_only_type'] == 'Everything') {
                            echo 'Title';
                        } else {
                            foreach ($task_types as $the_task_type) {
                                if ($r['show_only_type'] == $the_task_type['id']) {
                                    echo $the_task_type['name'];
                                }
                            }
                        }
                        echo '</td>';
                        break;
                    case 'targets':
                        echo '<td class="spt_targets" id="spt_targets_hd">Targets</td>';
                        break;
                    case 'assigned_to':
                        echo '<td class="spt_assigned_to" id="spt_assigned_to_hd">Assigned to</td>';
                        break;
                    case 'progress':
                        echo '<td class="spt_progress" id="spt_progress_hd">Progress</td>';
                        break;
                    case 'status':
                        echo '<td class="spt_status" id="spt_status_hd">Status</td>';
                        break;
                    case 'due_date':
                        echo '<td class="spt_due_date" id="spt_due_date_hd">Due Date</td>';
                        break;
                    case 'creator':
                        echo '<td class="spt_creator" id="spt_creator_hd">Created by</td>';
                        break;
                    case 'priority':
                        echo '<td class="spt_priority" id="spt_priority_hd">Priority</td>';
                        break;
                }
            }
            ?>
					</thead>
					<tbody><?php 
            $previous_target_type = '';
            $sql_where = '';
            $target_count = 0;
            $last_type = '';
            $first_row = true;
            $task_count = 0;
            // Build ths SQL String
            $sql = 'SELECT * ';
            foreach ($all_targets as $target) {
                if ($previous_target_type != $target['target_type']) {
                    $target_count++;
                    $sql .= ', (select count(*) from ' . $project_tasks->data->task_relation_table_name . ' where task = ' . $project_tasks->data->tasks_table_name . '.id AND target_type = "' . $target['target_type'] . '") as target' . $target_count . ' ';
                    $previous_target_type = $target['target_type'];
                }
            }
            $sql .= ' FROM ' . $project_tasks->data->tasks_table_name;
            if ($r['show_only_mine'] != '0') {
                $sql_where .= ' assigned_to = ' . get_current_user_id();
            }
            if ($r['show_only_open'] != '0') {
                $sql_where .= ($sql_where != '' ? ' AND ' : '') . ' status <> \'spt_status_closed\' AND status <> \'spt_status_removed\'';
            }
            if ($r['show_only_type'] != 'Everything') {
                $sql_where .= ($sql_where != '' ? ' AND ' : '') . ' type = \'' . $r['show_only_type'] . '\'';
            }
            if ($r['post_id'] != '0') {
                $sql_where .= ($sql_where != '' ? ' AND ' : '') . $project_tasks->data->tasks_table_name . '.ID IN ( SELECT task FROM ' . $project_tasks->data->task_relation_table_name . ' WHERE ' . $project_tasks->data->get_sql_where_for_post_tasks($r['post_id']) . ')';
            }
            $sql_where .= ($sql_where != '' ? ' AND ' : '') . ' parent_task = ' . $r['parent_task'];
            if ($sql_where != '') {
                $sql .= ' WHERE ' . $sql_where;
            }
            $sql .= ' ORDER BY type, id DESC';
            $all_tasks = $wpdb->get_results($sql);
            $num_all_tasks = count($all_tasks);
            // Get data from database, get all tasks
            $tasks = $wpdb->get_results($sql . ' LIMIT ' . $offset . ', ' . $r['num_tasks_per_page'] . '');
            // loop thru all task in the list and store them in an array and the id's in a comma separated string
            foreach ($tasks as $task) {
                $task_count++;
                $the_list_html = '';
                if ($task->type != $last_type && $r['show_only_type'] == 'Everything') {
                    $the_list_html .= '<tr class="spt_task_type_row' . ($first_row === true ? ' spt_r_firstrow' : '') . '"><td colspan="' . $num_columns . '">';
                    $the_list_html .= project_tasks_general::find_name_in_array($task_types, $task->type, 'id', 'plural', '[Unknown Type]');
                    $the_list_html .= '</td></tr>';
                    $last_type = $task->type;
                    $first_row = false;
                }
                $taskstatus = project_tasks_general::find_name_in_array($all_status, $task->status);
                $the_list_html .= '<tr class="spt_task_row spt_task_status_' . $taskstatus . ($first_row === true ? ' spt_r_firstrow' : '') . '" taskid="' . $task->id . '">';
                $first_row = false;
                foreach ($r['columns'] as $column) {
                    switch ($column) {
                        case 'title':
                            $the_list_html .= '<td class="spt_title">' . $task->id . '. ' . $task->title . '</td>';
                            break;
                        case 'targets':
                            $targets_html = '';
                            $previous_target_type = '';
                            $target_count = 0;
                            foreach ($all_targets as $target) {
                                if ($previous_target_type != $target['target_type']) {
                                    $target_count++;
                                    if ($task->{'target' . $target_count}) {
                                        $targets_html .= '<span title="' . $target['name'] . '">' . $target['shortening'] . '</span>';
                                    }
                                    $previous_target_type = $target['target_type'];
                                }
                            }
                            $the_list_html .= '<td class="spt_targets">' . ($targets_html == '' ? '&nbsp;' : $targets_html) . '</td>';
                            break;
                        case 'assigned_to':
                            $the_list_html .= '<td>' . project_tasks_general::nbsp(project_tasks_general::find_name_in_array($users, $task->assigned_to), '- none -') . '</td>';
                            break;
                        case 'progress':
                            $the_list_html .= '<td>' . project_tasks_general::nbsp($task->progress) . '%</td>';
                            break;
                        case 'status':
                            $the_list_html .= '<td>' . project_tasks_general::nbsp($taskstatus) . '</td>';
                            break;
                        case 'due_date':
                            $the_list_html .= '<td>' . project_tasks_general::nbsp($task->due_date != '0' ? date('d', $task->due_date) . '/' . date('m', $task->due_date) . '/' . date('Y', $task->due_date) : '') . '</td>';
                            break;
                        case 'creator':
                            $the_list_html .= '<td>' . project_tasks_general::nbsp(project_tasks_general::find_name_in_array($users, $task->creator)) . '</td>';
                            break;
                        case 'priority':
                            $the_list_html .= '<td>' . project_tasks_general::nbsp($task->priority) . '</td>';
                            break;
                    }
                }
                $the_list_html .= '</tr>';
                $the_list[$task->id] = array('task' => $the_list_html, 'subtasks' => '');
                $this_list_ids .= $task->id . ',';
            }
            // SUB TASKS IS NOT IMPLEMENTED YET
            // Make another request to the database, get all sub tasks for the current list
            /*if ( $r['show_subtasks'] != '0' ) {
            			if ( count($this_list_ids) > 0 ) $this_list_ids = substr( $this_list_ids, 0, -1 );
            			$sub_tasks = $wpdb->get_results( 'SELECT title, id, parent_task, assigned_to from ' . $project_tasks->data->tasks_table_name . ' where parent_task in (' . $this_list_ids . ') ORDER BY parent_task ' );
            			echo $this_list_ids;
            			var_dump($sub_tasks);
            			foreach ( $sub_tasks as $sub_task ) {
            				if ( isset( $the_list[ $sub_task->parent_task ] ) ) {
            					$the_list[ $sub_task->parent_task ]['subtasks'] .= '<span class="" taskid="' . $sub_task->id . '">' . $sub_task->title . ' (' . project_tasks_general::find_name_in_array ( $project_tasks->data->get_users (),  $sub_task->assigned_to ) . ')</span>';
            				}
            			}
            		}*/
            // loop thru the list and output it
            foreach ($the_list as $list_item) {
                echo $list_item['task'];
                // SUB TASKS IS NOT IMPLEMENTED YET
                /*if ( $list_item['subtasks'] != '' ) {
                			echo '<tr class="spt_subtaskslist"><td colspan="' . $num_columns . '">' . $list_item['subtasks'] . '</td></tr>';
                		}*/
            }
            ?>
					</tbody>
				</table><?php 
            if ($r['show_controls'] == '1') {
                ?>
				
					<div class="spt_action_buttons"><?php 
                $num_pages = ceil($num_all_tasks / $r['num_tasks_per_page']);
                if ($num_pages > 1) {
                    echo '<div id="spt_list_pagination">';
                    for ($ipage = 1; $ipage <= $num_pages; $ipage++) {
                        echo '<span' . ($r['page_num'] == $ipage ? ' class="active"' : '') . ' id="spt_list_page_' . $ipage . '">' . $ipage . '</span>';
                    }
                    echo '</div>';
                }
                ?>
						<span id="spt_add_new_task" class="spt_action_button spt_button_primary">Add New Task</span>
						<span id="spt_refresh_list" class="spt_action_button">Refresh</span><!--spt_action_button -->
						<div id="spt_action_alts">
							<div id="spt_action_altchecks">
								<label for="show_only_mine"><input type="checkbox" name="show_only_mine" id="show_only_mine" value="1" <?php 
                if ($r['show_only_mine'] == '1') {
                    echo ' checked="checked"';
                }
                ?>
>that are assigned to me</label>
								<label for="show_only_open"><input type="checkbox" name="show_only_open" id="show_only_open" value="1" <?php 
                if ($r['show_only_open'] == '1') {
                    echo ' checked="checked"';
                }
                ?>
>and Open.</label>
							</div>
							<div id="spt_action_altdrop">
								<label for="show_onlye_type">Show: 
									<select name="show_onlye_type" id="show_onlye_type">
										<option value="Everything" <?php 
                if ($r['show_only_type'] == 'Everything') {
                    echo ' selected';
                }
                ?>
>Everything</option><?php 
                // display dropdown of task types
                foreach ($task_types as $the_task_type) {
                    echo '<option value="' . $the_task_type['id'] . '"' . ($r['show_only_type'] == $the_task_type['id'] ? ' selected="selected"' : '') . '>' . $the_task_type['plural'] . '</option>';
                }
                ?>
									</select>
								</label>
							</div>
						</div>
					</div><?php 
            }
            ?>

			</div>
			<script language="javascript">spt_number_of_items_in_list = <?php 
            echo count($tasks);
            ?>
;</script><?php 
        }