Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color picker support #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions controllers/dashboard/blocks/designer_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public function generate_block() { //In single_pages, do not prepend "action_" (
$block->add_url_field($field_labels[$id], $field_prefixes[$id], $field_suffixes[$id], !empty($fields_required[$id]), $field_url_targets[$id]);
} else if ($type == 'date') {
$block->add_date_field($field_labels[$id], $field_prefixes[$id], $field_suffixes[$id], !empty($fields_required[$id]), $field_date_formats[$id]);
} else if ($type == 'color') {
$block->add_color_field($field_labels[$id], $field_prefixes[$id], $field_suffixes[$id], !empty($fields_required[$id]));
} else if ($type == 'select') {
$block->add_select_field($field_labels[$id], $field_select_options[$id], !empty($fields_required[$id]), $field_select_show_headers[$id], $field_select_header_texts[$id]);
} else if ($type == 'wysiwyg') {
Expand Down
47 changes: 46 additions & 1 deletion libraries/block_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ public function add_date_field($label, $prefix = '', $suffix = '', $required = f
);
}

public function add_color_field($label, $prefix = '', $suffix = '', $required = false) {
$this->fields[] = array(
'num' => count($this->fields) + 1,
'type' => 'color',
'label' => $label,
'prefix' => $prefix,
'suffix' => $suffix,
'required' => $required
);
}

public function add_select_field($label, $options, $required, $show_header = false, $header_text = '') {
$this->fields[] = array(
'num' => count($this->fields) + 1,
Expand Down Expand Up @@ -237,6 +248,14 @@ private function generate_auto_js() {
$code .= "\t}\n\n";
}

if ($field['type'] == 'color' && $field['required']) {
$code .= "\tif (\$('input[name=field_{$field['num']}_color_value]').val() == '' || \$('input[name=field_{$field['num']}_color_value]').val() == 0) {\n";
$translated_error = $this->addslashes_single( t('Missing required color') );
$label = $this->addslashes_single($field['label']);
$code .= "\t\tccm_addError('{$translated_error}: {$field_label}');\n";
$code .= "\t}\n\n";
}

if ($field['type'] == 'select' && $field['required']) {
$code .= "\tif (\$('select[name=field_{$field['num']}_select_value]').val() == '' || \$('select[name=field_{$field['num']}_select_value]').val() == 0) {\n";
$translated_error = $this->addslashes_single( t('Missing required selection') );
Expand Down Expand Up @@ -323,6 +342,10 @@ private function generate_controller_php() {
$code .= "\t\t\$content[] = date('{$field['format']}', \$this->field_{$field['num']}_date_value);\n";
$fieldcount++;
}
if ($field['type'] == 'color') {
$code .= "\t\t\$content[] = \$this->field_{$field['num']}_color_value;\n";
$fieldcount++;
}
if ($field['type'] == 'wysiwyg') {
$code .= "\t\t\$content[] = \$this->field_{$field['num']}_wysiwyg_content;\n";
$fieldcount++;
Expand Down Expand Up @@ -470,6 +493,9 @@ private function generate_db_xml() {
if ($field['type'] == 'date') {
$code .= "\t\t<field name=\"field_{$field['num']}_date_value\" type=\"D\"></field>\n\n";
}
if ($field['type'] == 'color') {
$code .= "\t\t<field name=\"field_{$field['num']}_color_value\" type=\"X\"></field>\n\n";
}
if ($field['type'] == 'select') {
$code .= "\t\t<field name=\"field_{$field['num']}_select_value\" type=\"I\"><default value=\"0\" /></field>\n\n";
}
Expand Down Expand Up @@ -605,6 +631,14 @@ private function generate_edit_php() {
$include_date_time = true;
}

if ($field['type'] == 'color') {
$code .= "<div class=\"ccm-block-field-group\">\n";
$code .= "\t<h2>{$field['label']}</h2>\n";
$code .= "\t<?php echo \$fh->output('field_{$field['num']}_color_value', '', \$field_{$field['num']}_color_value); ?>\n";
$code .= "</div>\n\n";
$include_color = true;
}

if ($field['type'] == 'select') {
$code .= "<div class=\"ccm-block-field-group\">\n";
$code .= "\t<h2>{$field['label']}</h2>\n";
Expand Down Expand Up @@ -641,7 +675,8 @@ private function generate_edit_php() {
$code = '';
$code .= $include_asset_library ? "\$al = Loader::helper('concrete/asset_library');\n" : '';
$code .= $include_page_selector ? "\$ps = Loader::helper('form/page_selector');\n" : '';
$code .= $include_date_time ? "\$dt = Loader::helper('form/date_time');\n" : '';
$code .= $include_date_time ? "\$dt = Loader::helper('form/date_time');\n" : '';;
$code .= $include_color ? "\$fh = Package::getByHandle('jl_spectrum_color_picker') ? Loader::helper('spectrum_color', 'jl_spectrum_color_picker') : Loader::helper('form/color');\n" : '';
$code .= $include_editor_config ? "Loader::element('editor_config');\n" : '';
$token = '[[[GENERATOR_REPLACE_HELPERLOADERS]]]';
$template = str_replace($token, $code, $template);
Expand Down Expand Up @@ -764,6 +799,16 @@ private function generate_view_php() {
$code .= "<?php endif; ?>\n\n";
}

if ($field['type'] == 'color') {
$code .= "<?php if (!empty(\$field_{$field['num']}_color_value)): ?>\n";
$code .= "\t";
$code .= empty($field['prefix']) ? '' : $field['prefix'];
$code .= "<?php echo \$field_{$field['num']}_color_value; ?>";
$code .= empty($field['suffix']) ? '' : $field['suffix'];
$code .= "\n";
$code .= "<?php endif; ?>\n\n";
}

if ($field['type'] == 'select') {
$i = 1;
foreach ($field['options'] as $option) {
Expand Down
3 changes: 3 additions & 0 deletions single_pages/dashboard/blocks/designer_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ function s2nb($text) {
&nbsp;&nbsp;
[<a href="#" class="add-field-type" data-type="date"><?php echo s2nb(t('Date Picker')); ?></a>]
&nbsp;&nbsp;
[<a href="#" class="add-field-type" data-type="color"><?php echo s2nb(t('Color Picker')); ?></a>]
&nbsp;&nbsp;
[<a href="#" class="add-field-type" data-type="select"><?php echo s2nb(t('Dropdown List')); ?></a>]
&nbsp;&nbsp;
[<a href="#" class="add-field-type" data-type="wysiwyg"><?php echo s2nb(t('WYSIWYG Editor')); ?></a>]
Expand Down Expand Up @@ -289,6 +291,7 @@ function s2nb($text) {
'link': '<?php echo t("Page Link Field"); ?>',
'url': '<?php echo t("External URL Field"); ?>',
'date': '<?php echo t("Date Picker Field"); ?>',
'color': '<?php echo t("Color Picker Field"); ?>',
'select': '<?php echo t("Dropdown List"); ?>',
'wysiwyg': '<?php echo t("WYSIWYG Editor"); ?>'
};
Expand Down