Example #1
0
 /**
  * Create form
  */
 public function create_form()
 {
     $fields = get_post_meta(get_the_ID());
     $volunteer = new API_Volunteer(get_the_ID());
     $volunteer->save();
     include CINDA_DIR . 'assets/views/volunteer/profile_data_table.php';
 }
Example #2
0
 /**
  * Set the volunteers
  */
 private function set_volunteers()
 {
     global $wpdb;
     $volunteers = $wpdb->get_results($this->sql, ARRAY_A);
     foreach ($volunteers as $volunteer) {
         $tmp_volunteer = new API_Volunteer($volunteer['id']);
         $tmp_volunteer->set_contributions($volunteer['n']);
         $this->volunteers[] = $tmp_volunteer;
     }
 }
 /**
  * List contributions based on Campaign ID and/or Volunteer ID
  */
 static function listData()
 {
     global $wp;
     $args = array();
     if (isset($wp->query_vars['cid']) && !empty($wp->query_vars['cid'])) {
         $args['campaign'] = $wp->query_vars['cid'];
     }
     // Volunteer by ID
     if (isset($wp->query_vars['vid']) && !empty($wp->query_vars['vid'])) {
         $args['volunteer'] = $wp->query_vars['vid'];
     } else {
         if (isset($_POST['token']) && !empty($_POST['token'])) {
             if ($volunteer = API_Volunteer::get_volunter_by_token($_POST['token'])) {
                 $args['volunteer'] = $volunteer->get_id();
             }
         }
     }
     $contributionList = new self($args);
     $contributions = array();
     if (0 < count($contributionList->get_contributions())) {
         foreach ($contributionList->get_contributions() as $contribution) {
             $contributions[] = $contribution->serialize(false);
         }
     }
     echo json_encode($contributions);
     die;
 }
Example #4
0
 /**
  * Constructor
  * @param unknown $args
  */
 function __construct($args = array())
 {
     global $wpdb;
     $args_defaults = array('token' => null, 'extended' => false, 'date_start' => array("CURDATE()", "<="), 'date_end' => array("CURDATE()", ">="), 'all' => false);
     if (!empty($args)) {
         if (!isset($args['all']) || $args['all'] != true) {
             if (isset($args['date_start']) && !empty($args['date_start'])) {
                 $args['date_start'] = array("STR_TO_DATE('" . $args['date_start'] . "','%Y-%m-%d')", ">=");
             }
             if (isset($args['date_end']) && !empty($args['date_end'])) {
                 $args['date_end'] = array("STR_TO_DATE('" . $args['date_end'] . "','%Y-%m-%d')", "<=");
             }
         }
     }
     $args = array_merge($args_defaults, $args);
     // Check if Volunteer is sucribed
     if (isset($args['token']) && $args['token']) {
         if ($volunteer = API_Volunteer::get_volunter_by_token($args['token'])) {
             $suscriptions = $volunteer->get_suscriptions();
         }
     }
     $select_sql = "";
     $inner_sql = "";
     $where_sql = "";
     $order_sql = "";
     $group_sql = "";
     $limit_sql = "";
     if (!isset($args['all']) || $args['all'] != true) {
         if (isset($args['date_start']) && is_array($args['date_start']) && !empty($args['date_start']) || isset($args['date_end']) && is_array($args['date_end']) && !empty($args['date_end'])) {
             if (isset($args['date_start']) && !empty($args['date_start'])) {
                 $inner_sql .= " INNER JOIN " . $wpdb->prefix . "postmeta AS m1 ON ( p.ID = m1.post_id ) ";
                 $where_sql .= " AND (m1.meta_key LIKE '" . CINDA_PREFIX . "start_date' AND STR_TO_DATE(m1.meta_value,'%Y-%m-%d') " . $args['date_start'][1] . " " . $args['date_start'][0] . " ) ";
             }
             if (isset($args['date_end']) && !empty($args['date_end'])) {
                 $inner_sql .= " INNER JOIN " . $wpdb->prefix . "postmeta AS m2 ON ( p.ID = m2.post_id ) ";
                 $where_sql .= " AND (m2.meta_key LIKE '" . CINDA_PREFIX . "end_date' AND STR_TO_DATE(m2.meta_value,'%Y-%m-%d') " . $args['date_end'][1] . " " . $args['date_end'][0] . " ) ";
             }
         }
     }
     if (isset($args['orderby'])) {
         if ($args['orderby'] == "suscriptions") {
             $inner_sql .= " INNER JOIN " . CINDA_TABLE_SUSCRIPTIONS_NAME . " AS s ON ( p.ID = s.id_campaign ) ";
             $order_sql .= " ORDER BY COUNT( DISTINCT s.id_volunteer )";
             $group_sql .= " GROUP BY s.id_campaign";
         }
     }
     if (isset($args['number']) && is_numeric($args['number'])) {
         $limit_sql .= " LIMIT " . $args['number'];
     }
     $this->sql = "\n\t\t\t\tSELECT \n\t\t\t\t\tp.ID \n\t\t\t\tFROM \n\t\t\t\t\t" . $wpdb->prefix . "posts AS p\n\t\t\t\t" . $inner_sql . "\n\t\t\t\tWHERE\n\t\t\t\t\tp.post_type LIKE '" . CINDA_PREFIX . "campaign' \n\t\t\t\tAND \n\t\t\t\t\tp.post_status LIKE 'publish' \n\t\t\t\t" . $where_sql . "\n\t\t\t\t" . $group_sql . " \n\t\t\t\t" . $order_sql . " \n\t\t\t\t" . $limit_sql . "\n\t\t\t\t;";
     $campaigns = $wpdb->get_results($this->sql);
     foreach ($campaigns as $campaign) {
         $obj = new API_Campaign($campaign->ID);
         if (isset($suscriptions) && in_array($campaign->ID, $suscriptions)) {
             $obj->set_subscribed('true');
         }
         $this->campaigns[] = $obj;
     }
 }
Example #5
0
 /**
  * Function for API request
  * return JSON encode of the Campaign Object
  */
 public static function campaign_info()
 {
     global $wp;
     $campaign = new self($wp->query_vars['cid']);
     // Check if Volunteer is sucribed
     if (isset($_POST['token'])) {
         if ($volunteer = API_Volunteer::get_volunter_by_token($_POST['token'])) {
             $suscriptions = $volunteer->get_suscriptions();
             if (in_array($campaign->ID, $suscriptions)) {
                 $campaign->set_subscribed();
             }
         }
     }
     echo json_encode($campaign);
 }
Example #6
0
 /**
  * Print json array with all information for the watchface application
  */
 public static function get_watchfaceData()
 {
     global $wp;
     global $wpdb;
     $token = isset($_POST['token']) ? $_POST['token'] : "";
     // TMP
     if (isset($_GET['dev']) && $_GET['dev'] == true) {
         $token = "04685cfa05ff2a0fa97a36be3997819a81c62522";
     }
     if ($id_volunteer = API_Volunteer::get_volunter_id($token)) {
         $campaigns = new CampaignsList(array('orderby' => 'suscriptions', 'number' => 10, 'token' => $token));
         $volunteers = new VolunteerList(array('number' => 10));
         $contributions = new ContributionList(array('volunteer' => $id_volunteer));
         $response = array();
         // CAMPAIGNS
         if (0 < count($campaigns = $campaigns->get_campaigns())) {
             // Calc Angle
             $total_suscriptors = 0;
             foreach ($campaigns as $campaign) {
                 $total_suscriptors += $campaign->suscriptions;
             }
             $response['campaigns'] = array();
             $total_angle = 0;
             foreach ($campaigns as $campaign) {
                 $angle = round($campaign->suscriptions * 100 / $total_suscriptors * 360 / 100);
                 $total_angle += $angle;
                 $response['campaigns'][] = array('id' => $campaign->ID, 'color' => $campaign->color, 'name' => $campaign->title, 'suscriptions' => $campaign->suscriptions, 'angle' => $angle);
             }
             if ($total_angle < 360) {
                 $dif = 360 - $total_angle;
                 $response['campaigns'][count($response['campaigns']) - 1]['angle'] += $dif;
             }
         }
         // VOLUNTEERS
         if (0 < count($volunteers = $volunteers->get_volunteers())) {
             $total_contributions = 0;
             foreach ($volunteers as $volunteer) {
                 $total_contributions += $volunteer->contributions;
             }
             $response['volunteers'] = array();
             foreach ($volunteers as $volunteer) {
                 $response['volunteers'][] = array('id' => $volunteer->get_ID(), 'name' => $volunteer->nickname, 'avatar' => $volunteer->avatar, 'contributions' => $volunteer->contributions, 'angle' => round($volunteer->contributions * 100 / $total_contributions * 360 / 100));
             }
         }
         // VOLUNTEERS
         $total_contributions = count($contributions = $contributions->get_contributions());
         if (0 < $total_contributions) {
             $response['contributions'] = array();
             foreach ($contributions as $contribution) {
                 $data = $contribution->serialize(false);
                 unset($data['author_id']);
                 unset($data['author_name']);
                 $data['angle'] = round(1 * 100 / $total_contributions * 360 / 100);
                 $response['contributions'][] = $data;
             }
         }
         // TIMELINE
         $response['timeline'] = array();
         $from = date('Y-m-1 00:00:00', strtotime("-5 month", strtotime('m')));
         $to = date('Y-m-d 00:00:00', strtotime("+1 day"));
         $sql = "SELECT CONCAT( MONTH( p.post_date ), '-', YEAR( p.post_date ) ) AS month, COUNT( p.ID ) AS contributions\n\t\t\t\t\tFROM " . $wpdb->prefix . "posts AS p \n\t\t\t\t\tINNER JOIN " . $wpdb->prefix . "postmeta AS m ON p.ID = m.post_id \n\t\t\t\t\tWHERE m.meta_key = '" . CINDA_PREFIX . "author_id' \n\t\t\t\t\tAND m.meta_value = " . $id_volunteer . " \n\t\t\t\t\tAND STR_TO_DATE( p.post_date, '%Y-%m-%d %H:%i:%s') >= STR_TO_DATE( '" . $from . "', '%Y-%m-%d %H:%i:%s') \n\t\t\t\t\tAND STR_TO_DATE( p.post_date, '%Y-%m-%d %H:%i:%s') <= STR_TO_DATE( '" . $to . "', '%Y-%m-%d %H:%i:%s')\n\t\t\t\t\tGROUP BY MONTH( p.post_date );";
         $months = $wpdb->get_results($sql, ARRAY_A);
         for ($i = 5, $e = 0; $i >= 0; $i--, $e++) {
             $month_tmp = date('n-Y', strtotime("-{$i} month", strtotime($to)));
             foreach ($months as $month) {
                 if ($month['month'] == $month_tmp) {
                     $response['timeline'][$e] = array('month' => $month['month'], 'contributions' => intval($month['contributions']));
                     break;
                 }
             }
             if (!isset($response['timeline'][$e])) {
                 $response['timeline'][$e] = array('month' => $month_tmp, 'contributions' => 0);
             }
         }
         die(json_encode($response));
     } else {
         die(json_encode(0));
     }
 }
Example #7
0
 public static function get_all_trackings()
 {
     $trackings = array();
     $id_volunteer = 0;
     global $wpdb;
     if (isset($_POST['token'])) {
         $id_volunteer = Volunteer::get_volunter_id($_POST['token']);
     }
     if ($id_volunteer) {
         $id_trackings = $wpdb->get_col('SELECT id FROM ' . CINDA_TABLE_TRACKINGS_NAME . ' where id_volunteer = ' . $id_volunteer . ' ORDER BY create_date, id ASC');
         if (0 < count($id_trackings)) {
             foreach ($id_trackings as $id) {
                 $tracking = new self($id);
                 $trackings[] = $tracking->serialize();
             }
         }
     }
     die(json_encode($trackings));
 }
Example #8
0
 /**
  * Save contribution in DataBase
  */
 public static function save()
 {
     global $wp;
     global $wpdb;
     $errors = array();
     // Check if exists data in $_POST and $_POST['token']
     if (!empty($_POST) && isset($_POST['token'])) {
         // Check if volunteer exists
         if ($volunteer = API_Volunteer::get_volunter_by_token($_POST['token'])) {
             $id_campaign = $wp->query_vars['cid'];
             // Check if campaign exists
             if (API_Campaign::campaign_exists($id_campaign)) {
                 $campaign = new API_Campaign($id_campaign);
                 $model = $campaign->get_model();
                 $post = array('post_title' => $volunteer->nickname . " " . __('for', 'Cinda') . " " . $campaign->title . " " . __('on', 'Cinda') . " " . date('Y-m-d H:i:s'), 'post_type' => CINDA_PREFIX . 'contribution', 'post_parent' => $campaign->get_ID(), 'post_status' => 'publish');
                 // Create POST
                 $id_contribution = wp_insert_post($post);
                 // Add metakey author ID
                 update_post_meta($id_contribution, CINDA_PREFIX . 'author_id', $volunteer->get_ID());
                 if (isset($_POST['tracking'])) {
                     update_post_meta($id_contribution, CINDA_PREFIX . 'tracking', $_POST['tracking']);
                 }
                 // Add all metakeys
                 foreach ($model as $field) {
                     // Upload image
                     if ($field->field_type == 'image' || $field->field_type == 'file') {
                         // Load functions if not exists
                         if (!function_exists('wp_handle_upload')) {
                             require_once ABSPATH . 'wp-admin/includes/file.php';
                         }
                         if (!function_exists('wp_generate_attachment_metadata')) {
                             require_once ABSPATH . 'wp-admin/includes/image.php';
                         }
                         // Check if file exists
                         if (isset($_FILES[$field->field_name]) && !empty($_FILES[$field->field_name])) {
                             $file = $_FILES[$field->field_name];
                             // Move file to Uploads folder
                             $movefile = wp_handle_upload($file, array('test_form' => false));
                             // Success
                             if ($movefile && !isset($movefile['error'])) {
                                 $filetype = $movefile['type'];
                                 $filename = $movefile['file'];
                                 $upload_dir = wp_upload_dir();
                                 $attachment = array('guid' => $upload_dir['url'] . '/' . basename($filename), 'post_mime_type' => $filetype, 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit');
                                 $attachment_id = wp_insert_attachment($attachment, $filename);
                                 $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
                                 wp_update_attachment_metadata($attachment_id, $attachment_data);
                                 update_post_meta($id_contribution, CINDA_PREFIX . $field->field_name, $attachment_id);
                             } else {
                                 $errors[] = __($movefile['error'], 'Cinda');
                             }
                             // ERROR empty image
                         } else {
                             if ($field->field_required) {
                                 $errors[] = __('Empty image or image not sent', 'Cinda');
                             }
                         }
                     } else {
                         if (isset($_POST[$field->field_name])) {
                             update_post_meta($id_contribution, CINDA_PREFIX . $field->field_name, $_POST[$field->field_name]);
                         }
                     }
                 }
                 // END Foreach
                 // 404 Campaign
             } else {
                 $errors[] = "Campaign not found";
             }
             // 404 Volunteer
         } else {
             $errors[] = "Volunteer not found";
         }
     } else {
         if (empty($_POST)) {
             $errors[] = "No data send";
         }
         if (!isset($_POST['token'])) {
             $errors[] = "No token send";
         }
     }
     // Returns values
     if (empty($errors)) {
         die(json_encode(1));
     } else {
         die(json_encode(0));
     }
 }