/**
  * Create a project select list that are grouped by client.
  * @param  integer $selected_id
  * @return string
  */
 function si_projects_select($selected_id = 0, $client_id = 0, $blank = true, $el_id = 'doc_project')
 {
     $selections = array();
     if ($client_id) {
         $client = SI_Client::get_instance($client_id);
         if (is_a($client, 'SI_Client')) {
             $projects = SI_Project::get_projects_by_client($client_id);
             $selections[$client->get_title()] = $projects;
         }
     }
     if (empty($selections)) {
         $args = array('post_type' => SI_Project::POST_TYPE, 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids');
         $projects = get_posts($args);
         foreach ($projects as $project_id) {
             $project = SI_Project::get_instance($project_id);
             $clients = $project->get_associated_clients();
             foreach ($clients as $client_id) {
                 $client = SI_Client::get_instance($client_id);
                 if (is_a($client, 'SI_Client')) {
                     if (!isset($selections[$client->get_title()])) {
                         $selections[$client->get_title()] = array();
                     }
                     $selections[$client->get_title()][] = $project_id;
                 } else {
                     // no client assigned
                     $selections[si__('Client N/A')][] = $project_id;
                 }
             }
         }
     }
     if (!empty($selections)) {
         $out = '<select name="' . $el_id . '" class="select2">';
         if ($blank) {
             $out .= sprintf('<option value="0">%s</option>', si__('Select Project'));
         }
         foreach ($selections as $client => $projects) {
             $out .= sprintf('<optgroup label="%s">', $client);
             foreach ($projects as $project_id) {
                 $out .= sprintf('<option value="%s" %s>%s</option>', $project_id, selected($project_id, $selected_id, false), get_the_title($project_id));
             }
             $out .= '</optgroup>';
         }
         $out .= '</select>';
     } else {
         $out = '<span>' . sprintf(si__('No <a href="%s" target="_blank">projects</a> found'), admin_url('post-new.php?post_type=' . SI_Project::POST_TYPE)) . '</span>';
     }
     echo $out;
 }
 /**
  * Query sa_client posts with assigned user ID
  *
  * @param 	(int) $user_id 	wp_user ID
  * @return 	(array) 		sa_client post ID's
  **/
 public static function get_client_si_projects($user_id = 0)
 {
     $project_ids = array();
     if (!$user_id) {
         $user_id = get_current_user_id();
     }
     if (!$user_id) {
         return $project_ids;
     }
     $client_ids = SI_Client::get_clients_by_user($user_id);
     if (empty($client_ids)) {
         return $project_ids;
     }
     foreach ($client_ids as $client_id) {
         $project_ids[] = SI_Project::get_projects_by_client($client_id);
     }
     return $project_ids;
 }
 public static function maybe_disassociate_records($post_id = 0)
 {
     if (SI_Client::POST_TYPE === get_post_type($post_id)) {
         $client = SI_Client::get_instance($post_id);
         $invoices = $client->get_invoices();
         foreach ($invoices as $invoice_id) {
             $invoice = SI_Invoice::get_instance($invoice_id);
             $invoice->set_client_id(0);
         }
         $estimates = $client->get_estimates();
         foreach ($estimates as $estimate_id) {
             $estimate = SI_Estimate::get_instance($estimate_id);
             $estimate->set_client_id(0);
         }
         $projects = SI_Project::get_projects_by_client($post_id);
         foreach ($projects as $project_id) {
             $project = SI_Project::get_instance($project_id);
             $project->set_associated_clients(array());
         }
     }
 }
 public static function add_projects_to_clients_admin()
 {
     echo self::load_view('admin/meta-boxes/projects/client-submit', array('projects' => SI_Project::get_projects_by_client(get_the_id())));
 }
 public static function time_entry_fields($context = 0, $hide_project_select = false)
 {
     $projects = array();
     $time_types = array();
     if (!$context) {
         $context = get_the_id();
     }
     // Get associated projects based on context
     switch (get_post_type($context)) {
         case SI_Project::POST_TYPE:
             $project_id = $context;
             // Projects
             $projects = array($project_id);
             break;
         case SI_Client::POST_TYPE:
             $client_id = $context;
             // Projects
             $projects = SI_Project::get_projects_by_client($client_id);
             break;
         case SI_Invoice::POST_TYPE:
             $invoice_id = $context;
             // Projects
             $invoice = SI_Invoice::get_instance($invoice_id);
             $project_id = $invoice->get_project();
             if ($project_id) {
                 $projects = array($project_id);
             }
             break;
         default:
             // Projects
             $args = array('post_type' => SI_Project::POST_TYPE, 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids');
             $projects = get_posts($args);
             break;
     }
     $fields = array();
     $project_options = array();
     foreach ($projects as $project_id) {
         $title = get_the_title($project_id);
         $title = $title == __('Auto Draft') ? __('Current Project', 'sprout-invoices') : $title;
         $project_options[$project_id] = $title;
     }
     $fields['project_id'] = array('weight' => 1, 'label' => __('Project', 'sprout-invoices'), 'type' => 'select', 'options' => $project_options);
     $description = sprintf(__('Select an activity, <a href="%s">create a new activity</a> or <a class="thickbox" href="%s" title="Edit Activities">manage existing activities</a>.', 'sprout-invoices'), 'javascript:void(0)" id="show_time_creation_modal"', admin_url('admin-ajax.php?action=sa_manage_time&width=750&height=450'));
     $time_types_options = SI_Time::get_activities();
     $fields['activity_id'] = array('weight' => 10, 'label' => __('Activity', 'sprout-invoices'), 'type' => 'select', 'description' => $description, 'options' => $time_types_options);
     $fields['time_inc'] = array('weight' => 20, 'label' => __('Time', 'sprout-invoices'), 'type' => 'text', 'description' => __('In hours, e.g. 1.25 for 75 minutes.', 'sprout-invoices'));
     $fields['note'] = array('weight' => 30, 'label' => __('Note', 'sprout-invoices'), 'type' => 'textarea', 'default' => '');
     $fields['date'] = array('weight' => 100, 'label' => __('Date', 'sprout-invoices'), 'type' => 'date', 'default' => date('Y-m-d', current_time('timestamp')), 'placeholder' => '');
     $fields['nonce'] = array('type' => 'hidden', 'value' => wp_create_nonce(self::SUBMISSION_NONCE), 'weight' => 10000);
     $fields = apply_filters('si_time_entry_form_fields', $fields);
     uasort($fields, array(__CLASS__, 'sort_by_weight'));
     return $fields;
 }