function edit()
 {
     global $wpdb;
     // trim
     array_map('trim', $_POST);
     // extract
     extract($_POST);
     // system
     $system_obj = mgm_get_class('system');
     // save
     if (isset($submit_download)) {
         // response
         $response = array('status' => 'error', 'message' => sprintf(__('Error while updating download <b>%s</b>!', 'mgm'), $title));
         // check duplicate
         if (mgm_is_duplicate(TBL_MGM_DOWNLOAD, array('title'), "id <> '{$id}'")) {
             $response['message'] = sprintf(__('Error while updating download <b>%s</b>, title exists!', 'mgm'), $title);
         } else {
             // set vars
             $members_only = isset($members_only) ? 'Y' : 'N';
             // set vars
             $restrict_acces_ip = isset($restrict_acces_ip) ? 'Y' : 'N';
             // is_s3_torrent
             $is_s3_torrent = isset($is_s3_torrent) ? 'Y' : 'N';
             // filename
             $filename = isset($download_file_new) ? $download_file_new : $direct_url;
             // real name
             $real_filename = isset($download_file_new_realname) ? $download_file_new_realname : basename($filename);
             // filesize
             $filesize = mgm_file_get_size($filename);
             // post vars
             $post_date = date('Y-m-d H:i:s');
             // user
             $current_user = wp_get_current_user();
             // data
             $data = array('title' => $title, 'filename' => $filename, 'real_filename' => $real_filename, 'filesize' => $filesize, 'post_date' => $post_date, 'restrict_acces_ip' => $restrict_acces_ip, 'user_id' => $current_user->ID, 'members_only' => $members_only, 'is_s3_torrent' => $is_s3_torrent);
             // null
             $null_columns = array();
             // download limit
             if (isset($download_limit) && (int) $download_limit > 0) {
                 $data['download_limit'] = (int) $download_limit;
             } else {
                 $null_columns[] = "`download_limit` = NULL ";
             }
             // expire date
             if (isset($expire_dt) && !empty($expire_dt)) {
                 $data['expire_dt'] = mgm_format_inputdate_to_mysql($expire_dt);
             } else {
                 $null_columns[] = "`expire_dt` = NULL ";
             }
             // code
             if (!isset($code) || isset($code) && empty($code)) {
                 $data['code'] = uniqid();
             }
             // update
             if ($wpdb->update(TBL_MGM_DOWNLOAD, $data, array('id' => $id))) {
                 // update null
                 if (count($null_columns) > 0) {
                     // join
                     $set_string = implode(',', $null_columns);
                     // clear old
                     $wpdb->query($wpdb->prepare('UPDATE `' . TBL_MGM_DOWNLOAD . '` SET ' . $set_string . ' WHERE `id` = %d', $id));
                 }
                 // clear old
                 $wpdb->query($wpdb->prepare('DELETE FROM `' . TBL_MGM_DOWNLOAD_POST_ASSOC . '` WHERE `download_id` = %d', $id));
                 // save
                 if (bool_from_yn($members_only)) {
                     if (isset($link_to_post_id)) {
                         // loop
                         foreach ($link_to_post_id as $post_id) {
                             // insert
                             $wpdb->insert(TBL_MGM_DOWNLOAD_POST_ASSOC, array('download_id' => $id, 'post_id' => $post_id));
                         }
                     }
                 }
                 // set message
                 $response['message'] = sprintf(__('Download updated successfully <b>%s</b>', 'mgm'), $title);
                 $response['status'] = 'success';
             } else {
                 $response['message'] = sprintf(__('Error while updating download <b>%s</b> Or nothing updated!', 'mgm'), $title);
             }
         }
         // return response
         echo json_encode($response);
         exit;
     }
     // data
     $data = array();
     // download
     $data['download'] = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . TBL_MGM_DOWNLOAD . "` WHERE id = %d", $id));
     // download_posts
     $data['download_posts'] = mgm_get_download_post_ids($id);
     // get all post types
     $post_types = mgm_get_post_types();
     //get all published posts - issue #1034
     $all_posts = mgm_field_values($wpdb->posts, 'ID', 'SUBSTR(post_title,1, 100) AS post_title', "AND `post_status` ='publish' AND `post_type` IN ({$post_types})", 'post_title');
     //get all scheduled posts
     $scheduled_posts = mgm_field_values($wpdb->posts, 'ID', 'SUBSTR(post_title,1, 100) AS post_title', "AND `post_status` ='future' AND `post_type` IN ({$post_types})", 'post_title');
     foreach ($scheduled_posts as $k => $scheduled_post) {
         $all_posts[$k] = $scheduled_post . '(S)';
     }
     //get all draft posts
     $draft_posts = mgm_field_values($wpdb->posts, 'ID', 'SUBSTR(post_title,1, 100) AS post_title', "AND `post_status` ='draft' AND `post_type` IN ({$post_types})", 'post_title');
     foreach ($draft_posts as $k => $draft_post) {
         $all_posts[$k] = $draft_post . '(D)';
     }
     //sort by post name
     asort($all_posts);
     //all posts
     $data['posts'] = $all_posts;
     // hook
     $data['download_hook'] = $system_obj->get_setting('download_hook', 'download');
     // slug
     $data['download_slug'] = $system_obj->get_setting('download_slug', 'download');
     // load template view
     $this->load->template('downloads/edit', array('data' => $data));
 }
 function _do_import($i_filepath = '')
 {
     global $wpdb;
     // init data
     $header = $coupons = array();
     $status = 'error';
     $message = __('Error while importing.', 'mgm');
     $extension = pathinfo($i_filepath, PATHINFO_EXTENSION);
     //mgm_log($extension);
     // uploaded ext
     switch (strtolower($extension)) {
         //CSV
         case 'csv':
             // read csv
             if ($handle = @fopen($i_filepath, 'r')) {
                 // loop
                 while ($data = fgetcsv($handle, null, ';')) {
                     //get headers:
                     if (empty($header)) {
                         $header = $data;
                     } else {
                         // $user_count++;
                         // update rowws for empty cells:
                         $row = array();
                         // loop
                         foreach ($header as $key => $val) {
                             // create an array with header value as index:
                             $row[$val] = !isset($data[$key]) ? '' : trim($data[$key]);
                         }
                         // set user
                         $coupons[] = $row;
                         // unset
                         unset($row);
                         // check limit reached:
                         // if(($user_count+1) >= $row_limit) break;
                     }
                     // unset
                     unset($data);
                 }
                 // close
                 @fclose($handle);
             }
             break;
     }
     // fields
     $fields = array('name', 'value', 'description', 'use_limit', 'used_count', 'product', 'expire_dt', 'create_dt');
     $sucess_coupons = $duplicate_coupons = 0;
     if (!empty($coupons)) {
         foreach ($coupons as $coupon) {
             if (!isset($coupon['create_dt']) || empty($coupon['create_dt'])) {
                 // create_dt
                 $coupon['create_dt'] = date('Y-m-d H:i:s');
             }
             $source = array('name' => $coupon['name']);
             if (mgm_is_duplicate(TBL_MGM_COUPON, array('name'), '', $source)) {
                 $duplicate_coupons++;
             } else {
                 // loop
                 foreach ($fields as $field) {
                     // check
                     if (isset($coupon[$field]) && !is_null($coupon[$field])) {
                         $column_data[$field] = trim($coupon[$field]);
                     }
                 }
                 // save
                 if ($wpdb->insert(TBL_MGM_COUPON, $column_data)) {
                     $sucess_coupons++;
                 }
             }
         }
     }
     if (isset($sucess_coupons) && $sucess_coupons > 0) {
         $status = 'success';
         $message = sprintf(__('Import completed successfully. %d coupons imported', 'mgm'), $sucess_coupons);
     }
     // delete uploaded file:
     if (file_exists($i_filepath)) {
         @unlink($i_filepath);
     }
     return array('status' => $status, 'message' => $message, 'action' => 'import');
 }
 function _validate_data($action, $data)
 {
     // errors
     $errors = array();
     // action
     switch ($action) {
         case 'create':
             // required fields
             $req_flds = array('title');
             // loop
             foreach ($req_flds as $req_fld) {
                 // check
                 if (!isset($data[$req_fld]) || isset($data[$req_fld]) && empty($data[$req_fld])) {
                     $errors[$req_fld . '_required'] = sprintf(__('%s is a required parameter', 'mgm'), $req_fld);
                 }
             }
             // check if title already validated
             if (!in_array('title_required', array_keys($errors))) {
                 // size overflow
                 if (strlen($data['title']) > 150) {
                     $errors['title_overflow'] = __('Download title should not exceed 150 characters', 'mgm');
                 }
                 // duplicate
                 if (mgm_is_duplicate(TBL_MGM_DOWNLOAD, array('title'), '', $data)) {
                     $errors['title_duplicate'] = __('Duplicate download title provided, please try with a different title', 'mgm');
                 }
             }
             break;
         case 'update':
             // required fields
             $req_flds = array('id');
             // loop
             foreach ($req_flds as $req_fld) {
                 // check
                 if (!isset($data[$req_fld]) || isset($data[$req_fld]) && empty($data[$req_fld])) {
                     $errors[$req_fld . '_required'] = sprintf(__('%s is a required parameter', 'mgm'), $req_fld);
                 }
             }
             // check if title already validated
             if (!in_array('title_required', array_keys($errors))) {
                 // size overflow
                 if (isset($data['title']) && strlen($data['title']) > 150) {
                     $errors['title_overflow'] = __('Download title should not exceed 150 characters', 'mgm');
                 }
                 // duplicate
                 if (mgm_is_duplicate(TBL_MGM_DOWNLOAD, array('title'), '`id`<>' . $data['id'], $data)) {
                     $errors['title_duplicate'] = __('Duplicate download title provided, please try with a different title', 'mgm');
                 }
             }
             break;
         case 'delete':
             // required fields
             $req_flds = array('id');
             // loop
             foreach ($req_flds as $req_fld) {
                 // check
                 if (!isset($data[$req_fld]) || isset($data[$req_fld]) && empty($data[$req_fld])) {
                     $errors[$req_fld . '_required'] = sprintf(__('%s is a required parameter', 'mgm'), $req_fld);
                 }
             }
             break;
     }
     // return
     return count($errors) > 0 ? $errors : false;
 }
 function edit()
 {
     global $wpdb;
     extract($_POST);
     // save
     if (isset($save_addon)) {
         // response
         $response = array('status' => 'error', 'message' => __('Addon update failed, Unknown error!', 'mgm'));
         // check duplicate
         if (mgm_is_duplicate(TBL_MGM_ADDON, array('name'), "id <> '{$id}'")) {
             $response['message'] = sprintf(__('Error while updating addon: %s, same code exists!', 'mgm'), $name);
         } else {
             // fields
             $fields = array('name', 'description', 'expire_dt');
             // colums
             $column_data = $column_null_data = array();
             // expire dt format
             if (isset($expire_dt) && !empty($expire_dt)) {
                 $expire_dt = date('Y-m-d H:i:s', strtotime(mgm_format_inputdate_to_mysql($expire_dt, mgm_get_date_format('date_format_short'))));
             } else {
                 $expire_dt = NULL;
             }
             // loop
             foreach ($fields as $field) {
                 // check
                 if (isset(${$field}) && !is_null(${$field})) {
                     $column_data[$field] = trim(${$field});
                 } else {
                     $column_null_data[$field] = 'NULL';
                     // need string to track
                 }
             }
             // affected
             $affected = 0;
             // update
             if ($wpdb->update(TBL_MGM_ADDON, $column_data, array('id' => $id))) {
                 $affected++;
             }
             // null
             if (!empty($column_null_data)) {
                 // column_data2
                 $column_data2_a = mgm_implode_a(',', array_keys($column_null_data), array_values($column_null_data));
                 // update
                 if ($wpdb->query("UPDATE `" . TBL_MGM_ADDON . "` SET {$column_data2_a} WHERE `id`='{$id}' ")) {
                     $affected++;
                 }
             }
             // check
             if (!isset($addon_options)) {
                 $addon_options = array();
             }
             // save options
             if ($this->_save_addon_options($id, $addon_options, 'update')) {
                 $affected++;
             }
             // save
             if ($affected) {
                 // response
                 $response = array('status' => 'success', 'message' => sprintf(__('Successfully updated addon: "%s"', 'mgm'), $name));
             } else {
                 $response = array('status' => 'error', 'message' => sprintf(__('Error while updating addon: "%s"', 'mgm'), $name));
             }
         }
         // return response
         echo json_encode($response);
         exit;
     }
     // data
     $data = array();
     // addon
     $data['addon'] = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . TBL_MGM_ADDON . "` WHERE `id`=%d", $id));
     $data['addon_options'] = $wpdb->get_results($wpdb->prepare("SELECT `option`,`price` FROM `" . TBL_MGM_ADDON_OPTION . "` WHERE `addon_id`=%d", $id), ARRAY_A);
     // empty
     if (empty($data['addon_options'])) {
         $data['addon_options'] = array(array('option' => '', 'price' => ''));
     }
     // load template view
     $this->load->template('addons/edit', array('data' => $data));
 }
 function restapi_key_edit()
 {
     global $wpdb;
     // id
     $id = (int) $_POST['id'];
     extract($_POST);
     // save
     if (isset($save_key)) {
         // check duplicate
         if (mgm_is_duplicate(TBL_MGM_REST_API_KEY, array('api_key'), "id <> '{$id}'")) {
             $message = sprintf(__('Error while updating api key: %s, same api key exists!', 'mgm'), $api_key);
             $status = 'error';
         } else {
             // init
             $sql_fields = array();
             // fields
             $sql_fields[] = "api_key='{$api_key}'";
             $sql_fields[] = "level='{$level}'";
             // sql
             $sql = "UPDATE `" . TBL_MGM_REST_API_KEY . "` SET " . implode(',', $sql_fields) . " WHERE id='{$id}' ";
             // saved
             if ($wpdb->query($sql)) {
                 $message = sprintf(__('Successfully updated api key: %s', 'mgm'), $api_key);
                 $status = 'success';
             } else {
                 $message = sprintf(__('Error while updating api key: %s', 'mgm'), $api_key);
                 $status = 'error';
             }
         }
         // return response
         echo json_encode(array('status' => $status, 'message' => $message));
         exit;
     }
     // data
     $data = array();
     // get list of levels
     $data['key'] = $wpdb->get_row("SELECT * FROM `" . TBL_MGM_REST_API_KEY . "` WHERE `id` = '{$id}'");
     // get levels
     $data['levels'] = $this->_get_levels_list();
     // load template view
     $this->load->template('settings/restapi/keys/edit', array('data' => $data));
 }