/**
  * 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;
     }
 }
Exemple #2
0
function cinda_header_hook($name)
{
    if ($name == 'cinda') {
        wp_enqueue_style('cinda-global', CINDA_URL . 'templates/css/global.css', false, "1.0", false);
        if (file_exists(get_template_directory_uri() . '/cinda/templates/css/global.css')) {
            wp_enqueue_style('cinda-global-custom-theme', get_template_directory_uri() . '/cinda/templates/css/global.css', false, "1.0", false);
        }
        if (is_single() && CINDA_PREFIX . 'campaign' == get_post_type()) {
            global $campaign;
            $campaign = new Campaign(get_the_ID());
            $campaign->set_contributions();
        }
    }
    return;
}
 /**
  * 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));
     }
 }