Tripal v1.0 (6.x-1.0)
|
00001 <?php 00002 00010 class tripal_views_handler_filter_file_upload extends views_handler_filter { 00011 00016 function value_form(&$form, &$form_state) { 00017 parent::value_form($form, $form_state); 00018 00019 $this->value_form = array( 00020 '#type' => 'file_upload_combo', 00021 '#title' => t('%label', array('%label' => $this->options['expose']['label'])), 00022 '#default_value' => $this->value, 00023 '#multiple' => FALSE, 00024 '#description' => t('Provide search values for %label. Please place each search 00025 item on a separate line or separated by commas.', array('%label' => $this->options['expose']['label'])), 00026 ); 00027 $form['value'] = &$this->value_form; 00028 } 00029 00034 function exposed_form(&$form, &$form_state) { 00035 00036 // don't do anything if the form isn't exposed. 00037 if (empty($this->options['exposed'])) { 00038 return; 00039 } 00040 // rebuild the form elements 00041 $value = $this->options['expose']['identifier']; 00042 $this->value_form($form, $form_state); 00043 00044 $form[$value] = $form['value']; 00045 unset($form[$value]['#title']); 00046 00047 // since this is an exposed form we want to enable file uploads by 00048 // setting the 'enctype' attribute and the method to POST 00049 $form['#attributes']['enctype'] = 'multipart/form-data'; 00050 $form['#method'] = 'POST'; 00051 $this->exposed_translate($form[$value], 'value'); 00052 00053 if ($value != 'value') { 00054 unset($form['value']); 00055 } 00056 00057 } 00058 00062 function exposed_validate(&$form, &$form_state) { 00063 //dpm($form_state); 00064 } 00065 00069 function query() { 00070 $this->ensure_my_table(); 00071 $field = "$this->table.$this->real_field"; 00072 00073 // get the form element value 00074 $value = $this->value[0]; 00075 00076 // the form element value has sub values, so get those 00077 $items = $value['items']; 00078 $file_path = $value['file_path']; 00079 00080 $holders = array(); 00081 $values = array(); 00082 00083 // get the file upload content if one has been provided 00084 if ($file_path) { 00085 $fh = fopen($file_path, 'r'); 00086 while ($line = fgets($fh)) { 00087 $items = trim($line); 00088 00089 // remove extra spaces and new lines 00090 $items = preg_replace("/\s+,/", ",", $items); 00091 $items = preg_replace("/\s+\n/", "\n", $items); 00092 $items = preg_replace("/,\n/", "\n", $items); 00093 00094 // add the values from this line to the values[] array 00095 $vals = preg_split("/[\n,]+/", $items); 00096 $values = array_merge($values, $vals); 00097 } 00098 } 00099 00100 // if a file upload has not been provided then use the value in the textarea 00101 if ($items) { 00102 00103 // remove extra spaces and new lines 00104 $items = preg_replace("/\s+,/", ",", $items); 00105 $items = preg_replace("/\s+\n/", "\n", $items); 00106 $items = preg_replace("/,\n/", "\n", $items); 00107 00108 // in the event the user uploaded a file and provided items in the 00109 // textbox then we need to merge these two lists 00110 $vals = preg_split("/[\n,]+/", $items); 00111 $values = array_merge($values, $vals); 00112 } 00113 00114 // iterate through all of the values and generate the corresponding 00115 // sprintf style holders 00116 for ($i = 0 ; $i < count($values); $i++) { 00117 $values[$i] = trim($values[$i]); 00118 $holders[] = "'%s'"; 00119 } 00120 00121 // if we have any values supplied then update the where clause for 00122 // the views query 00123 if (count($holders) > 0) { 00124 $where = "$field IN (" . implode(", ", $holders) . ")"; 00125 $this->query->add_where($this->options['group'], $where, $values); 00126 } 00127 } 00128 }