<?php
function trade_history_menu()
{
$items = array();
$items['stock/trade_history/balance/%'] = array(
'title' => 'Balance',
'page callback' => 'shopping_car',
'page arguments' => array(3),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK
);
$items['admin/example/import'] = array(
'title' => 'Import',
'page callback' => 'drupal_get_form',
'page arguments' => array('trade_history_import_form'),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
$items['admin/example/balance'] = array(
'title' => 'Balance_test',
'page callback' => 'drupal_get_form',
'page arguments' => array('trade_history_balance_form'),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function trade_history_balance_form($node, &$form_state, $params)
{
foreach ($params['buy_list'] as $buy_detail) {
$forms[$buy_detail->id]['num'] = array(
'#type' => 'textfield',
'#default_value' => $buy_detail->num,
'#size' => 10,
);
$forms[$buy_detail->id]['sale_num[]'] = array(
//'#title' => t('num'),
'#type' => 'textfield',
'#size' => 10,
);
}
return $forms;
}
function formexample_nameform() {
$form['name'] = array(
'#title' => t('Your Name'),
'#type' => 'fieldset',
'#description' => t('What people call you.')
);
$form['name']['user_name'] = array(
'#title' => t('Your Name'),
'#type' => 'textfield',
'#description' => t('Please enter your name.')
);
$form['color'] = array(
'#title' => t('Color'),
'#type' => 'fieldset',
'#description' => t('This fieldset contains the Color field.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE
);
$form['color_options'] = array(
'#type' => 'value',
'#value' => array(t('red'), t('green'), t('blue'))
);
$form['color']['favorite_color'] = array(
'#title' => t('Favorite Color'),
'#type' => 'select',
'#description' => t('Please select your favorite color.'),
'#options' => $form['color_options']['#value']
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
return $form;
}
function theme_trade_history_balance_form(&$variables)
{
$variables['formexample_nameform'] = array();
$hidden = array();
$output = "
<table>
<tr>
<th></th>
<th>ID</th>
<th>trade_day</th>
<th>own num</th>
<th>sale num</th>
</tr>";
foreach (element_children($variables['form']) as $key) {
if (isset($variables['form'][$key]['#type'])) {
$hidden[] = drupal_render($variables['form'][$key]);
} else {
//$variables['formexample_nameform'][$key] = drupal_render($variables['form'][$key]);
$output .= '
<tr>
<td><input type="checkbox" /></td>
<td>1</td>
<td>2012-04-16</td>
<td>' . drupal_render($variables['form'][$key]['num']) . '</td>
<td>{$a}</td>
</tr>';
}
}
$variables['formexample_nameform']['hidden'] = implode($hidden);
$variables['formexample_nameform_form'] = implode($variables['formexample_nameform']);
$output .= '</table>';
return $output;
}
function shopping_car($stock_trade_history_id)
{
drupal_add_js('jquery-ui-1.8.18.custom/js/jquery-1.7.1.min.js');
drupal_add_js('jquery-ui-1.8.18.custom/js/jquery-ui-1.8.18.custom.min.js');
$result = trade_history_load($stock_trade_history_id);
$params['sale_detail'] = $result->fetchObject();
$result = db_query("
SELECT *
FROM {stock_trade_history}
WHERE trade_type = :trade_type AND trade_day <= :trade_day",
array(
':trade_type' => 'B',
':trade_day' => $params['sale_detail']->trade_day
)
);
foreach ($result as $object) {
$params['buy_list'][] = $object;
}
$balance = drupal_get_form('trade_history_balance_form', $params);
$balance = drupal_render($balance);
$html = <<<HTML
<style type="text/css">
</style>
<script type="text/javascript">
</script>
HTML;
return $balance . $html;
}
function trade_history_import_form()
{
$form = array();
$form['browser'] = array(
'#type' => 'fieldset',
'#title' => t('History Import'),
'#collapsible' => TRUE,
'#description' => t("Import trade history from a CSV file."),
);
$file_size = t('Maximum file size: !size MB.', array('!size' => file_upload_max_size()));
$form['browser']['file_upload'] = array(
'#type' => 'file',
'#title' => t('CSV File'),
'#size' => 40,
'#description' => t('Select the CSV file to be imported. ') . $file_size,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Import'),
);
// set the form encoding type
$form['#attributes']['enctype'] = "multipart/form-data";
return $form;
}
function trade_history_theme()
{
return array(
'trade_history_balance_form' => array(
'render element' => 'form',
),
'formexample_nameform' => array(
'render element' => 'form',
'template' => 'formexample-nameform',
),
);
}
function template_preprocess_formexample_nameform(&$variables) {
$variables['formexample_nameform'] = array();
$hidden = array();
// Provide variables named after form keys so themers can print each element independently.
foreach (element_children($variables['form']) as $key) {
$type = $variables['form'][$key]['#type'];
if ($type == 'hidden' || $type == 'token') {
$hidden[] = drupal_render($variables['form'][$key]);
}
else {
$variables['formexample_nameform'][$key] = drupal_render($variables['form'][$key]);
}
}
// Hidden form elements have no value to themers. No need for separation.
$variables['formexample_nameform']['hidden'] = implode($hidden);
// Collect all form elements to make it easier to print the whole form.
$variables['formexample_nameform_form'] = implode($variables['formexample_nameform']);
}
function trade_history_import_form_validate($form, &$form_state)
{
// attempt to save the uploaded file
$file = file_save_upload('file_upload',
array(
'file_validate_extensions' => array('csv')
)
);
// check file uploaded OK
if (!$file) {
form_set_error('file_upload', t('A file must be uploaded or selected from FTP updates.'));
} else if($file->filemime != 'text/csv') {
form_set_error('file_upload', t('The file must be of CSV type only.'));
} else {
// set files to form_state, to process when form is submitted
$form_state['values']['file_upload'] = $file;
}
}
function trade_history_import_form_submit($form, &$form_state)
{
$line_max = variable_get('user_import_line_max', 1000);
ini_set('auto_detect_line_endings', true);
//$filepath = $form_state['values']['file_upload']->filepath;
$filepath = $form_state['values']['file_upload']->uri;
$handle = @fopen($filepath, "r");
// start count of imports for this upload
$send_counter = 0;
while ($row = fgetcsv($handle, $line_max, ',')) {
$row[1] = time();
$row[3] = (int) $row[3];
$row[1] = $row[10] = $row[11] = date('Y-m-d H:i:s');
var_dump($row);
db_query("INSERT INTO {stock_trade_history} VALUES(NULL,
:stock_id,
:trade_day,
:trade_type,
:num,
:price,
:total,
:stamp_tax,
:poundage,
:transfer_fee,
:status,
:created_at,
:modified_at)",
array(
':stock_id' => $row[0],
':trade_day' => $row[1],
':trade_type' => $row[2],
':num' => $row[3],
':price' => $row[4],
':total' => $row[5],
':stamp_tax' => $row[6],
':poundage' => $row[7],
':transfer_fee' => $row[8],
':status' => $row[9],
':created_at' => $row[10],
':modified_at' => $row[11]
)
);
}
drupal_set_message('Import sucess.');
}
function trade_history_load($id)
{
return db_query('SELECT * FROM {stock_trade_history} WHERE id=:id', array(':id' => $id));
}
trade_history
最新推荐文章于 2022-02-21 17:25:12 发布