示例#1
0
/**
 * Get a project user role by project id
 *
 * @param int $project_id
 * @return string
 */
function cpm_project_user_role($project_id)
{
    $user_id = get_current_user_id();
    $cache_key = 'cpm_project_user_role_' . $project_id . $user_id;
    $project_user_role = wp_cache_get($cache_key);
    if ($project_user_role === false) {
        $project_user_role = cpm_project_user_role_pre_chache($project_id);
        wp_cache_set($cache_key, $project_user_role);
    }
    return $project_user_role;
}
 /**
  * Create a new todolist.
  *
  * @param int $project_id
  * @param array $data
  *
  *  - tasklist_name (string, required)
  *  - tasklist_detail (string)
  *  - tasklist_privacy (string)
  *  - tasklist_milestone (integer)
  *
  *  - Method: POST
  *  - URL: http://example.com/cpm-json/projects/project_id/lists
  *
  * @since 1.2
  * @return array $response
  */
 public function create_list($project_id, $data)
 {
     $project_id = (int) $project_id;
     $list_id = (int) $list_id;
     if (!$project_id) {
         return new WP_Error('json_post_invalid_id', __('Invalid project ID.'), array('status' => 404));
     }
     $manage_capability = cpm_manage_capability();
     if (!$manage_capability && !cpm_is_single_project_manager($project_id)) {
         if (!cpm_project_user_role_pre_chache($project_id)) {
             return new WP_Error('permission', __('Sorry! you are not assigned in this project', 'cpm'), array('status' => 404));
         }
         if (!cpm_user_can_access($project_id, 'create_todolist')) {
             return new WP_Error('permission', __('Sorry! you do not have permission to create todo list', 'cpm'), array('status' => 404));
         }
     }
     if (empty($data['tasklist_name'])) {
         return new WP_Error('task_list_name', __('Task List Name Required', 'cpm'));
     }
     $data['tasklist_milestone'] = isset($data['tasklist_milestone']) ? $data['tasklist_milestone'] : '-1';
     $data['tasklist_privacy'] = isset($data['tasklist_privacy']) ? $data['tasklist_privacy'] : 'no';
     $list_id = cpm()->task->add_list($project_id, $data);
     $get_list = cpm()->task->get_task_list($list_id);
     $response = new WP_JSON_Response();
     $response->set_data($get_list);
     return $response;
 }
 /**
  * Create a new milestone.
  *
  * @param int $project_id
  * @param array $data
  *
  *  - milestone_name (string, required)
  *  - milestone_due (date format)
  *  - milestone_detail (string)
  *  - milestone_privacy (string)
  *
  *  - Method: POST
  *  - URL: http://example.com/cpm-json/projects/project_id/milestones
  *
  * @since 1.2
  * @return array $response
  */
 public function create_milestone($project_id, $data)
 {
     $project_id = intval($project_id);
     if (!$project_id) {
         return new WP_Error('json_post_invalid_id', __('Invalid project ID.'), array('status' => 404));
     }
     if (isset($data['milestone_name']) && empty($data['milestone_name'])) {
         return new WP_Error('milestone_name', __('Milestone Name Required', 'cpm'));
     }
     $manage_capability = cpm_manage_capability();
     if (!$manage_capability && !cpm_is_single_project_manager($project_id)) {
         if (!cpm_project_user_role_pre_chache($project_id)) {
             return new WP_Error('permission', __('Sorry! you are not assigned in this project', 'cpm'), array('status' => 404));
         }
         if (!cpm_user_can_access($project_id, 'create_milestone')) {
             return new WP_Error('permission', __('Sorry! you do not have permission to create milestone', 'cpm'), array('status' => 404));
         }
     }
     $milestone_id = cpm()->milestone->create($project_id, $data);
     $get_milestone = cpm()->milestone->get($milestone_id);
     $response = new WP_JSON_Response();
     $response->set_data($get_milestone);
     return $response;
 }
 /**
  * Check comment create permission
  *
  * @param int $project_id
  * @param int $post_id
  *
  * @since 1.2
  * @return boolen | array
  */
 function check_create_comment_permission($project_id, $post_id)
 {
     $post_id = intval($post_id);
     $project_id = intval($project_id);
     if (!$post_id) {
         return array('key' => 'json_post_invalid_id', 'message' => __('Invalid post ID.'));
     }
     if (!$project_id) {
         return array('key' => 'json_post_invalid_id', 'message' => __('Invalid project ID.'));
     }
     $post = get_post($post_id);
     $post_type = $post->post_type;
     switch ($post_type) {
         case 'message':
             $manage_capability = cpm_manage_capability();
             if ($manage_capability || cpm_is_single_project_manager($project_id)) {
                 return true;
             } else {
                 if (!cpm_project_user_role_pre_chache($project_id)) {
                     return array('key' => 'permission', 'message' => __('Sorry! you are not assigned in this project', 'cpm'));
                 } else {
                     if (!cpm_user_can_access($project_id, 'create_message')) {
                         return array('key' => 'permission', 'message' => __('Sorry! you do not have permission to view this comments', 'cpm'));
                     }
                 }
             }
             break;
         case 'task':
             $manage_capability = cpm_manage_capability();
             if ($manage_capability || cpm_is_single_project_manager($project_id)) {
                 $condition = true;
             } else {
                 if (!cpm_project_user_role_pre_chache($project_id)) {
                     return array('key' => 'permission', 'message' => __('Sorry! you are not assinged in this project', 'cpm'), array('status' => 404));
                 } else {
                     if (!cpm_user_can_access($project_id, 'tdolist_view_private')) {
                         return array('key' => 'permission', 'message' => __('Sorry! you do no have permission to view this comment', 'cpm'), array('status' => 404));
                     } else {
                         if (!cpm_user_can_access($project_id, 'create_todo')) {
                             return array('key' => 'permission', 'message' => __('Sorry! you do no have permission to view this comment', 'cpm'), array('status' => 404));
                         }
                     }
                 }
             }
             break;
         case 'task_list':
             $manage_capability = cpm_manage_capability();
             if ($manage_capability || cpm_is_single_project_manager($project_id)) {
                 return true;
             } else {
                 if (!cpm_project_user_role_pre_chache($project_id)) {
                     return array('key' => 'permission', 'message' => __('Sorry! you are not assinged in this project', 'cpm'), array('status' => 404));
                 } else {
                     if (!cpm_user_can_access($project_id, 'create_todolist')) {
                         return array('key' => 'permission', 'message' => __('Sorry! you do no have permission to view this comment', 'cpm'), array('status' => 404));
                     }
                 }
             }
             break;
     }
     return true;
 }
 /**
  * Delete project
  *
  * @param int $project_id
  * @param boolen $force
  *
  * - URL: http://example.com/cpm-json/projects/project_id/?force=1
  * - Method: DELETE
  *
  * @since 1.2
  * @return array
  */
 public function delete_project($project_id, $force = false)
 {
     $id = intval($project_id);
     if (!$id) {
         return new WP_Error('json_post_invalid_id', __('Invalid project ID.'), array('status' => 404));
     }
     $manage_capability = cpm_manage_capability();
     if (!$manage_capability && !cpm_project_user_role_pre_chache($id)) {
         return new WP_Error('permission', __('Sorry! you are not assigned in this project', 'cpm'), array('status' => 404));
     }
     if (!cpm_user_can_access($id)) {
         return new WP_Error('project_edit_capability', __('You do not have permission to delete this project', 'cpm'));
     }
     $post = get_post($id, ARRAY_A);
     if (empty($post['ID'])) {
         return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404));
     }
     cpm()->project->delete($id, $force);
     if ($force) {
         return array('message' => __('Permanently deleted post'));
     } else {
         return array('message' => __('Deleted post'));
     }
 }
 /**
  * Create a new task.
  *
  * @param int $project_id
  * @param int $list_id
  * @param array $data
  *
  *  - task_text (string, required)
  *  - task_privacy (string)
  *  - task_assign (array)
  *  - task_due (date format)
  *  - task_start (date format)
  *
  *  - Method: POST
  *  - URL: http://example.com/cpm-json/projects/project_id/lists/list_id/tasks
  *
  * @since 1.2
  * @return array $response
  */
 public function create_task($project_id, $list_id, $data)
 {
     $project_id = (int) $project_id;
     $list_id = (int) $list_id;
     if (!$project_id) {
         return new WP_Error('json_post_invalid_id', __('Invalid project ID.'), array('status' => 404));
     }
     if (!$list_id) {
         return new WP_Error('json_post_invalid_id', __('Invalid todo list ID.'), array('status' => 404));
     }
     if (empty($data['task_text'])) {
         return new WP_Error('task_text', __('Task Name Required', 'cpm'));
     }
     $posts_list = cpm()->task->get_task_list($list_id);
     $manage_capability = cpm_manage_capability();
     if (!$manage_capability && !cpm_is_single_project_manager($project_id)) {
         if (!cpm_project_user_role_pre_chache($project_id)) {
             return new WP_Error('permission', __('Sorry! you are not assigned in this project', 'cpm'), array('status' => 404));
         }
         if (!cpm_user_can_access($project_id, 'create_todolist')) {
             return new WP_Error('permission', __('Sorry! you do not have permission to create todo list', 'cpm'), array('status' => 404));
         }
         if (!cpm_user_can_access($project_id, 'create_todo')) {
             return new WP_Error('permission', __('Sorry! you do not have permission to create todo', 'cpm'), array('status' => 404));
         }
     }
     add_filter('cpm_new_task_notification', array($this, 'change_notification_status'));
     $task_id = cpm()->task->add_task($list_id, $data);
     $get_task = cpm()->task->get_task($task_id);
     $response = new WP_JSON_Response();
     $response->set_data($get_task);
     return $response;
 }