Example #1
0
    /**
     * Note that the HTML in $option_html should match the JavaScript version of
     * the same HTML in js/dropdown.js (see the append_dropdown_option() function).
     * I couldn't think of a clean way to do this, but the fundamental problem is
     * that both PHP and JS need to draw the same HTML into this form:
     * PHP draws it when an existing definition is *edited*, whereas JS draws it
     * when you dynamically *create* new dropdown options.
     *
     * @param array   $def nested array of existing definition.
     * @return string
     */
    public function get_edit_field_definition($def)
    {
        // Standard
        $out = $this->format_standard_fields($def, false);
        $is_checked = '';
        $is_sql_checked = '';
        $readonly_str = ' readonly="readonly"';
        if (isset($def['use_key_values']) && $def['use_key_values']) {
            $is_checked = 'checked="checked"';
            $readonly_str = '';
        }
        if (isset($def['is_sql']) && $def['is_sql']) {
            $is_sql_checked = 'checked="checked"';
        }
        // Options
        $out .= '
			<div class="postbox">
				<div class="handlediv" title="Click to toggle"><br /></div>
				<h3 class="hndle"><span>' . __('Options', CCTM_TXTDOMAIN) . '</span></h3>
				<div class="inside">
					<table><tr><td width="600" style="vertical-align:top">';
        // Use Key => Value Pairs?  (if not, the simple usage is simple options)
        $out .= '
			<input type="hidden" name="use_key_values" value="0"/>
			<div class="' . self::wrapper_css_class . '" id="use_key_values_wrapper">
				 <label for="use_key_values" class="cctm_label cctm_checkbox_label" id="use_key_values_label">' . __('Distinct options/values?', CCTM_TXTDOMAIN) . '</label>
				 <br />
				 <input type="checkbox" name="use_key_values" class="cctm_checkbox" id="use_key_values" value="1" onclick="javascript:toggle_readonly();" ' . $is_checked . '/> <span>' . $this->descriptions['use_key_values'] . '</span>

			 	</div>';
        // OPTIONS
        $option_cnt = 0;
        if (isset($def['options'])) {
            $option_cnt = count($def['options']);
        }
        // using the parse function because this got too crazy with escaping single quotes
        $hash = array();
        $hash['option_cnt'] = $option_cnt;
        $hash['delete'] = __('Delete');
        $hash['options'] = __('Options', CCTM_TXTDOMAIN);
        $hash['values'] = __('Stored Values', CCTM_TXTDOMAIN);
        $hash['add_option'] = __('Add Option', CCTM_TXTDOMAIN);
        $hash['set_as_default'] = __('Set as Default', CCTM_TXTDOMAIN);
        $tpl = '
			<script type="text/javascript">
				jQuery(function() {
					jQuery( "#dropdown_options2" ).sortable();
					// jQuery( "#dropdown_options2" ).disableSelection();
				});			
			</script>
			<table id="dropdown_options">
				<thead>
				<td scope="col" id="sorter" class=""  style="">&nbsp;</td>	
				<td width="200"><label for="options" class="cctm_label cctm_select_label" id="cctm_label_options">[+options+]</label></td>
				<td width="200"><label for="options" class="cctm_label cctm_select_label" id="cctm_label_options">[+values+]</label></td>
				<td>
				 <span class="button" onclick="javascript:append_dropdown_option(\'dropdown_options\',\'[+delete+]\',\'[+set_as_default+]\',\'[+option_cnt+]\');">[+add_option+]</span>
				</td>
				</thead>
				<tbody id="dropdown_options2">';
        $out .= CCTM::parse($tpl, $hash);
        // this html should match up with the js html in dropdown.js
        $option_html = '
			<tr id="%s">
				<td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></td>
				<td><input type="text" name="options[]" id="option_%s" value="%s"/></td>
				<td><input type="text" name="values[]" id="value_%s" value="%s" class="possibly_gray"' . $readonly_str . '/></td>
				<td><span class="button" onclick="javascript:remove_html(\'%s\');">%s</span>
				<span class="button" onclick="javascript:set_as_default(\'%s\');">%s</span></td>
			</tr>';
        $opt_i = 0;
        // used to uniquely ID options.
        if (!empty($def['options']) && is_array($def['options'])) {
            $opt_cnt = count($def['options']);
            for ($i = 0; $i < $opt_cnt; $i++) {
                // just in case the array isn't set
                $option_txt = '';
                if (isset($def['options'][$i])) {
                    $option_txt = htmlspecialchars(trim($def['options'][$i]));
                }
                $value_txt = '';
                if (isset($def['values'][$i])) {
                    $value_txt = htmlspecialchars(trim($def['values'][$i]));
                }
                $option_css_id = 'cctm_dropdown_option' . $opt_i;
                $out .= sprintf($option_html, $option_css_id, $opt_i, $option_txt, $opt_i, $value_txt, $option_css_id, __('Delete'), $opt_i, __('Set as Default'));
                $opt_i = $opt_i + 1;
            }
        }
        $out .= '
			</tbody>
		</table>';
        // close id="dropdown_options"
        // Display as Radio Button or as Dropdown?
        $out .= '<div class="' . self::wrapper_css_class . '" id="display_type_wrapper">
				 <label class="cctm_label cctm_checkbox_label" id="display_type_label">' . __('How should the field display?', CCTM_TXTDOMAIN) . '</label>
				 <br />
				 <input type="radio" name="display_type" class="cctm_radio" id="display_type_dropdown" value="dropdown" ' . CCTM::is_radio_selected('dropdown', CCTM::get_value($this->props, 'display_type', 'dropdown')) . '/>
				 <label for="display_type_dropdown" class="cctm_label cctm_radio_label" id="display_type_dropdown_label">' . __('Dropdown', CCTM_TXTDOMAIN) . '</label><br />
				 <input type="radio" name="display_type" class="cctm_radio" id="display_type_radio" value="radio" ' . CCTM::is_radio_selected('radio', CCTM::get_value($this->props, 'display_type', 'dropdown')) . '/>
				 <label for="display_type_radio" class="cctm_label cctm_radio_label" id="display_type_radio_label">' . __('Radio Button', CCTM_TXTDOMAIN) . '</label><br />
			 	</div>';
        // Secondary Input options
        $out .= '</td><td style="vertical-align:top">
			<label class="cctm_label cctm_textarea_label" id="advanced_label">' . __('Alternate Input', CCTM_TXTDOMAIN) . '</label>
			<span>' . __('Use this input if you want to options in bulk. 
				Separate options and values using double-pipes "||" with the visible option on the left, the corresponding value
				to be stored on the right (if present).  You may also enter a valid MySQL query. This field overrides 
				other inputs!', CCTM_TXTDOMAIN) . '</span><br/>
			<textarea name="alternate_input" id="alternate_input" cols="50" rows="10">' . CCTM::get_value($def, 'alternate_input') . '</textarea>';
        // Execute as MySQL?
        $out .= '<div class="' . self::wrapper_css_class . '" id="is_sql_wrapper">
				<input type="hidden" name="is_sql" value="0"/>
				 <input type="checkbox" name="is_sql" class="cctm_checkbox" id="is_sql" value="1"' . $is_sql_checked . '/> 				 <label for="is_sql" class="cctm_label cctm_checkbox_label" id="is_sql_label">' . __('Execute as a MySQL query?', CCTM_TXTDOMAIN) . '</label> <span>' . __('Select up to 2 columns: the 1st column will be the visible label and the 2nd column (if present) will represent the value stored in the database.
				 	Use [+table_prefix+] instead of hard-coding your WordPress database table prefix.', CCTM_TXTDOMAIN) . '</span>
			 	</div>';
        $out .= '
					</td></tr></table>		
				</div><!-- /inside -->
			</div><!-- /postbox -->';
        // Validations / Required
        $out .= $this->format_validators($def, false);
        // Output Filter
        $out .= $this->format_available_output_filters($def);
        return $out;
    }