/** * Clear class cache * * @access public * @param string $func function to clear on, else clears entire class * @return object returns $this for chaining */ public function clear_cache($func = '') { if (trim($func) !== '') { Freeform_cacher::clear($this->class, $func); } else { Freeform_cacher::clear($this->class); } return $this; }
/** * Delete Field * * checks field to see if its in any forms, then removes the field * * @access public * @param mixed int/array of ints $field_id field id to delete * @return boolean success */ public function delete_field($field_ids) { if (!is_array($field_ids) and !$this->is_positive_intlike($field_ids)) { return FALSE; } if (!is_array($field_ids)) { $field_ids = array($field_ids); } $field_ids = array_filter($field_ids, array($this, 'is_positive_intlike')); ee()->load->library('freeform_forms'); //go through each field and remove it from every form its in $remove_fields = array(); foreach ($field_ids as $field_id) { $field_form_data = $this->data->get_form_info_by_field_id($field_id); if ($field_form_data !== FALSE) { foreach ($field_form_data as $form_id => $form_data) { ee()->freeform_forms->remove_field_from_form($form_id, $field_id); } } $instance =& $this->get_field_instance($field_id); if (is_callable(array($instance, 'delete_field'))) { $instance->delete_field(); } $remove_fields[] = $field_id; } if (empty($remove_fields)) { return FALSE; } //delete all fields ee()->freeform_field_model->where_in('field_id', $remove_fields)->delete(); Freeform_cacher::clear(__CLASS__, 'get_field_instance'); return TRUE; }
/** * Main Export function for returning or forcing a download of file * * @access public * @param array $options various download options. Form_id and query required * @return string string of file contents or exit on array */ public function export($options = array()) { // ------------------------------------- // run defaults // ------------------------------------- $defaults = array('method' => 'csv', 'form_id' => 0, 'form_name' => '', 'output' => 'string', 'rows' => array(), 'model' => NULL, 'fields' => '*', 'remove_entry_id' => FALSE, 'header_labels' => array(), 'total_entries' => 0); foreach ($defaults as $key => $value) { if (isset($options[$key])) { $defaults[$key] = $options[$key]; } } extract($defaults); unset($defaults, $options); $chunk = FALSE; $chunk_start = FALSE; $chunk_end = FALSE; // ------------------------------------- // check prelim data // ------------------------------------- if ((!is_array($rows) or empty($rows)) and !$model or !$this->is_positive_intlike($form_id)) { return FALSE; } // ------------------------------------- // cache clean // ------------------------------------- $this->clean_file_cache(); // ------------------------------------- // export // ------------------------------------- $method = is_callable(array($this, $method)) ? $method : 'csv'; //return a string to write somewhere? if ($output == 'string') { if (empty($rows) and $model) { $rows = $model->get(); } return $this->{$method}(array('form_id' => $form_id, 'rows' => $rows, 'remove_entry_id' => $remove_entry_id, 'header_labels' => $header_labels, 'chunk' => $chunk, 'chunk_start' => $chunk_start, 'chunk_end' => $chunk_end)); } else { $form_name = rtrim(trim($form_name), '_') . '_'; $filename = ee()->security->sanitize_filename('freeform_' . $form_name . 'export_' . date('Ymd_Hi_s', ee()->localize->now) . '.' . $method); //do we need to chunk this? if (empty($rows) and $model and $total_entries > $this->export_chunk_size) { //Set the execution time to infinite. set_time_limit(0); //detect chunk size $chunk_size = ceil($total_entries / $this->export_chunk_size); // ------------------------------------- // get file path and check writability // ------------------------------------- ee()->load->helper('file'); $filepath = $this->cache_file_path($filename); //blank write_file($filepath, ''); //chunk and write for ($i = 0; $i < $chunk_size; $i++) { $model->limit($this->export_chunk_size, $i * $this->export_chunk_size); $data = $this->{$method}(array('form_id' => $form_id, 'remove_entry_id' => $remove_entry_id, 'header_labels' => $header_labels, 'rows' => $model->get(array(), FALSE), 'chunk' => TRUE, 'chunk_start' => $i == 0, 'chunk_end' => $i == $chunk_size - 1)); write_file($filepath, $data, 'a+'); unset($data); Freeform_cacher::clear(); } // ------------------------------------- // send to user // ------------------------------------- header('Content-disposition: attachment; filename=' . $filename); header('Content-type: ' . get_mime_by_extension($filename)); readfile($filepath); exit; } else { if (empty($rows) and $model) { $rows = $model->get(); } $data = $this->{$method}(array('form_id' => $form_id, 'rows' => $rows, 'remove_entry_id' => $remove_entry_id, 'header_labels' => $header_labels, 'chunk' => $chunk, 'chunk_start' => $chunk_start, 'chunk_end' => $chunk_end)); if ($data) { ee()->load->helper('download'); force_download($filename, $data); exit; } } //fail safe $this->actions()->full_stop(lang('error_creating_export')); } }