/system/expressionengine/third_party/freeform/mcp.freeform.php
PHP | 6572 lines | 3700 code | 1362 blank | 1510 comment | 411 complexity | 51f7cd09070ec34e760f2c338908caf7 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * Solspace - Freeform
- *
- * @package Solspace:Freeform
- * @author Solspace DevTeam
- * @copyright Copyright (c) 2008-2012, Solspace, Inc.
- * @link http://solspace.com/docs/addon/c/Freeform/
- * @version 4.0.8
- * @filesource ./system/expressionengine/third_party/freeform/
- */
- /**
- * Freeform - Control Panel
- *
- * The Control Panel master class that handles all of the CP Requests and Displaying
- *
- * @package Solspace:Freeform
- * @author Solspace DevTeam
- * @filesource ./system/expressionengine/third_party/freeform/mcp.freeform.php
- */
- if ( ! class_exists('Module_builder_freeform'))
- {
- require_once 'addon_builder/module_builder.php';
- }
- class Freeform_mcp extends Module_builder_freeform
- {
- private $migration_batch_limit = 100;
- private $pro_update = FALSE;
- // --------------------------------------------------------------------
- /**
- * Constructor
- *
- * @access public
- * @param bool Enable calling of methods based on URI string
- * @return string
- */
- public function __construct( $switch = TRUE )
- {
- parent::__construct('freeform');
- // Install or Uninstall Request
- if ((bool) $switch === FALSE) return;
- if ( ! function_exists('lang'))
- {
- ee()->load->helper('language');
- }
- // --------------------------------------------
- // Module Menu Items
- // --------------------------------------------
- $menu = array(
- 'module_forms' => array(
- 'link' => $this->base,
- 'title' => lang('forms')
- ),
- 'module_fields' => array(
- 'link' => $this->base . AMP . 'method=fields',
- 'title' => lang('fields')
- ),
- 'module_fieldtypes' => array(
- 'link' => $this->base . AMP . 'method=fieldtypes',
- 'title' => lang('fieldtypes')
- ),
- 'module_notifications' => array(
- 'link' => $this->base . AMP . 'method=notifications',
- 'title' => lang('notifications')
- ),
-
- /*'module_export' => array(
- 'link' => $this->base . AMP . 'method=export',
- 'title' => lang('export')
- ),*/
- 'module_utilities' => array(
- 'link' => $this->base . AMP . 'method=utilities',
- 'title' => lang('utilities')
- ),
-
- 'module_preferences' => array(
- 'link' => $this->base . AMP . 'method=preferences',
- 'title' => lang('preferences')
- ),
- 'module_documentation' => array(
- 'link' => FREEFORM_DOCS_URL,
- 'title' => lang('help'),
- 'new_window' => TRUE
- ),
- );
- $this->cached_vars['lang_module_version'] = lang('freeform_module_version');
- $this->cached_vars['module_version'] = FREEFORM_VERSION;
- $this->cached_vars['module_menu_highlight'] = 'module_forms';
- $this->cached_vars['inner_nav_links'] = array();
- // -------------------------------------
- // css includes. WOOT!
- // -------------------------------------
- $this->cached_vars['cp_stylesheet'] = array(
- 'chosen',
- 'standard_cp'
- );
- $this->cached_vars['cp_javascript'] = array(
- 'standard_cp.min',
- 'chosen.jquery.min'
- );
- // -------------------------------------
- // custom CP?
- // -------------------------------------
- $debug_normal = (ee()->input->get_post('debug_normal') !== FALSE);
- $is_crappy_ie_version = FALSE;
- $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
- if (stristr($ua, 'msie 6') OR
- stristr($ua, 'msie 7') OR
- stristr($ua, 'msie 8'))
- {
- $is_crappy_ie_version = TRUE;
- }
- if ( ! $debug_normal AND
- ! $is_crappy_ie_version AND
- ! $this->check_no($this->preference('use_solspace_mcp_style')))
- {
- $this->cached_vars['cp_stylesheet'][] = 'custom_cp';
- }
- // -------------------------------------
- // Module Installed and What Version?
- // -------------------------------------
- if ($this->database_version() == FALSE)
- {
- return;
- }
- else if ($this->version_compare($this->database_version(), '<', FREEFORM_VERSION)
- OR ! $this->extensions_enabled())
- {
- // For EE 2.x, we need to redirect the request to
- // Update Routine
- $_GET['method'] = 'freeform_module_update';
- }
- //avoids AR collisions
- $this->data->get_module_preferences();
- $this->data->get_global_module_preferences();
- $this->data->show_all_sites();
- // -------------------------------------
- // run upgrade or downgrade scripts
- // -------------------------------------
- if (FREEFORM_PRO AND $this->data->global_preference('ffp') === 'n' OR
- ! FREEFORM_PRO AND $this->data->global_preference('ffp') === 'y')
- {
- $_GET['method'] = 'freeform_module_update';
- $this->pro_update = TRUE;
- }
-
- $this->cached_vars['module_menu'] = $menu;
- }
- // END Freeform_cp_base()
- //---------------------------------------------------------------------
- // begin views
- //---------------------------------------------------------------------
- // --------------------------------------------------------------------
- /**
- * Module's Main Homepage
- *
- * @access public
- * @param string
- * @return null
- */
- public function index ($message='')
- {
- if ($message == '' AND ee()->input->get('msg') !== FALSE)
- {
- $message = lang(ee()->input->get('msg'));
- }
- return $this->forms($message);
- }
- // END index()
- // --------------------------------------------------------------------
- /**
- * My Forms
- *
- * @access public
- * @param string $message incoming message for flash data
- * @return string html output
- */
- public function forms ( $message = '' )
- {
- // -------------------------------------
- // Messages
- // -------------------------------------
- if ($message == '' AND ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
- {
- $message = lang(ee()->input->get('msg'));
- }
- $this->cached_vars['message'] = $message;
- //--------------------------------------------
- // Crumbs and tab highlight
- //--------------------------------------------
- $new_form_link = $this->mod_link(array(
- 'method' => 'edit_form'
- ));
- $this->cached_vars['new_form_link'] = $new_form_link;
- $this->add_crumb( lang('forms') );
- $this->freeform_add_right_link(lang('new_form'), $new_form_link);
- $this->set_highlight('module_forms');
- //--------------------------------------
- // start vars
- //--------------------------------------
- $row_limit = $this->data->defaults['mcp_row_limit'];
- $paginate = '';
- $row_count = 0;
- // -------------------------------------
- // pagination?
- // -------------------------------------
- ee()->load->model('freeform_form_model');
- if ( ! $this->data->show_all_sites())
- {
- ee()->freeform_form_model->where(
- 'site_id',
- ee()->config->item('site_id')
- );
- }
- $total_results = ee()->freeform_form_model->count(array(), FALSE);
- // do we need pagination?
- if ( $total_results > $row_limit )
- {
- $row_count = $this->get_post_or_zero('row');
- $url = $this->mod_link(array(
- 'method' => 'forms'
- ));
- //get pagination info
- $pagination_data = $this->universal_pagination(array(
- 'total_results' => $total_results,
- 'limit' => $row_limit,
- 'current_page' => $row_count,
- 'pagination_config' => array('base_url' => $url),
- 'query_string_segment' => 'row'
- ));
- ee()->freeform_form_model->limit(
- $row_limit,
- $pagination_data['pagination_page']
- );
- $paginate = $pagination_data['pagination_links'];
- }
- ee()->freeform_form_model->order_by('form_label');
- $this->cached_vars['paginate'] = $paginate;
- // -------------------------------------
- // Did they upgrade from FF3?
- // -------------------------------------
- $this->cached_vars['legacy'] = FALSE;
- $this->cached_vars['migrate_link'] = '';
- ee()->load->library('freeform_migration');
- if ( ee()->freeform_migration->legacy() === TRUE )
- {
- $this->cached_vars['legacy'] = TRUE;
- $this->cached_vars['migrate_link'] = $this->mod_link(array('method' => 'utilities'));
- }
- // -------------------------------------
- // data
- // -------------------------------------
- $rows = ee()->freeform_form_model->get();
- $form_data = array();
- if ($rows !== FALSE)
- {
- // -------------------------------------
- // check for composer for each form
- // -------------------------------------
- $form_ids = array();
- $potential_composer_ids = array();
- foreach ($rows as $row)
- {
- $form_ids[] = $row['form_id'];
- if ($this->is_positive_intlike($row['composer_id']))
- {
- $potential_composer_ids[$row['form_id']] = $row['composer_id'];
- }
- }
- $has_composer = array();
- if ( ! empty($potential_composer_ids))
- {
- ee()->load->model('freeform_composer_model');
- $composer_ids = ee()->freeform_composer_model
- ->key('composer_id', 'composer_id')
- ->where('preview !=', 'y')
- ->where_in(
- 'composer_id',
- array_values($potential_composer_ids)
- )
- ->get();
- if ( ! empty($composer_ids))
- {
- foreach ($potential_composer_ids as $form_id => $composer_id)
- {
- if (in_array($composer_id, $composer_ids))
- {
- $has_composer[$form_id] = $composer_id;
- }
- }
- }
- }
- // -------------------------------------
- // suppliment rows
- // -------------------------------------
- foreach ($rows as $row)
- {
- $row['submissions_count'] = (
- $this->data->get_form_submissions_count($row['form_id'])
- );
- $row['moderate_count'] = (
- $this->data->get_form_needs_moderation_count($row['form_id'])
- );
- $row['has_composer'] = isset(
- $has_composer[$row['form_id']]
- );
- // -------------------------------------
- // piles o' links
- // -------------------------------------
- $row['form_submissions_link'] = $this->mod_link(array(
- 'method' => 'entries',
- 'form_id' => $row['form_id']
- ));
- $row['form_moderate_link'] = $this->mod_link(array(
- 'method' => 'moderate_entries',
- 'form_id' => $row['form_id'],
- 'search_status' => 'pending'
- ));
- $row['form_edit_composer_link'] = $this->mod_link(array(
- 'method' => 'form_composer',
- 'form_id' => $row['form_id']
- ));
- $row['form_settings_link'] = $this->mod_link(array(
- 'method' => 'edit_form',
- 'form_id' => $row['form_id']
- ));
- $row['form_duplicate_link'] = $this->mod_link(array(
- 'method' => 'edit_form',
- 'duplicate_id' => $row['form_id']
- ));
- $row['form_delete_link'] = $this->mod_link(array(
- 'method' => 'delete_confirm_form',
- 'form_id' => $row['form_id']
- ));
- $form_data[] = $row;
- }
- }
- $this->cached_vars['form_data'] = $form_data;
- $this->cached_vars['form_url'] = $this->mod_link(array(
- 'method' => 'delete_confirm_form'
- ));
- // ----------------------------------------
- // Load vars
- // ----------------------------------------
- // -------------------------------------
- // JS
- // -------------------------------------
- ee()->cp->add_js_script(
- array('plugin' => array('tooltip', 'dataTables'))
- );
- // --------------------------------------------
- // Load page
- // --------------------------------------------
- $this->cached_vars['current_page'] = $this->view(
- 'forms.html',
- NULL,
- TRUE
- );
- return $this->ee_cp_view('index.html');
- }
- //END forms
- // --------------------------------------------------------------------
- /**
- * delete_confirm_form
- *
- * @access public
- * @return string
- */
- public function delete_confirm_form ()
- {
- $form_ids = ee()->input->get_post('form_id', TRUE);
- if ( ! is_array($form_ids) AND
- ! $this->is_positive_intlike($form_ids) )
- {
- $this->actions()->full_stop(lang('no_form_ids_submitted'));
- }
- //already checked for numeric :p
- if ( ! is_array($form_ids))
- {
- $form_ids = array($form_ids);
- }
- return $this->delete_confirm(
- 'delete_forms',
- array('form_ids' => $form_ids),
- 'delete_form_confirmation'
- );
- }
- //END delete_confirm_form
- // --------------------------------------------------------------------
- /**
- * delete_forms
- *
- * @access public
- * @return string
- */
- public function delete_forms ($form_ids = array())
- {
- $message = 'delete_form_success';
- if ( empty($form_ids) )
- {
- $form_ids = ee()->input->get_post('form_ids');
- }
- if ( ! is_array($form_ids) AND
- $this->is_positive_intlike($form_ids))
- {
- $form_ids = array($form_ids);
- }
- //if everything is all nice and array like, DELORT
- //but one last check on each item to make sure its a number
- if ( is_array($form_ids))
- {
- ee()->load->library('freeform_forms');
- foreach ($form_ids as $form_id)
- {
- if ($this->is_positive_intlike($form_id))
- {
- ee()->freeform_forms->delete_form($form_id);
- }
- }
- }
- //the voyage home
- ee()->functions->redirect($this->mod_link(array(
- 'method' => 'index',
- 'msg' => $message
- )));
- }
- //END delete_forms
- // --------------------------------------------------------------------
- /**
- * Edit Form
- *
- * @access public
- * @return string html output
- */
- public function edit_form ()
- {
- // -------------------------------------
- // form ID? we must be editing
- // -------------------------------------
- $form_id = $this->get_post_or_zero('form_id');
- $update = $this->cached_vars['update'] = ($form_id != 0);
- // -------------------------------------
- // default data
- // -------------------------------------
- $inputs = array(
- 'form_id' => '0',
- 'form_name' => '',
- 'form_label' => '',
- 'default_status' => $this->data->defaults['default_form_status'],
- 'notify_admin' => 'n',
- 'notify_user' => 'n',
- 'user_email_field' => '',
- 'user_notification_id' => '0',
- 'admin_notification_id' => '0',
- 'admin_notification_email' => ee()->config->item('webmaster_email'),
- 'form_description' => '',
- 'template_id' => '0',
- 'composer_id' => '0',
- 'field_ids' => '',
- 'field_order' => '',
- );
- // -------------------------------------
- // updating?
- // -------------------------------------
- if ($update)
- {
- $form_data = $this->data->get_form_info($form_id);
- if ($form_data)
- {
- foreach ($form_data as $key => $value)
- {
- if ($key == 'admin_notification_email')
- {
- $value = str_replace('|', ', ', $value);
- }
- if ($key == 'field_ids' AND ! empty($value))
- {
- $value = implode('|', $value);
- }
- $inputs[$key] = form_prep($value);
- }
- }
- else
- {
- $this->actions()->full_stop(lang('invalid_form_id'));
- }
- }
- //--------------------------------------------
- // Crumbs and tab highlight
- //--------------------------------------------
- $this->add_crumb(
- lang('forms'),
- $this->mod_link(array('method' => 'forms'))
- );
- $this->add_crumb(
- $update ?
- lang('update_form') . ': ' . $form_data['form_label'] :
- lang('new_form')
- );
- $this->set_highlight('module_forms');
- // -------------------------------------
- // duplicating?
- // -------------------------------------
- $duplicate_id = $this->get_post_or_zero('duplicate_id');
- $this->cached_vars['duplicate_id'] = $duplicate_id;
- $this->cached_vars['duplicated'] = FALSE;
- if ( ! $update AND $duplicate_id > 0)
- {
- $form_data = $this->data->get_form_info($duplicate_id);
- if ($form_data)
- {
- foreach ($form_data as $key => $value)
- {
- if (in_array($key, array('form_id', 'form_label', 'form_name')))
- {
- continue;
- }
- if ($key == 'admin_notification_email')
- {
- $value = str_replace('|', ', ', $value);
- }
- $inputs[$key] = $value;
- }
- $this->cached_vars['duplicated'] = TRUE;
- $this->cached_vars['duplicated_from'] = $form_data['form_label'];
- }
- }
- if (isset($form_data['field_ids']) AND
- ! empty($form_data['field_ids']) AND
- isset($form_data['field_order']) AND
- ! empty($form_data['field_order']))
- {
- $field_ids = $form_data['field_ids'];
- if ( ! is_array($field_ids))
- {
- $field_ids = $this->actions()->pipe_split($field_ids);
- }
- $field_order = $form_data['field_order'];
- if ( ! is_array($field_order))
- {
- $field_order = $this->actions()->pipe_split($field_order);
- }
- $missing_ids = array_diff($field_ids, $field_order);
- $inputs['field_order'] = implode('|', array_merge($field_order, $missing_ids));
- }
- // -------------------------------------
- // load inputs
- // -------------------------------------
- foreach ($inputs as $key => $value)
- {
- $this->cached_vars[$key] = $value;
- }
- // -------------------------------------
- // select boxes
- // -------------------------------------
- $this->cached_vars['statuses'] = $this->data->get_form_statuses();
- ee()->load->model('freeform_field_model');
- $available_fields = ee()->freeform_field_model->get();
- $available_fields = ($available_fields !== FALSE) ?
- $available_fields :
- array();
- //fields
- $this->cached_vars['available_fields'] = $available_fields;
- //notifications
- $this->cached_vars['notifications'] = $this->data->get_available_notification_templates();
- // -------------------------------------
- // user email fields
- // -------------------------------------
- $user_email_fields = array('' => lang('choose_user_email_field'));
- $f_rows = ee()->freeform_field_model
- ->select('field_id, field_label, settings')
- ->get(array('field_type' => 'text'));
- //we only want fields that are being validated as email
- if ($f_rows)
- {
- foreach ($f_rows as $row)
- {
- $row_settings = json_decode($row['settings'], TRUE);
- $row_settings = (is_array($row_settings)) ? $row_settings : array();
- if (isset($row_settings['field_content_type']) AND
- $row_settings['field_content_type'] == 'email')
- {
- $user_email_fields[$row['field_id']] = $row['field_label'];
- }
- }
- }
- $this->cached_vars['user_email_fields'] = $user_email_fields;
- // ----------------------------------------
- // Load vars
- // ----------------------------------------
- $this->cached_vars['form_uri'] = $this->mod_link(array(
- 'method' => 'save_form'
- ));
- // -------------------------------------
- // js libs
- // -------------------------------------
- $this->load_fancybox();
- $this->cached_vars['cp_javascript'][] = 'jquery.smooth-scroll.min';
- ee()->cp->add_js_script(array(
- 'ui' => array('draggable', 'droppable', 'sortable')
- ));
- // --------------------------------------------
- // Load page
- // --------------------------------------------
- $this->cached_vars['current_page'] = $this->view(
- 'edit_form.html',
- NULL,
- TRUE
- );
- return $this->ee_cp_view('index.html');
- }
- //END edit_form
- // --------------------------------------------------------------------
- /**
- * form composer
- *
- * ajax form and field builder
- *
- * @access public
- * @param string message lang line
- * @return string html output
- */
- public function form_composer ( $message = '' )
- {
- // -------------------------------------
- // Messages
- // -------------------------------------
- if ($message == '' AND ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
- {
- $message = lang(ee()->input->get('msg'));
- }
- $this->cached_vars['message'] = $message;
- // -------------------------------------
- // form_id
- // -------------------------------------
- $form_id = ee()->input->get_post('form_id', TRUE);
- $form_data = $this->data->get_form_info($form_id);
- if ( ! $form_data)
- {
- return $this->actions()->full_stop(lang('invalid_form_id'));
- }
- $update = $form_data['composer_id'] != 0;
- //--------------------------------------------
- // Crumbs and tab highlight
- //--------------------------------------------
- $this->add_crumb( lang('forms'), $this->base );
- $this->add_crumb( lang('composer') . ': ' . $form_data['form_label'] );
- $this->set_highlight('module_forms');
- // -------------------------------------
- // data
- // -------------------------------------
- $this->cached_vars['form_data'] = $form_data;
- // -------------------------------------
- // fields for composer
- // -------------------------------------
- ee()->load->model('freeform_field_model');
- $available_fields = ee()->freeform_field_model
- ->where('composer_use', 'y')
- ->order_by('field_label')
- ->key('field_name')
- ->get();
- $available_fields = ($available_fields !== FALSE) ?
- $available_fields :
- array();
- // -------------------------------------
- // templates
- // -------------------------------------
- ee()->load->model('freeform_template_model');
- $available_templates = ee()->freeform_template_model
- ->where('enable_template', 'y')
- ->order_by('template_label')
- ->key('template_id', 'template_label')
- ->get();
- $available_templates = ($available_templates !== FALSE) ?
- $available_templates :
- array();
- // -------------------------------------
- // get field output for composer
- // -------------------------------------
- ee()->load->library('freeform_fields');
- $field_composer_output = array();
- $field_id_list = array();
- foreach ($available_fields as $field_name => $field_data)
- {
- $field_id_list[$field_data['field_id']] = $field_name;
- //encode to keep JS from running
- //camel case because its exposed in JS
- $field_composer_output[$field_name] = $this->composer_field_data(
- $field_data['field_id'],
- $field_data,
- TRUE
- );
- }
- $this->cached_vars['field_id_list'] = $this->json_encode($field_id_list);
- $this->cached_vars['field_composer_output_json'] = $this->json_encode($field_composer_output);
- $this->cached_vars['available_fields'] = $available_fields;
- $this->cached_vars['available_templates'] = $available_templates;
- $this->cached_vars['prohibited_field_names'] = $this->data->prohibited_names;
- $this->cached_vars['notifications'] = $this->data->get_available_notification_templates();
- // -------------------------------------
- // previous composer data?
- // -------------------------------------
- $composer_data = '{}';
- if ($form_data['composer_id'] > 0)
- {
- ee()->load->model('freeform_composer_model');
- $composer = ee()->freeform_composer_model
- ->select('composer_data')
- ->where('composer_id', $form_data['composer_id'])
- ->get_row();
- if ($composer !== FALSE)
- {
- $composer_data_test = $this->json_decode($composer['composer_data']);
- if ($composer_data_test)
- {
- $composer_data = $composer['composer_data'];
- }
- }
- }
- $this->cached_vars['composer_layout_data'] = $composer_data;
- // ----------------------------------------
- // Load vars
- // ----------------------------------------
- $this->cached_vars['lang_allowed_html_tags'] = (
- lang('allowed_html_tags') .
- "<" . implode(">, <", $this->data->allowed_html_tags) . ">"
- );
- $this->cached_vars['captcha_dummy_url'] = $this->sc->addon_theme_url .
- 'images/captcha.png';
- $this->cached_vars['new_field_url'] = $this->mod_link(array(
- 'method' => 'edit_field',
- //this builds a URL, so yes this is intentionally a string
- 'modal' => 'true'
- ), TRUE);
- $this->cached_vars['field_data_url'] = $this->mod_link(array(
- 'method' => 'composer_field_data'
- ), TRUE);
- $this->cached_vars['composer_preview_url'] = $this->mod_link(array(
- 'method' => 'composer_preview',
- 'form_id' => $form_id
- ), TRUE);
- $this->cached_vars['composer_ajax_save_url'] = $this->mod_link(array(
- 'method' => 'save_composer_data',
- 'form_id' => $form_id
- ), TRUE);
- //
- $this->cached_vars['composer_save_url'] = $this->mod_link(array(
- 'method' => 'save_composer_data',
- 'form_id' => $form_id
- ));
- $this->cached_vars['allowed_html_tags'] = "'" .
- implode("','", $this->data->allowed_html_tags) . "'";
- // -------------------------------------
- // js libs
- // -------------------------------------
- $this->load_fancybox();
- ee()->cp->add_js_script(array(
- 'ui' => array('sortable', 'draggable', 'droppable'),
- 'file' => array('underscore', 'json2')
- ));
- // --------------------------------------------
- // Load page
- // --------------------------------------------
- $this->cached_vars['cp_javascript'][] = 'composer_cp.min';
- $this->cached_vars['cp_javascript'][] = 'edit_field_cp.min';
- $this->cached_vars['cp_javascript'][] = 'security.min';
- $this->cached_vars['current_page'] = $this->view(
- 'composer.html',
- NULL,
- TRUE
- );
- return $this->ee_cp_view('index.html');
- }
- //END form_composer
- // --------------------------------------------------------------------
- /**
- * Composer preview
- *
- * @access public
- * @return mixed ajax return if detected or else html without cp
- */
- public function composer_preview ()
- {
- $form_id = $this->get_post_or_zero('form_id');
- $template_id = $this->get_post_or_zero('template_id');
- $composer_id = $this->get_post_or_zero('composer_id');
- $preview_id = $this->get_post_or_zero('preview_id');
- $subpreview = (ee()->input->get_post('subpreview') !== FALSE);
- $composer_page = $this->get_post_or_zero('composer_page');
- if ( ! $this->data->is_valid_form_id($form_id))
- {
- $this->actions()->full_stop(lang('invalid_form_id'));
- }
- // -------------------------------------
- // is this a preview?
- // -------------------------------------
- if ($preview_id > 0)
- {
- $preview_mode = TRUE;
- $composer_id = $preview_id;
- }
- $page_count = 1;
- // -------------------------------------
- // main output or sub output?
- // -------------------------------------
- if ( ! $subpreview)
- {
- // -------------------------------------
- // get composer data and build page count
- // -------------------------------------
- if ($composer_id > 0)
- {
- ee()->load->model('freeform_composer_model');
- $composer = ee()->freeform_composer_model
- ->select('composer_data')
- ->where('composer_id', $composer_id)
- ->get_row();
- if ($composer !== FALSE)
- {
- $composer_data = $this->json_decode(
- $composer['composer_data'],
- TRUE
- );
- if ($composer_data AND
- isset($composer_data['rows']) AND
- ! empty($composer_data['rows']))
- {
- foreach ($composer_data['rows'] as $row)
- {
- if ($row == 'page_break')
- {
- $page_count++;
- }
- }
- }
- }
- }
- $page_url = array();
- for ($i = 1, $l = $page_count; $i <= $l; $i++)
- {
- $page_url[] = $this->mod_link(array(
- 'method' => __FUNCTION__,
- 'form_id' => $form_id,
- 'template_id' => $template_id,
- 'preview_id' => $preview_id,
- 'subpreview' => 'true',
- 'composer_page' => $i
- ));
- }
- $this->cached_vars['page_url'] = $page_url;
- $this->cached_vars['default_preview_css'] = $this->sc->addon_theme_url .
- 'css/default_composer.css';
- $this->cached_vars['jquery_src'] = rtrim(ee()->config->item('theme_folder_url'), '/') .
- '/javascript/compressed/jquery/jquery.js';
- $html = $this->view('composer_preview.html', NULL, TRUE);
- }
- else
- {
- $subhtml = "{exp:freeform:composer form_id='$form_id'";
- $subhtml .= ($composer_page > 1) ? " multipage_page='" . $composer_page . "'" : '';
- $subhtml .= ($template_id > 0) ? " composer_template_id='" . $template_id . "'" : '';
- $subhtml .= ($preview_id > 0) ? " preview_id='" . $preview_id . "'" : '';
- $subhtml .= "}";
- $html = $this->actions()->template()->process_string_as_template($subhtml);
- }
- if ($this->is_ajax_request())
- {
- return $this->send_ajax_response(array(
- 'success' => TRUE,
- 'html' => $html
- ));
- }
- else
- {
- exit($html);
- }
- }
- //end composer preview
- // --------------------------------------------------------------------
- /**
- * entries
- *
- * @access public
- * @param string $message message lang line
- * @param bool $moderate are we moderating?
- * @param bool $export export?
- * @return string html output
- */
- public function entries ( $message = '' , $moderate = FALSE, $export = FALSE)
- {
- // -------------------------------------
- // Messages
- // -------------------------------------
- if ($message == '' AND
- ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
- {
- $message = lang(ee()->input->get('msg', TRUE));
- }
- $this->cached_vars['message'] = $message;
- // -------------------------------------
- // moderate
- // -------------------------------------
- $search_status = ee()->input->get_post('search_status');
- $moderate = (
- $moderate AND
- ($search_status == 'pending' OR
- $search_status === FALSE
- )
- );
- //if moderate and search status was not submitted, fake into pending
- if ($moderate AND $search_status === FALSE)
- {
- $_POST['search_status'] = 'pending';
- }
- $this->cached_vars['moderate'] = $moderate;
- $this->cached_vars['method'] = $method = (
- $moderate ? 'moderate_entries' : 'entries'
- );
- // -------------------------------------
- // form data? legit? GTFO?
- // -------------------------------------
- $form_id = ee()->input->get_post('form_id');
- ee()->load->library('freeform_forms');
- ee()->load->model('freeform_form_model');
- //form data does all of the proper id validity checks for us
- $form_data = $this->data->get_form_info($form_id);
- if ( ! $form_data)
- {
- $this->actions()->full_stop(lang('invalid_form_id'));
- exit();
- }
- $this->cached_vars['form_id'] = $form_id;
- $this->cached_vars['form_label'] = $form_data['form_label'];
- //--------------------------------------------
- // Crumbs and tab highlight
- //--------------------------------------------
- $this->add_crumb(
- lang('forms'),
- $this->mod_link(array('method' => 'forms'))
- );
- $this->add_crumb(
- $form_data['form_label'] . ': ' .
- lang($moderate ? 'moderate' : 'entries')
- );
- $this->set_highlight('module_forms');
- $this->freeform_add_right_link(
- lang('new_entry'),
- $this->mod_link(array(
- 'method' => 'edit_entry',
- 'form_id' => $form_id
- ))
- );
- // -------------------------------------
- // status prefs
- // -------------------------------------
- $form_statuses = $this->data->get_form_statuses();
- $this->cached_vars['form_statuses'] = $form_statuses;
- // -------------------------------------
- // rest of models
- // -------------------------------------
- ee()->load->model('freeform_entry_model');
- ee()->freeform_entry_model->id($form_id);
- // -------------------------------------
- // custom field labels
- // -------------------------------------
- $standard_columns = $this->get_standard_column_names();
- //we want author instead of author id until we get data
- $possible_columns = $standard_columns;
- //key = value
- $all_columns = array_combine($standard_columns, $standard_columns);
- $column_labels = array();
- $field_column_names = array();
- //field prefix
- $f_prefix = ee()->freeform_form_model->form_field_prefix;
- //keyed labels for the front end
- foreach ($standard_columns as $column_name)
- {
- $column_labels[$column_name] = lang($column_name);
- }
- // -------------------------------------
- // check for fields with custom views for entry tables
- // -------------------------------------
- ee()->load->library('freeform_fields');
- //fields in this form
- foreach ($form_data['fields'] as $field_id => $field_data)
- {
- //outputs form_field_1, form_field_2, etc for ->select()
- $field_id_name = $f_prefix . $field_id;
- $field_column_names[$field_id_name] = $field_data['field_name'];
- $all_columns[$field_id_name] = $field_data['field_name'];
- $column_labels[$field_data['field_name']] = $field_data['field_label'];
- $column_labels[$field_id_name] = $field_data['field_label'];
- $possible_columns[] = $field_id;
- $instance =& ee()->freeform_fields->get_field_instance(array(
- 'field_id' => $field_id,
- 'field_data' => $field_data
- ));
- if ( ! empty($instance->entry_views))
- {
- foreach ($instance->entry_views as $e_lang => $e_method)
- {
- $this->freeform_add_right_link(
- $e_lang,
- $this->mod_link(array(
- 'method' => 'field_method',
- 'field_id' => $field_id,
- 'field_method' => $e_method,
- 'form_id' => $form_id
- ))
- );
- }
- }
- }
- // -------------------------------------
- // visible columns
- // -------------------------------------
- $visible_columns = $this->visible_columns($standard_columns, $possible_columns);
- $this->cached_vars['visible_columns'] = $visible_columns;
- $this->cached_vars['column_labels'] = $column_labels;
- $this->cached_vars['possible_columns'] = $possible_columns;
- $this->cached_vars['all_columns'] = $all_columns;
- // -------------------------------------
- // prep unused from from possible
- // -------------------------------------
- //so so used
- $un_used = array();
- foreach ($possible_columns as $pcid)
- {
- $check = ($this->is_positive_intlike($pcid)) ?
- $f_prefix . $pcid :
- $pcid;
- if ( ! in_array($check, $visible_columns))
- {
- $un_used[] = $check;
- }
- }
- $this->cached_vars['unused_columns'] = $un_used;
- // -------------------------------------
- // build query
- // -------------------------------------
- //base url for pagination
- $pag_url = array(
- 'method' => $method,
- 'form_id' => $form_id
- );
- //cleans out blank keys from unset
- $find_columns = array_merge(array(), $visible_columns);
- $must_haves = array('entry_id');
- // -------------------------------------
- // search criteria
- // building query
- // -------------------------------------
- $has_search = FALSE;
- $search_vars = array(
- 'search_keywords',
- 'search_status',
- 'search_date_range',
- 'search_date_range_start',
- 'search_date_range_end',
- 'search_on_field'
- );
- foreach ($search_vars as $search_var)
- {
- $$search_var = ee()->input->get_post($search_var, TRUE);
- //set for output
- $this->cached_vars[$search_var] = (
- ($$search_var) ? trim($$search_var) : ''
- );
- }
- // -------------------------------------
- // search keywords
- // -------------------------------------
- if ($search_keywords AND
- trim($search_keywords) !== '' AND
- $search_on_field AND
- in_array($search_on_field, $visible_columns))
- {
- ee()->freeform_entry_model->like(
- $search_on_field,
- $search_keywords
- );
- //pagination
- $pag_url['search_keywords'] = $search_keywords;
- $pag_url['search_on_field'] = $search_on_field;
- $has_search = TRUE;
- }
- //no search on field? guess we had better search it all *gulp*
- else if ($search_keywords AND trim($search_keywords) !== '')
- {
- $first = TRUE;
- ee()->freeform_entry_model->group_like(
- $search_keywords,
- array_values($visible_columns)
- );
- $pag_url['search_keywords'] = $search_keywords;
- $has_search = TRUE;
- }
- //status search?
- if ($moderate)
- {
- ee()->freeform_entry_model->where('status', 'pending');
- }
- else if ($search_status AND in_array($search_status, array_flip( $form_statuses)))
- {
- ee()->freeform_entry_model->where('status', $search_status);
- //pagination
- $pag_url['search_status'] = $search_status;
- $has_search = TRUE;
- }
- // -------------------------------------
- // date range?
- // -------------------------------------
- //pagination
- if ($search_date_range == 'date_range')
- {
- if ($search_date_range_start !== FALSE)
- {
- $pag_url['search_date_range_start'] = $search_date_range_start;
- }
- if ($search_date_range_end !== FALSE)
- {
- $pag_url['search_date_range_end'] = $search_date_range_end;
- }
- //pagination
- if ($search_date_range_start OR $search_date_range_end)
- {
- $pag_url['search_date_range'] = 'date_range';
- $has_search = TRUE;
- }
- }
- else if ($search_date_range !== FALSE)
- {
- $pag_url['search_date_range'] = $search_date_range;
- $has_search = TRUE;
- }
- ee()->freeform_entry_model->date_where(
- $search_date_range,
- $search_date_range_start,
- $search_date_range_end
- );
- // -------------------------------------
- // any searches?
- // -------------------------------------
- $this->cached_vars['has_search'] = $has_search;
- // -------------------------------------
- // data from all sites?
- // -------------------------------------
- if ( ! $this->data->show_all_sites())
- {
- ee()->freeform_entry_model->where(
- 'site_id',
- ee()->config->item('site_id')
- );
- }
- //we need the counts for exports and end results
- $total_entries = ee()->freeform_entry_model->count(array(), FALSE);
- // -------------------------------------
- // orderby
- // -------------------------------------
- $order_by = 'entry_date';
- $p_order_by = ee()->input->get_post('order_by');
- if ($p_order_by !== FALSE AND in_array($p_order_by, $all_columns))
- {
- $order_by = $p_order_by;
- $pag_url['order_by'] = $order_by;
- }
- // -------------------------------------
- // sort
- // -------------------------------------
- $sort = ($order_by == 'entry_date') ? 'desc' : 'asc';
- $p_sort = ee()->input->get_post('sort');
- if ($p_sort !== FALSE AND
- in_array(strtolower($p_sort), array('asc', 'desc')))
- {
- $sort = strtolower($p_sort);
- $pag_url['sort'] = $sort;
- }
- ee()->freeform_entry_model->order_by($order_by, $sort);
- $this->cached_vars['order_by'] = $order_by;
- $this->cached_vars['sort'] = $sort;
- // -------------------------------------
- // export button
- // -------------------------------------
- if ($total_entries > 0)
- {
- $this->freeform_add_right_link(
- lang('export_entries'),
- '#export_entries'
- );
- }
- // -------------------------------------
- // export url
- // -------------------------------------
- $export_url = $pag_url;
- $export_url['moderate'] = $moderate ? 'true' : 'false';
- $export_url['method'] = 'export_entries';
- $this->cached_vars['export_url'] = $this->mod_link($export_url);
- // -------------------------------------
- // export?
- // -------------------------------------
- if ($export)
- {
- $export_fields = ee()->input->get_post('export_fields');
- $export_labels = $column_labels;
- // -------------------------------------
- // build possible select alls
- // -------------------------------------
- $select = array();
- //are we sending just the selected fields?
- if ($export_fields != 'all')
- {
- $select = array_unique(array_merge($must_haves, $find_columns));
- foreach ($export_labels as $key => $value)
- {
- //clean export labels for json
- if ( ! in_array($key, $select))
- {
- unset($export_labels[$key]);
- }
- }
- //get real names
- foreach ($select as $key => $value)
- {
- if (isset($field_column_names[$value]))
- {
- $select[$key] = $field_column_names[$value];
- }
- }
- }
- //sending all fields means we need to still clean some labels
- else
- {
- foreach ($all_columns as $field_id_name => $field_name)
- {
- //clean export labels for json
- if ($field_id_name != $field_name)
- {
- unset($export_labels[$field_id_name]);
- }
- $select[] = $field_name;
- }
- }
- foreach ($export_labels as $key => $value)
- {
- //fix entities
- $value = html_entity_decode($value, ENT_COMPAT, 'UTF-8');
- $export_labels[$key] = $value;
- if (isset($field_column_names[$key]))
- {
- $export_labels[$field_column_names[$key]] = $value;
- }
- }
- ee()->freeform_entry_model->select(implode(', ', $select));
- // -------------------------------------
- // check for chunking, etc
- // -------------------------------------
- ee()->load->library('freeform_export');
- ee()->freeform_export->export(array(
- 'method' => ee()->input->get_post('export_method'),
- 'form_id' => $form_id,
- 'form_name' => $form_data['form_name'],
- 'output' => 'download',
- 'model' => ee()->freeform_entry_model,
- 'remove_entry_id' => ($export_fields != 'all' AND ! in_array('entry_id', $visible_columns)),
- 'header_labels' => $export_labels,
- 'total_entries' => $total_entries
- ));
- }
- //END if ($export)
- // -------------------------------------
- // selects
- // -------------------------------------
- $needed_selects = array_unique(array_merge($must_haves, $find_columns));
- ee()->freeform_entry_model->select(implode(', ', $needed_selects));
- //--------------------------------------
- // pagination start vars
- //--------------------------------------
- $pag_url = $this->mod_link($pag_url);
- $row_limit = $this->data->defaults['mcp_row_limit'];
- $paginate = '';
- $row_count = 0;
- //moved above exports
- //$total_entries = ee()->freeform_entry_model->count(array(), FALSE);
- $current_page = 0;
- // -------------------------------------
- // pagination?
- // -------------------------------------
- // do we need pagination?
- if ($total_entries > $row_limit )
- {
- $row_count = $this->get_post_or_zero('row');
- //get pagination info
- $pagination_data = $this->universal_pagination(array(
- 'total_results' => $total_entries,
- 'limit' => $row_limit,
- 'current_page' => $row_count,
- 'pagination_config' => array('base_url' => $pag_url),
- 'query_string_segment' => 'row'
- ));
- $paginate = $pagination_data['pagination_links'];
- $current_page = $pagination_data['pagination_page'];
- ee()->freeform_entry_model->limit($row_limit, $current_page);
- }
- $this->cached_vars['paginate'] = $paginate;
- // -------------------------------------
- // get data
- // -------------------------------------
- $result_array = ee()->freeform_entry_model->get();
- $count = $row_count;
- $entries = array();
- if ( ! $result_array)
- {
- $result_array = array();
- }
- $entry_ids = array();
- foreach ($result_array as $row)
- {
- $entry_ids[] = $row['entry_id'];
- }
- // -------------------------------------
- // allow pre_process
- // -------------------------------------
- ee()->freeform_fields->apply_field_method(array(
- 'method' => 'pre_process_entries',
- 'form_id' => $form_id,
- 'entry_id' => $entry_ids,
- 'form_data' => $form_data,
- 'field_data' => $form_data['fields']
- ));
- foreach ( $result_array as $row)
- {
- //apply display_entry_cp to our field data
- $field_parse = ee()->freeform_fields->apply_field_method(array(
- 'method' => 'display_entry_cp',
- 'form_id' => $form_id,
- 'entry_id' => $row['entry_id'],
- 'form_data' => $form_data,
- 'field_data' => $form_data['fields'],
- 'field_input_data' => $row
- ));
- $row = array_merge($row, $field_parse['variables']);
- $entry = array();
- $entry['view_entry_link'] = $this->mod_link(array(
- 'method' => 'view_entry',
- 'form_id' => $form_id,
- 'entry_id' => $row['entry_id']
- ));
- $entry['edit_entry_link'] = $this->mod_link(array(
- 'method' => 'edit_entry',
- 'form_id' => $form_id,
- 'entry_id' => $row['entry_id']
- ));
- $entry['approve_link'] = $this->mod_link(array(
- 'method' => 'approve_entries',
- 'form_id' => $form_id,
- 'entry_ids' => $row['entry_id']
- ));
- $entry['count'] = ++$count;
- $entry['id'] = $row['entry_id'];
- // -------------------------------------
- // remove entry_id and author_id if we
- // arent showing them
- // -------------------------------------
- if ( ! in_array('entry_id', $visible_columns))
- {
- unset($row['entry_id']);
- }
- // -------------------------------------
- // dates
- // -------------------------------------
- if (in_array('entry_date', $visible_columns))
- {
- $row['entry_date'] = gmdate(
- $this->preference('cp_date_formatting'),
- ee()->localize->set_localized_time($row['entry_date'])
- );
- }
- if (in_array('edit_date', $visible_columns))
- {
- $row['edit_date'] = (
- ($row['edit_date'] > 0) ?
- gmdate(
- $this->preference('cp_date_formatting'),
- ee()->localize->set_localized_time($row['edit_date'])
- ) :
- lang('n_a')
- );
- }
- $entry['data'] = $row;
- $entries[] = $entry;
- }
- $this->cached_vars['entries'] = $entries;
- // -------------------------------------
- // ajax request?
- // -------------------------------------
- if ($this->is_ajax_request())
- {
- $this->send_ajax_response(array(
- 'entries' => $entries,
- 'paginate' => $paginate,
- 'visibleColumns' => $visible_columns,
- 'allColumns' => $all_columns,
- 'columnLabels' => $column_labels,
- 'success' => TRUE
- ));
- exit();
- }
- // -------------------------------------
- // moderation count?
- // -------------------------------------
- //lets not waste the query if we are already moderating
- $moderation_count = (
- ( ! $moderate) ?
- $this->data->get_form_needs_moderation_count($form_id) :
- 0
- );
- if ($moderation_count > 0)
- {
- $this->cached_vars['lang_num_items_awaiting_moderation'] = str_replace(
- array('%num%', '%form_label%'),
- array($moderation_count, $form_data['form_label']),
- lang('num_items_awaiting_moderation')
- );
- }
- $this->cached_vars['moderation_count'] = $moderation_count;
- $this->cached_vars['moderation_link'] = $this->mod_link(array(
- 'method' => 'moderate_entries',
- 'form_id' => $form_id,
- 'search_status' => 'pending'
- ));
- // -------------------------------------
- // is admin?
- // -------------------------------------
- $this->cached_vars['is_admin'] = $is_admin = (
- ee()->session->userdata('group_id') == 1
- );
- // -------------------------------------
- // can save field layout?
- // -------------------------------------
- //$this->cached_vars['can_edit_layout'] = $can_edit_layout = (
- // $is_admin OR
- // $this->check_yes($this->preference('allow_user_field_layout'))
- //);
- //just in case
- $this->cached_vars['can_edit_layout'] = TRUE;
- $this->freeform_add_right_link(
- lang('edit_field_layout'),
- '#edit_field_layout'
- );
- // -------------------------------------
- // member groups
- // -------------------------------------
- $member_groups = array();
- if ($is_admin)
- {
- ee()->db->select('group_id, group_title');
- $member_groups = $this->prepare_keyed_result(
- ee()->db->get('member_groups'),
- 'group_id',
- 'group_title'
- );
- }
- $this->cached_vars['member_groups'] = $member_groups;
- // -------------------------------------
- // lang items
- // -------------------------------------
- // -------------------------------------
- // no results lang
- // -------------------------------------
- $this->cached_vars['lang_no_results_for_form'] = (
- ($has_search) ?
- lang('no_results_for_search') :
- (
- ($moderate) ?
- lang('no_entries_awaiting_approval') :
- lang('no_entries_for_form')
- )
- );
- // -------------------------------------
- // moderation lang
- // -------------------------------------
- $this->cached_vars['lang_viewing_moderation'] = str_replace(
- '%form_label%',
- $form_data['form_label'],
- lang('viewing_moderation')
- );
- // -------------------------------------
- // other vars
- // -------------------------------------
- $this->cached_vars['form_url'] = $this->mod_link(array(
- 'method' => 'entries_action',
- 'return_method' => (($moderate) ? 'moderate_' : '' ) . 'entries'
- ));
- $this->cached_vars['save_layout_url'] = $this->mod_link(array(
- 'method' => 'save_field_layout'
- ));
- // -------------------------------------
- // js libs
- // -------------------------------------
- $this->load_fancybox();
- //$this->load_datatables();
- ee()->cp->add_js_script(array(
- 'ui' => array('datepicker', 'sortable'),
- 'file' => 'underscore'
- ));
- // --------------------------------------------
- // Load page
- // --------------------------------------------
- $this->cached_vars['current_page'] = $this->view(
- 'entries.html',
- NULL,
- TRUE
- );
- return $this->ee_cp_view('index.html');
- }
- //END entries
- // --------------------------------------------------------------------
- public function field_method ($message = '')
- {
- // -------------------------------------
- // Messages
- // -------------------------------------
- if ($message == '' AND
- ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
- {
- $message = lang(ee()->input->get('msg', TRUE));
- }
- $this->cached_vars['message'] = $message;
- // -------------------------------------
- // goods
- // -------------------------------------
- $form_id = $this->get_post_or_zero('form_id');
- $field_id = $this->get_post_or_zero('field_id');
- $field_method = ee()->input->get_post('field_method');
- $instance = FALSE;
- if ( $field_method == FALSE OR
- ! $this->data->is_valid_form_id($form_id) OR
- $field_id == 0)
- {
- ee()->functions->redirect($this->mod_link(array('method' => 'forms')));
- }
- ee()->load->library('freeform_fields');
- $instance =& ee()->freeform_fields->get_field_instance(array(
- 'form_id' => $form_id,
- 'field_id' => $field_id
- ));
- //legit?
- if ( ! is_object($instance) OR
- empty($instance->entry_views) OR
- //removed so you can post to this
- //! in_array($field_method, $instance->entry_views) OR
- ! is_callable(array($instance, $field_method)))
- {
- ee()->functions->redirect($this->mod_link(array('method' => 'forms')));
- }
- $method_lang = lang('field_entry_view');
- foreach ($instance->entry_views as $e_lang => $e_method)
- {
- if ($field_method == $e_method)
- {
- $method_lang = $e_lang;
- }
- }
- //--------------------------------------------
- // Crumbs and tab highlight
- //--------------------------------------------
- $this->add_crumb(
- lang('entries'),
- $this->mod_link(array(
- 'method' => 'entries',
- 'form_id' => $form_id
- ))
- );
- $this->add_crumb($method_lang);
- $this->set_highlight('module_forms');
- // --------------------------------------------
- // Load page
- // --------------------------------------------
- $this…
Large files files are truncated, but you can click here to view the full file