Example #1
0
    /**
     * Edit Config
     */
    function config_edit()
    {
        $default_config_settings = Wygwam_helper::default_config_settings();
        if (($config_id = $this->EE->input->get('config_id')) && ($query = $this->EE->db->get_where('wygwam_configs', array('config_id' => $config_id))) && $query->num_rows()) {
            $config = $query->row_array();
            $config['settings'] = unserialize(base64_decode($config['settings']));
            $config['settings'] = array_merge($default_config_settings, $config['settings']);
            // duplicate?
            if ($this->EE->input->get('clone') == 'y') {
                $config['config_id'] = '';
                $config['config_name'] .= ' ' . lang('wygwam_clone');
                $this->_set_page_title(lang('wygwam_create_config'));
            } else {
                $this->_set_page_title(lang('wygwam_edit_config') . ' - ' . $config['config_name']);
            }
        } else {
            $config = array('config_id' => '', 'config_name' => '', 'settings' => $default_config_settings);
            $this->_set_page_title(lang('wygwam_create_config'));
        }
        $vars['config'] = $config;
        $vars['base'] = $this->base;
        $this->EE->load->library('table');
        // -------------------------------------------
        //  Upload Directory
        // -------------------------------------------
        $wygwam_settings = Wygwam_helper::get_global_settings();
        $msm = $this->EE->config->item('multiple_sites_enabled') == 'y';
        // If we're using Assets and it is installed, let's show Assets sources instead of just EE filedirs.
        if (isset($wygwam_settings['file_browser']) && $wygwam_settings['file_browser'] == 'assets' && Wygwam_helper::is_assets_installed()) {
            // Initialize the Assets lib
            require_once PATH_THIRD . 'assets/helper.php';
            $assets_helper = new Assets_helper();
            if ($msm) {
                $query = $this->EE->db->query("SELECT site_id, site_label FROM exp_sites")->result();
                foreach ($query as $row) {
                    $site_map[$row->site_id] = $row->site_label;
                }
            }
            $upload_dirs = array('' => '--');
            $all_sources = ee()->assets_lib->get_all_sources();
            foreach ($all_sources as $source) {
                $upload_dirs[$source->type . ':' . $source->id] = ($msm && $source->type == 'ee' ? $site_map[$source->site_id] . ' - ' : '') . $source->name;
            }
        } else {
            $dir_query = $this->EE->db->query('SELECT u.id, u.name, s.site_label
											   FROM exp_upload_prefs u, exp_sites s
											   WHERE u.site_id = s.site_id
											   ORDER BY site_label, name');
            if ($dir_query->num_rows()) {
                $upload_dirs = array('' => '--');
                foreach ($dir_query->result_array() as $row) {
                    $upload_dirs[$row['id']] = ($msm ? $row['site_label'] . ' - ' : '') . $row['name'];
                }
            }
        }
        if (!empty($upload_dirs)) {
            $vars['upload_dir'] = form_dropdown('settings[upload_dir]', $upload_dirs, $config['settings']['upload_dir'], 'id="upload_dir"');
        } else {
            $this->EE->lang->loadfile('admin_content');
            $vars['upload_dir'] = lang('no_upload_prefs');
        }
        // -------------------------------------------
        //  Advanced Settings
        // -------------------------------------------
        // which settings have we already shown?
        $skip = array_keys($default_config_settings);
        // get settings that should be treated as lists
        $config_lists = Wygwam_helper::config_lists();
        // sort settings by key
        ksort($config['settings']);
        $js = '';
        foreach ($config['settings'] as $setting => $value) {
            // skip?
            if (in_array($setting, $skip)) {
                continue;
            }
            // format_tags?
            if ($setting == 'format_tags') {
                $value = explode(';', $value);
            }
            // list?
            if (in_array($setting, $config_lists)) {
                $value = implode("\n", $value);
            }
            $json = Wygwam_helper::get_json($value);
            $js .= 'new wygwam_addSettingRow("' . $setting . '", ' . $json . ');' . NL;
        }
        // Resources
        Wygwam_helper::include_theme_css('lib/ckeditor/skins/wygwam3/editor.css');
        Wygwam_helper::include_theme_css('styles/config_edit.css');
        $this->EE->cp->add_js_script(array('ui' => 'draggable'));
        Wygwam_helper::include_theme_js('lib/ckeditor/ckeditor.js');
        Wygwam_helper::include_theme_js('scripts/config_edit_toolbar.js');
        Wygwam_helper::include_theme_js('scripts/config_edit_advanced.js');
        Wygwam_helper::insert_js('jQuery(document).ready(function(){' . NL . $js . '});');
        Wygwam_helper::insert_js('jQuery("#restrict_html").ptSwitch();');
        return $this->EE->load->view('config_edit', $vars, TRUE);
    }
    /**
     * Edit Config
     */
    function config_edit()
    {
        $default_config_settings = Wygwam_helper::default_config_settings();
        if (($config_id = $this->EE->input->get('config_id')) && ($query = $this->EE->db->get_where('wygwam_configs', array('config_id' => $config_id))) && $query->num_rows()) {
            $config = $query->row_array();
            $config['settings'] = unserialize(base64_decode($config['settings']));
            $config['settings'] = array_merge($default_config_settings, $config['settings']);
            // duplicate?
            if ($this->EE->input->get('clone') == 'y') {
                $config['config_id'] = '';
                $config['config_name'] .= ' ' . lang('wygwam_clone');
                $this->_set_page_title(lang('wygwam_create_config'));
            } else {
                $this->_set_page_title(lang('wygwam_edit_config') . ' - ' . $config['config_name']);
            }
        } else {
            $config = array('config_id' => '', 'config_name' => '', 'settings' => $default_config_settings);
            $this->_set_page_title(lang('wygwam_create_config'));
        }
        $vars['config'] = $config;
        $vars['base'] = $this->base;
        $this->EE->load->library('table');
        // css and js
        Wygwam_helper::include_theme_css('lib/ckeditor/skins/wygwam2/editor.css');
        Wygwam_helper::include_theme_css('styles/config_edit.css');
        $this->EE->cp->add_js_script(array('ui' => 'draggable'));
        Wygwam_helper::include_theme_js('scripts/config_edit_toolbar.js');
        // -------------------------------------------
        //  Upload Directory
        // -------------------------------------------
        $dir_query = $this->EE->db->query('SELECT u.id, u.name, s.site_label
		                                   FROM exp_upload_prefs u, exp_sites s
		                                   WHERE u.site_id = s.site_id
		                                   ORDER BY site_label, name');
        if ($dir_query->num_rows()) {
            $msm = $this->EE->config->item('multiple_sites_enabled') == 'y';
            $upload_dirs = array('' => '--');
            foreach ($dir_query->result_array() as $row) {
                $upload_dirs[$row['id']] = ($msm ? $row['site_label'] . ' - ' : '') . $row['name'];
            }
            $vars['upload_dir'] = form_dropdown('settings[upload_dir]', $upload_dirs, $config['settings']['upload_dir'], 'id="upload_dir"');
        } else {
            $this->EE->lang->loadfile('admin_content');
            $vars['upload_dir'] = lang('no_upload_prefs');
        }
        // -------------------------------------------
        //  Advanced Settings
        // -------------------------------------------
        // which settings have we already shown?
        $skip = array_keys($default_config_settings);
        // get settings that should be treated as lists
        $config_lists = Wygwam_helper::config_lists();
        // sort settings by key
        ksort($config['settings']);
        $js = '';
        foreach ($config['settings'] as $setting => $value) {
            // skip?
            if (in_array($setting, $skip)) {
                continue;
            }
            // format_tags?
            if ($setting == 'format_tags') {
                $value = explode(';', $value);
            }
            // list?
            if (in_array($setting, $config_lists)) {
                $value = implode("\n", $value);
            }
            $json = $this->EE->javascript->generate_json($value, TRUE);
            $js .= 'new wygwam_addSettingRow("' . $setting . '", ' . $json . ');' . NL;
        }
        Wygwam_helper::include_theme_js('scripts/config_edit_advanced.js');
        Wygwam_helper::insert_js('jQuery(document).ready(function(){' . NL . $js . '});');
        return $this->EE->load->view('config_edit', $vars, TRUE);
    }
 /**
  * Install
  */
 function install()
 {
     $this->EE->load->dbforge();
     $this->EE->db->insert('modules', array('module_name' => WYGWAM_NAME, 'module_version' => WYGWAM_VER, 'has_cp_backend' => 'y', 'has_publish_fields' => 'n'));
     // -------------------------------------------
     //  Create the exp_wygwam_configs table
     // -------------------------------------------
     if (!$this->EE->db->table_exists('wygwam_configs')) {
         $this->EE->load->dbforge();
         $this->EE->dbforge->add_field(array('config_id' => array('type' => 'int', 'constraint' => 6, 'unsigned' => TRUE, 'auto_increment' => TRUE), 'config_name' => array('type' => 'varchar', 'constraint' => 32), 'settings' => array('type' => 'text')));
         $this->EE->dbforge->add_key('config_id', TRUE);
         $this->EE->dbforge->create_table('wygwam_configs');
     }
     // -------------------------------------------
     //  Populate it
     // -------------------------------------------
     // Do toolbars already exist in the fieldtype's global settings?
     $this->EE->db->select('settings');
     $query = $this->EE->db->get_where('fieldtypes', array('name' => 'wygwam'));
     if ($query->num_rows() && ($ft_settings = unserialize(base64_decode($query->row('settings')))) && is_array($ft_settings) && isset($ft_settings['toolbars']) && is_array($ft_settings['toolbars']) && $ft_settings['toolbars']) {
         $toolbars = $ft_settings['toolbars'];
         foreach ($toolbars as &$toolbar) {
             // stylesCombo_stylesSet => stylesSet
             if (isset($toolbar['stylesCombo_stylesSet'])) {
                 $toolbar['stylesSet'] = $toolbar['stylesCombo_stylesSet'];
                 unset($toolbar['stylesCombo_stylesSet']);
             }
             // remove MediaEmbed
             if (($key = array_search('MediaEmbed', $toolbar)) !== FALSE) {
                 array_splice($toolbar, $key, 1, 'EmbedMedia');
             }
         }
     } else {
         $toolbars = Wygwam_helper::default_toolbars();
     }
     foreach ($toolbars as $name => &$toolbar) {
         $config_settings = array_merge(Wygwam_helper::default_config_settings(), array('toolbar' => $toolbar));
         $this->EE->db->insert('wygwam_configs', array('config_name' => $name, 'settings' => base64_encode(serialize($config_settings))));
     }
     // -------------------------------------------
     //  Get Config IDs
     // -------------------------------------------
     $config_ids = array();
     $this->EE->db->select('config_id, config_name');
     $query = $this->EE->db->get('wygwam_configs');
     foreach ($query->result_array() as $config) {
         $config_ids[$config['config_name']] = $config['config_id'];
     }
     // -------------------------------------------
     //  Update fields
     // -------------------------------------------
     $this->EE->db->select('field_id, field_label, field_settings');
     $query = $this->EE->db->get_where('channel_fields', array('field_type' => 'wygwam'));
     foreach ($query->result_array() as $field) {
         $field_settings = unserialize(base64_decode($field['field_settings']));
         if (!isset($field_settings['toolbar']) || !isset($toolbars[$field_settings['toolbar']])) {
             // wtf is wrong with this?
             continue;
         }
         // create a new config?
         if (isset($field_settings['config']) && $field_settings['config'] || (isset($field_settings['upload_dir']) || $field_settings['upload_dir'])) {
             $new_config_settings = array_merge(Wygwam_helper::default_config_settings(), array('toolbar' => $toolbars[$field_settings['toolbar']]));
             // merge in config settings
             if (isset($field_settings['config']) && $field_settings['config']) {
                 $new_config_settings = array_merge($new_config_settings, $field_settings['config']);
                 unset($field_settings['config']);
             }
             // merge in upload directory
             if (isset($field_settings['upload_dir']) && $field_settings['upload_dir']) {
                 $new_config_settings['upload_dir'] = $field_settings['upload_dir'];
                 unset($field_settings['upload_dir']);
             }
             // add the new config
             $this->EE->db->insert('wygwam_configs', array('config_name' => $field_settings['toolbar'] . ' - ' . $field['field_label'], 'settings' => base64_encode(serialize($new_config_settings))));
             // get its ID
             $this->EE->db->select_max('config_id');
             $query = $this->EE->db->get('wygwam_configs');
             $field_settings['config'] = $query->row('config_id');
         } else {
             $field_settings['config'] = $config_ids[$field_settings['toolbar']];
         }
         unset($field_settings['toolbar']);
         // update the field
         $data = array('field_settings' => base64_encode(serialize($field_settings)));
         $this->EE->db->update('channel_fields', $data, array('field_id' => $field['field_id']));
     }
     return TRUE;
 }