Skip to content

Instantly share code, notes, and snippets.

@petenelson
Last active March 25, 2018 21:51
Show Gist options
  • Save petenelson/4679482 to your computer and use it in GitHub Desktop.
Save petenelson/4679482 to your computer and use it in GitHub Desktop.
WordPress: Expands upon the Custom Meta Boxes library to provide a user-facing input form, list and detail page of submitted forms for custom post types.
<?php
/*
Author: Pete Nelson @GunGeekATX
Expands upon the Custom Meta Boxes library to provide a user-facing input form,
list & detail page of custom post types. This code sample doesn't include all the libraries
such as jQuery tablesorter or validate but can be found elsewhere easily enough.
*/
class GGA_Marketing_Request {
public static $version = '2013-01-30-01';
public static $post_type = 'gga-mktg-request';
public static $meta_prefix = '_gga_mktg_req_';
public static $css_prefix = 'ggaMktgForm';
public static $options = 'gga-marketing-request';
public static $type_new_revised = 'new-revised';
public static $type_event_promo = 'event-promo';
public static $type_print_reorder = 'print-reorder';
public static $status_open = 'Open';
public static $status_completed = 'Completed';
public static $status_hold = 'Hold';
public function register_actions_and_filters() {
add_action('init', array(&$this, 'init'));
add_action('wp_ajax_gga_mkt_request', array(&$this, 'process_mkt_request_form'));
add_action('wp_ajax_nopriv_gga_mkt_request', array(&$this, 'process_mkt_request_form'));
add_filter( 'cmb_meta_boxes', array(&$this, 'define_custom_meta_boxes'));
if (is_admin()) {
add_action( 'admin_menu', array(&$this, 'admin_menu' ));
add_action( 'admin_init', array(&$this, 'admin_register_settings') );
add_filter( 'wp_insert_post_data', array(&$this, 'wp_insert_post_data_handler'), 99, 2 );
add_action( 'admin_head-post.php', array(&$this, 'admin_head_post_check' ));
add_action( 'admin_head-post-new.php', array(&$this, 'admin_head_post_check' ));
add_action( 'admin_head-edit.php', array(&$this, 'admin_head_post_check' ));
}
}
function init() {
// if you do not have Custom Meta Boxes library installed, you can grab it from here
// https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
if ( ! class_exists( 'cmb_Meta_Box' ) )
require_once 'metabox/init.php';
$this->register_custom_post_type();
$this->add_shortcodes();
}
function add_shortcodes() {
add_shortcode('gga-marketing-request-list', array(&$this, 'request_list'));
add_shortcode('gga-marketing-request-form', array(&$this, 'request_form'));
}
function admin_head_post_check() {
global $post_type;
if ($post_type === GGA_Marketing_Request::$post_type) {
if (!$this->current_user_is_in_roles())
wp_die( __('You do not have access to this function.') );
}
}
function process_mkt_request_form() {
// handles the AJAX form post when the user submits the marketing request form
$nonce=$_REQUEST['_wpnonce'];
if (! wp_verify_nonce($nonce, GGA_Marketing_Request::$post_type) ) die('Security check');
// required for wp_generate_attachment_metadata
require_once(ABSPATH . 'wp-admin/includes/image.php');
$form_type = $_REQUEST['form_type'];
$prefix = GGA_Marketing_Request::$meta_prefix;
$project_name = trim($_REQUEST[$prefix . 'project_name']);
$slug = wp_unique_post_slug(
sanitize_title_with_dashes($project_name),
0,
'publish',
GGA_Marketing_Request::$post_type,
0);
$post_data = array(
'post_title' => htmlspecialchars($project_name),
'post_content' => '',
'post_status' => 'publish',
'post_name' => $slug,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_type' => GGA_Marketing_Request::$post_type,
);
// create the post
// TODO add some error handling here
$post_id = wp_insert_post($post_data);
$fields = array();
$boxes = array();
// build a list of fields that we expect to see from the form post
$sections = $this->create_sections_for_type($form_type);
$boxes = $this->define_custom_meta_boxes($boxes);
foreach ($sections as $section) {
$fieldForSection = $this->get_fields_for_section($boxes, $section);
foreach($fieldForSection as $field)
$fields[] = $field;
}
add_post_meta($post_id, $prefix . 'type' , $form_type);
add_post_meta($post_id, $prefix . 'status' , GGA_Marketing_Request::$status_open);
$time = 0;
if (isset($_REQUEST[$prefix . 'estimated_completion']))
$time = strtotime($_REQUEST[$prefix . 'estimated_completion']);
else
$time = 4102358400; // 12/31/2099
add_post_meta($post_id, $prefix . 'estimated_completion_time', $time);
$messages = array();
// now that we have a list of fields that we expect from the form, add them to the post's meta
foreach ($fields as $field) {
$fieldType = $field['type'];
$fieldId = $field['id'];
if ($fieldType === 'file') {
$messages[] = 'Processing ' . $fieldId;
if (isset($_FILES[$fieldId])) {
$uploadedFile = wp_handle_upload($_FILES[$fieldId], array( 'test_form' => false));
if (isset($uploadedFile['file']))
{
$filename = $uploadedFile['file'];
$messages[] = 'Filename for ' . $fieldId . ' is ' . $filename;
$wp_filetype = wp_check_filetype(basename($filename), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $filename ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
$messages[] = 'attach_id = ' . $attach_id;
}
}
}
else if ($fieldType === 'multicheck') {
if (isset($_REQUEST[$fieldId])) {
foreach($_REQUEST[$fieldId] as $value)
add_post_meta($post_id, $fieldId, $value);
}
}
else
add_post_meta($post_id, $fieldId, htmlspecialchars($_REQUEST[$fieldId])); // should be any text or textarea field
}
//header('Content-Type: application/json');
$this->email_new_request_form_to_roles($post_id);
$success = true;
echo json_encode(
array(
'post_id' => $post_id,
'messages' => $messages,
'success' => $success,
'permalink' => get_permalink($post_id)
)
);
do_action( GGA_Marketing_Request::$post_type . '_post_saved', $post_id );
die();
}
function wp_insert_post_data_handler($data, $postarr) {
// fired when a post/page is updated
$post_type = $data['post_type'];
$post_status = $data['post_status'];
$post_id = $postarr['ID'];
$prefix = GGA_Marketing_Request::$meta_prefix;
if ($post_type === GGA_Marketing_Request::$post_type) {
if (isset($postarr[$prefix . 'project_name']))
$data['post_title'] = $postarr[$prefix . 'project_name'];
if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' )))
$data['post_name'] = wp_unique_post_slug(
sanitize_title_with_dashes( $data['post_title'] ),
$post_id,
$post_status,
$post_type,
0);
}
// send notification e-mail
if ($post_type === GGA_Marketing_Request::$post_type && $post_id !== 0 && $post_status === 'publish') {
// we only want to handle this if:
// 1) it is a marketing request form
// 2) we're updating an existing one
// 3) we're changing a value that triggers a notification
$meta = get_post_meta($post_id);
$current_status = $this->get_value_from_post_meta($meta, 'status');
$current_estimated_completion = $this->get_value_from_post_meta($meta, 'estimated_completion');
$current_assigned_to = $this->get_value_from_post_meta($meta, 'assigned_to');
$email_to = $this->get_value_from_post_meta($meta, 'email');
$new_status = '';
$new_estimated_completion = '';
$new_assigned_to = '';
if (isset($postarr[$prefix . 'status']))
$new_status = $postarr[$prefix . 'status'];
if (isset($postarr[$prefix . 'estimated_completion'])) {
$new_estimated_completion = $postarr[$prefix . 'estimated_completion'];
// update the estimated_completion_time field, used for sorting
$time = strtotime($postarr[$prefix . 'estimated_completion']);
add_post_meta($post_id, $prefix . 'estimated_completion_time', $time, true) or update_post_meta($post_id, $prefix . 'estimated_completion_time', $time);
}
if (isset($postarr[$prefix . 'assigned_to']))
$new_assigned_to = $postarr[$prefix . 'assigned_to'];
if (!is_array($current_assigned_to))
$current_assigned_to = array();
if ($this->notifications_enabled() && (
$current_status !== $new_status ||
$current_estimated_completion !== $new_estimated_completion ||
count(array_diff($current_assigned_to, $new_assigned_to) > 0))
) {
$email_to = filter_var($email_to, FILTER_SANITIZE_EMAIL);
if (isset($email_to) && !empty($email_to)) {
$options = get_option(GGA_Marketing_Request::$options);
$send_assigned_email = false;
$type = $this->get_value_from_post_meta($meta, 'type');
$content = get_permalink($post_id);
$content .= "<br/><br/>";
if ($current_status !== $new_status)
$content .= "Status changed to: {$new_status}<br/>";
if ($current_estimated_completion !== $new_estimated_completion)
$content .= "Estimated Completion changed to: {$new_estimated_completion}<br/>";
if (count(array_diff($current_assigned_to, $new_assigned_to) > 0)) {
$send_assigned_email = true;
$assigned_to_names = '';
foreach($new_assigned_to as $user_id) {
if ($user_id !== '0') {
if ($assigned_to_names !== '')
$assigned_to_names .= ', ';
$assigned_to_names .= get_userdata(intval($user_id))->display_name;
}
}
$content .= "Assigned To changed to: {$assigned_to_names}<br/>";
}
$headers = array();
if (isset($options['bcc-email']) && !empty($options['bcc-email']))
$headers[] = 'Bcc: ' . $options['bcc-email'];
// Nov 18 2012 PN
// Removed code that sends e-mail to the users that the request is assigned to
// if ($send_assigned_email) {
// foreach($new_assigned_to as $user_id) {
// $user = get_userdata($user_id);
// $email = filter_var($user->user_email, FILTER_SANITIZE_EMAIL);
// if (isset($email) && !empty($email))
// $headers[] = 'Bcc: ' . $email;
// }
// }
if (isset($options['from-email']) && !empty($options['from-email']))
$headers[] = "From: " . $options['from-email'];
$subject = 'Updated Markting Request: ' . html_entity_decode($this->get_value_from_post_meta($meta, 'project_name'));
if ($this->notifications_enabled()) {
add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
$mail_status = wp_mail($email_to, $subject, $content, $headers);
}
}
}
}
if ($post_id !== 0)
do_action( GGA_Marketing_Request::$post_type . '_post_saved', $post_id );
return $data;
}
function email_new_request_form_to_roles($post_id) {
if (!$this->notifications_enabled())
return true;
$options = get_option(GGA_Marketing_Request::$options);
$post = get_post($post_id);
$meta = get_post_meta($post_id);
$prefix = GGA_Marketing_Request::$meta_prefix;
$type = $this->get_value_from_post_meta($meta, 'type');
$content = get_permalink($post_id);
$content .= "<br/><br/>";
$content .= "Project Name: " . $this->get_value_from_post_meta($meta, 'project_name') . "<br/>";
$content .= "Due Date: " . $this->get_value_from_post_meta($meta, 'due_date') . "<br/>";
$content .= "Contact Name: " . $this->get_value_from_post_meta($meta, 'your_name') . "<br/>";
$content .= "Contact E-Mail: " . $this->get_value_from_post_meta($meta, 'email') . "<br/>";
$headers = array();
if (isset($options['bcc-email']) && !empty($options['bcc-email']))
$headers[] = 'Bcc:' . $options['bcc-email'];
if (isset($options['from-email']) && !empty($options['from-email']))
$headers[] = "From:" . $options['from-email'];
$emergency = $this->get_value_from_post_meta($meta, 'emergency_request');
$emergency = (isset($emergency) && $emergency == 'Emergency Request');
$emergency_text = '';
if ($emergency) {
$headers[] = "Importance: high";
$emergency_text = '[Emergency] ';
}
$subject = $emergency_text . 'New Marketing Request: ' . html_entity_decode($this->get_value_from_post_meta($meta, 'project_name'));
$to = array();
if (isset($options['role-' . $type])) {
foreach($options['role-' . $type] as $user_id) {
$user = get_userdata($user_id);
$email = filter_var($user->user_email, FILTER_SANITIZE_EMAIL);
if (isset($email) && !empty($email))
$to[] = $email;
}
}
if (count($to) > 0) {
add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
wp_mail($to, $subject, $content, $headers);
// email form to submitter
$from_email = $this->get_value_from_post_meta($meta, 'email');
if (isset($from_email))
$from_email = filter_var($from_email, FILTER_SANITIZE_EMAIL);
if (!empty($from_email))
wp_mail($from_email, $subject, $content, $headers);
}
}
function create_sections_for_type($type, $include_status = false)
{
$prefix = GGA_Marketing_Request::$meta_prefix;
$sections = array();
if ($include_status)
$sections[] = array(
'id' => 'request_information',
'name' => 'Request Information and Status',
'cssClass' => 'statusInformation',
'fields' => array(
$prefix . 'status',
$prefix . 'assigned_to_names',
$prefix . 'estimated_completion',
),
);
// contact is the same for all forms
$sections[] = array(
'id' => 'contact_information',
'name' => 'Contact Information',
'cssClass' => 'contactInformation',
'fields' => array(
$prefix . 'your_name',
$prefix . 'email',
$prefix . 'phone',
$prefix . 'market',
$prefix . 'channel',
),
);
if ($type == GGA_Marketing_Request::$type_new_revised) {
$sections[] = array(
'id' => 'project_information',
'name' => 'Project Information',
'cssClass' => 'projectInformation',
'fields' => array(
$prefix . 'project_name',
$prefix . 'quantity',
$prefix . 'budget',
$prefix . 'accounting_code',
$prefix . 'project_description',
$prefix . 'copy_needs',
$prefix . 'any_specs',
$prefix . 'any_additional_details',
$prefix . 'file_1',
$prefix . 'file_2',
$prefix . 'file_3',
$prefix . 'delivery_address',
$prefix . 'due_date',
$prefix . 'emergency_request',
),
);
}
if ($type == 'event-promo') {
$sections[] = array(
'id' => 'request_approval',
'name' => 'Request Approval',
'cssClass' => 'requestApproval',
'fields' => array(
$prefix . 'approval_name',
$prefix . 'approval_email',
$prefix . 'approval_phone',
),
);
$sections[] = array(
'id' => 'project_information',
'name' => 'Product Information',
'cssClass' => 'projectInformation',
'fields' => array(
$prefix . 'project_name',
$prefix . 'quantity',
$prefix . 'budget',
$prefix . 'accounting_code',
$prefix . 'delivery_address',
$prefix . 'due_date',
$prefix . 'emergency_request',
),
);
}
if ($type == 'print-reorder') {
$sections[] = array(
'id' => 'request_approval',
'name' => 'Proof Approval Contact',
'cssClass' => 'requestApproval',
'fields' => array(
$prefix . 'approval_name',
$prefix . 'approval_email',
$prefix . 'approval_phone',
),
);
$sections[] = array(
'id' => 'project_information',
'name' => 'Project Information',
'cssClass' => 'projectInformation',
'fields' => array(
$prefix . 'project_name',
$prefix . 'file_1',
$prefix . 'quantity',
$prefix . 'budget',
$prefix . 'accounting_code',
$prefix . 'delivery_address',
$prefix . 'due_date',
$prefix . 'emergency_request',
),
);
}
return $sections;
}
function request_form($atts, $content)
{
// builds the HTML for the form to be filled out by the user
extract(shortcode_atts(array(
'type' => GGA_Marketing_Request::$type_new_revised,
'content_after' => '',
), $atts));
// ajax the form
wp_enqueue_script('jquery-form');
wp_enqueue_script('jquery-ui-datepicker');
// validate the form
$this->enqueue_jquery_validate();
// format the form
wp_enqueue_style( 'gga-mktg-request', get_bloginfo('template_url') . '/gga-marketing-request/style.css', null, GGA_Marketing_Request::$version );
wp_enqueue_style( 'gga-mktg-request-jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css');
$boxes = $this->define_custom_meta_boxes(array());
$sections = $this->create_sections_for_type($type);
$html = '<div class="' . GGA_Marketing_Request::$css_prefix . '"><form id="mkt-request-form" method="post" action="' . admin_url('admin-ajax.php') . '">';
$html .= '<input type="hidden" name="action" value="gga_mkt_request" />';
$html .= '<input type="hidden" name="form_type" value="'. $type . '" />';
$html .= wp_nonce_field( GGA_Marketing_Request::$post_type);
//$html .= '<pre>' . var_export($sections, true) . '</pre>';
foreach($sections as $section)
{
$sectionId = $section['id'];
$html .= '<fieldset class="' . $section['cssClass'] . '">';
$html .= '<legend>' . $section['name'] . '</legend>';
$fields = $this->get_fields_for_section($boxes, $section);
foreach($fields as $field) {
$required = (isset($field['required']) && $field['required']) || (isset($field['required-' . $type]) && $field['required-' . $type]) === true;
if ($required)
$html .= '<span class="required">*</span>';
$html .= '<span class="label">';
if (isset($field['prompt-' . $type ]))
$html .= $field['prompt-' . $type ];
else
$html .= htmlspecialchars($field['name']) . ':';
if (isset($field['desc']) && !empty($field['desc']))
$html .= ' <span class="smaller">' . $field['desc'] . '</span>';
if (isset($field['desc-' . $type]) && !empty($field['desc-' . $type]))
$html .= ' <span class="smaller">' . $field['desc-' . $type] . '</span>';
$html .= '</span>';
$html .= $this->generate_form_input($field);
$html .= '<br/>';
if (isset($field['desc-bottom']) && !empty($field['desc-bottom']))
$html .= ' <em ' . (isset($field['desc-bottom-class']) ? 'class="' . $field['desc-bottom-class'] . '"' : '') . '>' . $field['desc-bottom'] . '</em>';
if (isset($content_after) && $field['id'] == (GGA_Marketing_Request::$meta_prefix . $content_after) && !empty($content))
$html .= $content;
}
$html .= '</fieldset>';
}
// there is some custom date validation in here that can be removed
$minDate = time() + (60 * 60 * 24 * 20);
$html .= '<input type="submit" value="Send" id="formSubmitButton" />
<img src="' . get_bloginfo('template_url') . '/gga-marketing-request/ajax-loader.gif" class="ajax-loader" style="display: none" />
</form>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery.validator.addMethod(\'mindate\',function(v,el,minDate){
if (this.optional(el)){
return true;
}
var curDate = jQuery(el).datepicker(\'getDate\');
return minDate <= curDate;
}, \'Date must be at least three weeeks in the future\');
jQuery("#mkt-request-form").validate({
rules: {
_gga_mktg_req_due_date: {
mindate: new Date(\'' . date('F', $minDate) . ' ' . date('d', $minDate) . ', ' . date('Y', $minDate) . '\'),
required: true
}
}
});
jQuery(\'.gga_text_date\').datepicker({
minDate: (\'+3W\')
});
jQuery(\'#mkt-request-form\').ajaxForm({
beforeSubmit: mktBeforeSubmit,
success: mktShowResponse,
dataType: "json"
});
});
function mktBeforeSubmit(formData, jqForm, options) {
if (!jQuery("#mkt-request-form").valid())
return false;
if (!jQuery("#formSubmitButton").is(":visible"))
return false;
jQuery("#formSubmitButton").hide();
jQuery("img.ajax-loader").show();
return true;
}
function mktShowResponse(response, statusText, xhr, $form) {
if (!response || !response.success) {
alert("We were unable to process your request at this time.");
}
else {
jQuery(\'#mkt-request-form\').clearForm();
jQuery("#formSubmitButton").show();
jQuery("img.ajax-loader").hide();
alert("Thank you! Your request has been received.");
document.location = response.permalink;
}
}
</script>
</div><!-- .' . GGA_Marketing_Request::$css_prefix . ' -->
';
return $html;
}
function get_fields_for_section($boxes, $section)
{
$fields = array();
$fieldIdsForSection = $section['fields'];
foreach($fieldIdsForSection as $field_id) {
foreach($boxes as $box) {
if ($box['id'] === $section['id']) {
foreach($box['fields'] as $field) {
if ($field['id'] === $field_id)
$fields[] = $field;
}
}
}
}
return $fields;
}
function generate_form_input($field)
{
// builds a form input for a specific field
$required = isset($field['required']) && $field['required'] === true;
$cssPrefix = GGA_Marketing_Request::$css_prefix;
$cssClass = $required ? 'required' : '';
$fieldType = $field['type'];
$fieldId = $field['id'];
$cssClass .= ' ' . $fieldId . ' gga_' . $fieldType;
if (isset($field['validateCssClass']) && !empty($field['validateCssClass']))
$cssClass .= ' ' . $field['validateCssClass'];
$html = '<span class="' . $cssPrefix . ' -form-control-wrap">';
switch ($fieldType)
{
case 'text':
case 'text_small':
case 'text_medium':
case 'text_date':
if ($fieldType === 'text_date')
$cssClass .= ' datepicker ';
$html .= '<input type="text" id="' . $fieldId . '" name="' . $fieldId . '" class="' . $cssClass . '" value="" />';
break;
case 'multicheck':
$html .= '<br/><span class="' . $cssPrefix . '-checkbox">';
foreach($field['options'] as $key => $value) {
$html .= '<span class="' . $cssPrefix . '-list-item">';
$html .= '<label><input type="checkbox" class="' . $cssClass . '" name="' . $fieldId . '[]" value="' . htmlspecialchars($key) . '">&nbsp;<span class="' . $cssPrefix . '-list-item-label">' . htmlspecialchars($value) . '</span></label>';
$html .= '</span>';
}
$html .= '</span><br/>';
break;
case 'select':
$html .= '<select id="' . $fieldId . '" name="' . $fieldId . '" class="' . $cssClass . '" value="" />';
foreach($field['options'] as $option) {
$html .= '<option value="' . $option['name'] . '">' . $option['value'] . '</option>';
}
$html .= '</select>';
break;
case 'textarea':
$html .= '<textarea id="' . $fieldId . '" name="' . $fieldId . '" class="' . $cssClass . '" value="" cols="80" rows="6"></textarea><br/>';
break;
case 'file':
$html .= '<input type="file" id="' . $fieldId . '" name="' . $fieldId . '" class="' . $cssClass . '" value="1" />';
break;
}
$html .= '</span>';
return $html;
}
function register_custom_post_type()
{
$type_name_s = 'Marketing Request';
$type_name_p = 'Marketing Requests';
$args = array(
'public' => true,
'labels' => array(
'name' => $type_name_p,
'singular_name' => $type_name_s,
'add_new_item' => 'Add New '. $type_name_s,
'edit_item' => 'Edit ' . $type_name_s,
'new_item' => 'New ' . $type_name_s,
'new_item' => 'View ' . $type_name_s,
'search_items' => 'Search ' . $type_name_p,
'not_found' => 'No ' . $type_name_p . ' found',
'not_found_in_trash' => 'No ' . $type_name_p . ' found in Trash'
),
'description' => 'Marketing requests',
'exclude_from_search' => true,
'show_in_menu' => $this->current_user_is_in_roles(),
'capability_type' => 'post',
'supports' => array(
'comments'
),
'has_archive' => true
);
register_post_type(GGA_Marketing_Request::$post_type, $args);
}
function define_custom_meta_boxes(array $meta_boxes, $include_assigned_to_names = false)
{
$prefix = GGA_Marketing_Request::$meta_prefix;
$assigned_to_names = array(
'name' => 'Assigned To',
'id' => $prefix . 'assigned_to_names',
'type' => 'text',
);
$meta_boxes[] = array(
'id' => 'request_information',
'title' => 'Request Information and Status',
'pages' => array( GGA_Marketing_Request::$post_type, ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'Request Type',
'id' => $prefix . 'type',
'type' => 'select',
'options' => array(
array( 'name' => 'New or Revised Project', 'value' => GGA_Marketing_Request::$type_new_revised, ),
array( 'name' => 'Event Promo Item and/or Equipment', 'value' => GGA_Marketing_Request::$type_event_promo, ),
array( 'name' => 'Print Re-order', 'value' => GGA_Marketing_Request::$type_print_reorder, )
)
),
array(
'name' => 'Status',
'id' => $prefix . 'status',
'type' => 'select',
'options' => array(
//array( 'name' => 'New', 'value' => GGA_Marketing_Request::$status_new, ),
array( 'name' => 'Open', 'value' => GGA_Marketing_Request::$status_open, ),
array( 'name' => 'Completed', 'value' => GGA_Marketing_Request::$status_completed, ),
array( 'name' => 'Hold', 'value' => GGA_Marketing_Request::$status_hold, )
)
),
array(
'name' => 'Assigned To',
'id' => $prefix . 'assigned_to',
'type' => 'multicheck',
'options' => $this->role_user_list(true),
),
array(
'name' => 'Estimated Completion',
'id' => $prefix . 'estimated_completion',
'type' => 'text_date',
),
)
);
if ($include_assigned_to_names)
$meta_boxes[0]['fields'][] = $assigned_to_names;
$meta_boxes[] = array(
'id' => 'contact_information',
'title' => 'Contact Information',
'pages' => array( GGA_Marketing_Request::$post_type, ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'Your Name',
'id' => $prefix . 'your_name',
'type' => 'text_medium',
'required' => true,
),
array(
'name' => 'E-Mail',
'id' => $prefix . 'email',
'type' => 'text',
'required' => true,
'validateCssClass' => 'email',
),
array(
'name' => 'Phone',
'id' => $prefix . 'phone',
'type' => 'text_medium',
'required' => true,
),
array(
'name' => 'Market',
'id' => $prefix . 'market',
'desc' => '(you may check more than one if applicable)',
'type' => 'multicheck',
'required' => true,
'options' => array(
'Dallas-Fort Worth/Central Texas' => 'Dallas-Fort Worth/Central Texas',
'Austin' => 'Austin',
'Houston' => 'Houston',
'South Texas' => 'South Texas',
'New York City' => 'New York City',
'Westchester/O&R' => 'Westchester/O&R',
'New Jersey' => 'New Jersey',
'Pennsylvania' => 'Pennsylvania',
'Oregon' => 'Oregon',
'Chicago' => 'Chicago',
'Baltimore' => 'Baltimore',
),
),
array(
'name' => 'Channel',
'id' => $prefix . 'channel',
'desc' => '(you may check more than one if applicable)',
'type' => 'multicheck',
'required' => true,
'options' => array(
'Direct Sales/RTLE' => 'Direct Sales/RTLE',
'ACP' => 'ACP',
'Door-to-Door' => 'Door-to-Door',
'Web' => 'Web',
'GMEN' => 'GMEN',
'Residential Broker' => 'Residential Broker',
'Vendor' => 'Vendor',
'Commercial/Solutions' => 'Commercial/Solutions',
'Referral' => 'Referral',
'Solar' => 'Solar',
'Recruitment/Hiring/HR' => 'Recruitment/Hiring/HR',
'Other' => 'Other'
),
),
),
);
$meta_boxes[] = array(
'id' => 'project_information',
'title' => 'Project Information',
'pages' => array( GGA_Marketing_Request::$post_type, ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'Project Name',
'id' => $prefix . 'project_name',
'prompt-' . GGA_Marketing_Request::$type_print_reorder => 'Print Material Description:',
'prompt-' . GGA_Marketing_Request::$type_event_promo => 'Item Description:',
'type' => 'text',
'required' => true,
),
array(
'name' => 'Project Description',
'id' => $prefix . 'project_description',
'prompt-' . GGA_Marketing_Request::$type_new_revised => 'Project Description (please be as detailed as possible):',
'type' => 'textarea',
'required' => true,
),
array(
'name' => 'Quantity',
'id' => $prefix . 'quantity',
'prompt-' . GGA_Marketing_Request::$type_new_revised => 'Quantity Needed (if materials):',
'prompt-' . GGA_Marketing_Request::$type_print_reorder => 'Quantity Needed (if materials):',
'prompt-' . GGA_Marketing_Request::$type_event_promo => 'Quantity Needed:',
'type' => 'text_small',
'required' => true,
),
array(
'name' => 'Budget',
'id' => $prefix . 'budget',
'prompt-' . GGA_Marketing_Request::$type_new_revised => 'Budget for Project:',
'prompt-' . GGA_Marketing_Request::$type_print_reorder => 'Budget for Printing:',
'prompt-' . GGA_Marketing_Request::$type_event_promo => 'Budget for Promo Item or Equipment:',
'required-' . GGA_Marketing_Request::$type_event_promo => true,
'required-' . GGA_Marketing_Request::$type_print_reorder => true,
'desc-bottom' => 'Please confirm your budget prior to submitting a request<br/>',
'desc-bottom-class' => 'budget',
'type' => 'text_small',
),
array(
'name' => 'Accounting Code',
'id' => $prefix . 'accounting_code',
'prompt-' . GGA_Marketing_Request::$type_new_revised => 'Accounting Code (if applicable):',
'type' => 'text_small',
),
array(
'name' => 'Due Date',
'id' => $prefix . 'due_date',
'prompt-' . GGA_Marketing_Request::$type_new_revised => 'Preferred Due Date:',
'prompt-' . GGA_Marketing_Request::$type_print_reorder => 'Preferred Due Date:',
'prompt-' . GGA_Marketing_Request::$type_event_promo => 'Preferred Delivery Date:',
'desc-' . GGA_Marketing_Request::$type_new_revised => 'We require 3 weeks lead time on any new requests',
'type' => 'text_date',
),
array(
'name' => 'Copy Needs',
'id' => $prefix . 'copy_needs',
'type' => 'select',
'required' => true,
'options' => array(
array( 'name' => 'New copy needed', 'value' => 'New copy needed', ),
array( 'name' => 'Copy drafted, needs review', 'value' => 'Copy drafted, needs review', ),
array( 'name' => 'Copy already submitted to ICRIE and approved (Please attach copy doc below)', 'value' => 'Copy already submitted to ICRIE and approved (Please attach copy doc below)', )
)
),
array(
'name' => 'Any Specs or delivery instructions (i.e. print ad size, character limit, file type, etc.)',
'id' => $prefix . 'any_specs',
'type' => 'textarea',
),
array(
'name' => 'Any additional details or research already scoped',
'id' => $prefix . 'any_additional_details',
'type' => 'textarea',
),
array(
'name' => 'Supporting Document 1',
'prompt-' . GGA_Marketing_Request::$type_new_revised => 'Please Attach Any Supporting Documents:<br/>',
'prompt-' . GGA_Marketing_Request::$type_print_reorder => 'Attach original design (if available):<br/>',
'id' => $prefix . 'file_1',
'type' => 'file',
),
array(
'name' => 'Supporting Document 2',
'prompt-' . GGA_Marketing_Request::$type_new_revised => '',
'id' => $prefix . 'file_2',
'type' => 'file',
),
array(
'name' => 'Supporting Document 3',
'prompt-' . GGA_Marketing_Request::$type_new_revised => '',
'id' => $prefix . 'file_3',
'type' => 'file',
),
array(
'name' => 'Delivery Address',
'id' => $prefix . 'delivery_address',
'desc' => '(Please include name, address, and phone number for each location):',
'type' => 'textarea',
),
array(
'name' => 'Emergency Request',
'id' => $prefix . 'emergency_request',
'prompt-' . GGA_Marketing_Request::$type_new_revised => '',
'desc-' . GGA_Marketing_Request::$type_new_revised => 'Please check this box if the project is urgent and requires delivery in 1 week or less.',
'type' => 'multicheck',
'options' => array(
'Emergency Request' => 'Emergency Request',
),
),
),
);
$meta_boxes[] = array(
'id' => 'request_approval',
'title' => 'Request Approval',
'pages' => array( GGA_Marketing_Request::$post_type, ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'Your Name',
'id' => $prefix . 'approval_name',
'prompt-' . GGA_Marketing_Request::$type_print_reorder => 'Approver Name:',
'prompt-' . GGA_Marketing_Request::$type_event_promo => 'Approver Name:',
'type' => 'text',
),
array(
'name' => 'E-Mail',
'id' => $prefix . 'approval_email',
'type' => 'text',
'prompt-' . GGA_Marketing_Request::$type_print_reorder => 'Approver E-Mail:',
'prompt-' . GGA_Marketing_Request::$type_event_promo => 'Approver E-Mail:',
'validateCssClass' => 'email',
),
array(
'name' => 'Phone',
'id' => $prefix . 'approval_phone',
'prompt-' . GGA_Marketing_Request::$type_print_reorder => 'Approver Phone:',
'prompt-' . GGA_Marketing_Request::$type_event_promo => 'Approver Phone:',
'type' => 'text',
),
)
);
return $meta_boxes;
}
public function role_user_list($for_multicheck = false) {
$userlist = array();
$userargs = array(
'blog_id' => $GLOBALS['blog_id'],
'role' => '',
'meta_key' => '',
'meta_value' => '',
'meta_compare' => '',
'meta_query' => array(),
'include' => array(),
'exclude' => array(),
'orderby' => 'display_name',
'order' => 'ASC',
'offset' => '',
'search' => '',
'number' => '',
'count_total' => false,
'fields' => 'all',
'who' => ''
);
$users = get_users($userargs);
$user_ids = $this->user_ids_assigned_to_roles();
foreach($users as $user) {
if (in_array($user->ID, $user_ids))
$userlist[] = $user;
}
if ($for_multicheck) {
$list = array();
foreach($userlist as $user) {
$list[$user->ID] = $user->display_name;
}
return $list;
} else {
return $userlist;
}
}
function query_request_list($type = 'all-open', $paged = 1) {
// builds and returns a WP_Query for marketing requests post types
$prefix = GGA_Marketing_Request::$meta_prefix;
// all-open
// my-open
// all-completed
$meta_query = array();
if ($type == 'all-open' || $type == 'my-open') {
// status = 'New' or 'Open'
$meta_query[] = array(
'key' => $prefix . 'status',
'value' => array ( GGA_Marketing_Request::$status_open, GGA_Marketing_Request::$status_hold ),
'compare' => 'IN'
);
}
if ($type == 'all-completed' ) {
// status = 'Completed'
$meta_query[] = array(
'key' => $prefix . 'status',
'value' => array ( GGA_Marketing_Request::$status_completed ),
'compare' => 'IN'
);
}
if ($type == 'my-open') {
$current_user = wp_get_current_user();
$meta_query[] = array(
'key' => $prefix . 'assigned_to',
'value' => $current_user->ID,
'compare' => '='
);
}
$posts_per_page = -1;
if ($type =='all-completed')
$posts_per_page = 20;
$args = array(
'post_type' => GGA_Marketing_Request::$post_type,
'post_status' => 'publish',
'meta_query' => $meta_query,
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'orderby' => 'meta_value_num',
'order' => 'asc',
'meta_key' => $prefix . 'estimated_completion_time'
);
return new WP_Query( $args );
}
function assigned_to_names($post_id) {
// returns a pretty list of assigned to names
$users = $this->role_user_list();
$assigned_to_names = '';
$assigned_to = get_post_meta($post_id, GGA_Marketing_Request::$meta_prefix . 'assigned_to'); // $this->get_value_from_post_meta($post_meta, 'assigned_to');
if (is_array($assigned_to)) {
foreach($assigned_to as $user_id) {
foreach($users as $user) {
if ($user->ID == $user_id) {
if ($assigned_to_names != '')
$assigned_to_names .= ', ';
$assigned_to_names .= $user->display_name;
}
}
}
}
return $assigned_to_names;
}
function request_list_table($query) {
// builds an HTML table of marketing requests based on the WP_Query that was passed
$html = '';
if ($query->have_posts()) {
wp_enqueue_script('jquery-tablesorter', get_bloginfo('template_url') . '/gga-marketing-request/tablesorter/jquery.tablesorter.min.js', array('jquery'));
wp_enqueue_style('jquery-tablesorter', get_bloginfo('template_url') . '/gga-marketing-request/tablesorter/blue/style.css');
$html .= '<table class="tablesorter marketing-request-list">';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th>Submitted By</th>';
$html .= '<th>Type</th>';
$html .= '<th>Title</th>';
$html .= '<th>Assigned To</th>';
$html .= '<th>Requested Due</th>';
$html .= '<th>Estimated Completion</th>';
$html .= '<th>Status</th>';
$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';
while ($query->have_posts()) {
$query->the_post();
$post_meta = get_post_meta(get_the_id());
$assigned_to_names = $this->assigned_to_names(get_the_id());
$html .= '<tr>';
$html .= '<td>' . ($this->get_value_from_post_meta($post_meta, 'your_name')) . '</td>';
$html .= '<td>' . ($this->friendly_request_type($this->get_value_from_post_meta($post_meta, 'type'))) . '</td>';
$html .= '<td><a href="' . get_permalink(get_the_id()) . '">' . ($this->get_value_from_post_meta($post_meta, 'project_name')) . '</a></td>';
$html .= '<td>' . ($assigned_to_names) . '</td>';
$html .= '<td>' . ($this->get_value_from_post_meta($post_meta, 'due_date')) . '</td>';
$html .= '<td>' . ($this->get_value_from_post_meta($post_meta, 'estimated_completion')) . '</td>';
$html .= '<td>' . ($this->get_value_from_post_meta($post_meta, 'status')) . '</td>';
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
} else {
$html .= '<div class="no-matches">No matches found</div>';
}
return $html;
}
function request_list($args)
{
// builds the page for the gga-marketing-request-list shortcode
global $wpdb;
$prefix = GGA_Marketing_Request::$meta_prefix;
$html = '';
$page_id = get_the_id();
//extract(shortcode_atts(array(
// 'type' => 'new-revised'
// ), $atts));
$paged = max( 1, isset($_REQUEST['pg']) ? $_REQUEST['pg'] : 1 );
if ($paged == 1) {
if ($this->current_user_is_in_roles()) {
$html .= '<h3>My Open Projects</h3>';
$query = $this->query_request_list('my-open');
$html .= $this->request_list_table($query);
}
$html .= '<h3>All Open Projects</h3>';
$query = $this->query_request_list('all-open');
$html .= $this->request_list_table($query);
}
$html .= '<h3>Completed Projects</h3>';
$query = $this->query_request_list('all-completed', $paged);
$html .= $this->request_list_table($query);
if ($query->found_posts > $query->query['posts_per_page'])
$html .= paginate_links(array(
'total' => $query->max_num_pages,
'current' => $paged,
'format' => '?pg=%#%',
'base' => get_permalink($page_id) . '%_%' ,
));
wp_reset_query();
$html .= '
<script type="text/javascript">
jQuery(document).ready(function() { jQuery("table.tablesorter").tablesorter(); });
</script>
';
return $html;
}
function get_value_from_post_meta($post_meta, $key) {
$prefix = GGA_Marketing_Request::$meta_prefix;
if ($post_meta && isset($post_meta[$prefix . $key]) && count($post_meta[$prefix . $key] > 0))
return $post_meta[$prefix . $key][0];
else
return '';
}
function friendly_request_type($type, $boxes = null) {
if (!$boxes) {
$boxes = array();
$boxes = $this->define_custom_meta_boxes($boxes);
}
// boxes-fields-type-option
foreach($boxes as $box) {
if (isset($box['fields'])) {
$fields = $box['fields'];
foreach($fields as $field) {
if ($field['id'] === GGA_Marketing_Request::$meta_prefix . 'type') {
foreach($field['options'] as $option) {
if ($option['value'] === $type)
return $option['name'];
}
}
}
}
}
return $type;
}
function admin_menu() {
add_options_page( 'Marketing Request Form Options', 'Marketing Request Form', 'manage_options', 'gga-mktg-request-options', array(&$this, 'admin_options_page') );
}
function enqueue_jquery_validate() {
wp_enqueue_script('jquery-validate', get_bloginfo('template_url') . '/gga-marketing-request/jquery.validate.min.js', array('jquery'), GGA_Marketing_Request::$version);
}
function admin_options_page()
{
$opt = GGA_Marketing_Request::$options;
if ( !current_user_can( 'manage_options' ) )
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
$this->enqueue_jquery_validate();
?>
<div class="wrap">
<h2>Marketing Request Form Options</h2>
<form method="post" action="options.php" id="gga-mkt-request-settings">
<?php
settings_fields( $opt );
do_settings_sections( $opt );
submit_button();
?>
</form>
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#gga-mkt-request-settings').validate(
);
});
</script>
<?php
}
function admin_register_settings()
{
$opt = GGA_Marketing_Request::$options;
register_setting( $opt, $opt);
$section = $opt . '_general';
add_settings_section($section, 'General', array(&$this, 'gga_mktg_request_settings_section'), $opt);
add_settings_field('notifications-enabled', 'Notifications Enabled:', array(&$this, 'gga_mktg_request_setting_notifications_enabled'), $opt, $section);
add_settings_field('from-email', 'From E-Mail:', array(&$this, 'gga_mktg_request_setting_from_email'), $opt, $section);
add_settings_field('bcc-email', 'BCC E-Mail:', array(&$this, 'gga_mktg_request_setting_bcc_email'), $opt, $section);
$section = $opt . '_roles';
add_settings_section($section, 'Roles', array(&$this, 'gga_mktg_request_settings_section'), $opt);
add_settings_field('role-' . GGA_Marketing_Request::$type_new_revised, 'New or Revised:', array(&$this, 'gga_mktg_request_setting_role_new_revised'), $opt, $section);
add_settings_field('role-' . GGA_Marketing_Request::$type_event_promo, 'Event Promo:', array(&$this, 'gga_mktg_request_setting_role_event_promo'), $opt, $section);
add_settings_field('role-' . GGA_Marketing_Request::$type_print_reorder, 'Print Re-order:', array(&$this, 'gga_mktg_request_setting_role_print_reorder'), $opt, $section);
}
public function user_ids_assigned_to_roles() {
$opt = GGA_Marketing_Request::$options;
$options = get_option($opt);
$user_ids = array();
$types = array(GGA_Marketing_Request::$type_new_revised, GGA_Marketing_Request::$type_print_reorder, GGA_Marketing_Request::$type_event_promo);
foreach($types as $type) {
$optionValue = array();
if (isset($options['role-' . $type])) {
$optionValue = $options['role-' . $type];
if (isset($optionValue) && is_array($optionValue)) {
foreach($optionValue as $ov)
$user_ids[] = intval($ov);
}
}
}
return $user_ids;
}
public function current_user_is_in_roles() {
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID )
return false;
else
return in_array($current_user->ID, $this->user_ids_assigned_to_roles());
}
public function user_id_is_in_roles($user_id) {
return in_array($user_id, $this->user_ids_assigned_to_roles());
}
function gga_mktg_request_settings_section() {}
function gga_mktg_request_setting_notifications_enabled() {
$this->gga_mktg_request_setting_checkbox('notifications-enabled');
}
function gga_mktg_request_setting_checkbox($name) {
$opt = GGA_Marketing_Request::$options;
$options = get_option($opt);
$optionValue = isset($options[$name]) ? $options[$name] : "";
$checked = $optionValue === '1' ? 'checked="checked"' : '';
echo "<input type=\"checkbox\" id='{$name}' name='" . GGA_Marketing_Request::$options . "[{$name}]' value='1' {$checked} />";
}
function gga_mktg_request_setting_user_checkboxes($name) {
$opt = GGA_Marketing_Request::$options;
$options = get_option($opt);
$userargs = array(
'blog_id' => $GLOBALS['blog_id'],
'role' => '',
'meta_key' => '',
'meta_value' => '',
'meta_compare' => '',
'meta_query' => array(),
'include' => array(),
'exclude' => array(),
'orderby' => 'display_name',
'order' => 'ASC',
'offset' => '',
'search' => '',
'number' => '',
'count_total' => false,
'fields' => 'all',
'who' => ''
);
$users = get_users($userargs);
//var_dump($users);
$option = '';
foreach($users as $user) {
$checked = '';
$optionValue = array();
if (isset($options[$name])) {
$optionValue = $options[$name];
if (isset($optionValue) && is_array($optionValue))
$checked = in_array($user->ID, $optionValue) ? 'checked="checked"' : '';
}
$element_id = $name . '_gga_user_' . $user->ID;
$option .= "<label for=\"{$element_id}\">";
$option .= "<input id=\"{$element_id}\" type=\"checkbox\" name=\"{$opt}[{$name}][]\" value=\"{$user->ID}\" {$checked} />&nbsp;";
$option .= htmlspecialchars($user->display_name) ;
$option .= "</label><br/>";
}
echo $option;
}
function gga_mktg_request_setting_input($name, $size, $maxlength, $classes = '')
{
$options = get_option(GGA_Marketing_Request::$options);
$optionValue = isset($options[$name]) ? $options[$name] : "";
echo "<input id='{$name}' name='" . GGA_Marketing_Request::$options . "[{$name}]' size='{$size}' maxlength='{$maxlength}' type='text' value='" . $optionValue . "' class=\"{$classes}\" />";
}
function gga_mktg_request_setting_from_email() {
$this->gga_mktg_request_setting_input('from-email', 50, 150, 'required email');
}
function gga_mktg_request_setting_bcc_email() {
$this->gga_mktg_request_setting_input('bcc-email', 50, 150, 'email');
}
function gga_mktg_request_setting_role_new_revised() {
$this->gga_mktg_request_setting_user_checkboxes('role-' . GGA_Marketing_Request::$type_new_revised);
}
function gga_mktg_request_setting_role_event_promo() {
$this->gga_mktg_request_setting_user_checkboxes('role-' . GGA_Marketing_Request::$type_event_promo);
}
function gga_mktg_request_setting_role_print_reorder() {
$this->gga_mktg_request_setting_user_checkboxes('role-' . GGA_Marketing_Request::$type_print_reorder);
}
function notifications_enabled() {
$options = get_option(GGA_Marketing_Request::$options);
return isset($options['notifications-enabled']) ? $options['notifications-enabled'] : "";
}
}
<?php
/*
Template for displaying a single marketing request
*/
$form_type = get_post_meta(get_the_id(), GGA_MARKETING_Request::$meta_prefix . 'type', true);
// add extra body classes for CSS formatting
add_filter('body_class','gga_mktg_request_single_body_classes');
function gga_mktg_request_single_body_classes($classes) {
$classes[] = 'marketing-form';
$classes[] = 'marketing-form-view';
$classes[] = $form_type . '-form';
$classes[] = $form_type . '-view-form';
return $classes;
}
get_header();
// defined in functions.php
global $gga_marketing_request;
$boxes = $gga_marketing_request->define_custom_meta_boxes(array(), true);
$sections = $gga_marketing_request->create_sections_for_type($form_type, true);
wp_enqueue_style( 'gga-mktg-request', get_bloginfo('template_url') . '/gga-marketing-request/style.css', null, GGA_MARKETING_Request::$version );
$editUrl = get_edit_post_link(get_the_id());
$editUrlText = 'Edit Marketing Request';
if (!is_user_logged_in()) {
$editUrlText = 'Login';
$editUrl = wp_login_url( get_permalink() );
}
else if (!$gga_marketing_request->user_id_is_in_roles(wp_get_current_user()->ID))
$editUrl = '';
?>
<div id="primary" class="ggaMktgForm entry-content site-content">
<div id="content" role="main" >
<article>
<header class="entry-header">
<h1 class="entry-title">Marketing Request: <?php the_title( $before = '', $after = '', $echo = true ) ?></h1>
</header>
<div class="entry-content">
<a title="Marketing Requests" href="marketing-request-list">&lt;&lt; Return to Marketing Requests</a>
<?php if (!empty($editUrl)) echo ' | <a href="' . $editUrl . '">' . $editUrlText . '</a>'; ?>
<br/><br/>
<?php
foreach($sections as $section) {
$attachments_output = false;
$sectionId = $section['id'];
$html .= '<fieldset class="' . $section['cssClass'] . '">';
$html .= '<legend>' . $section['name'] . '</legend>';
$fields = $gga_marketing_request->get_fields_for_section($boxes, $section);
foreach($fields as $field) {
//$html .= var_export($field, true);
$fieldType = $field['type'];
$fieldId = $field['id'];
$output_label = true;
if ($fieldType === 'file' && $attachments_output)
$output_label = false;
if ($output_label) {
$html .= '<span class="label">';
if (isset($field['prompt-' . $form_type ]))
$html .= $field['prompt-' . $form_type ];
else if (isset($field['name']) && !empty($field['name']))
$html .= htmlspecialchars($field['name']) . ':';
if (isset($field['desc']) && !empty($field['desc']))
$html .= ' <span class="smaller desc">' . $field['desc'] . '</span>';
$html .= '</span> ';
}
$html .= '<span class="data">';
if ($fieldId === GGA_MARKETING_Request::$meta_prefix . 'assigned_to_names')
$html .= $gga_marketing_request->assigned_to_names(get_the_id());
switch ($fieldType) {
case 'text':
case 'text_small':
case 'text_medium':
case 'text_date':
case 'select':
case 'textarea':
$html .= get_post_meta(get_the_id(), $fieldId, true);
break;
case 'multicheck':
$values = get_post_meta(get_the_id(), $fieldId);
if (isset($values))
{
for($i = 0; $i < count($values); $i++)
{
if ($i > 0)
$html .= ', ';
$html .= htmlspecialchars($values[$i]);
}
}
break;
case 'file':
if (!$attachments_output)
{
$attachments = get_children(
array(
'post_parent' => get_the_id(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any'
)
);
if (isset($attachments) && count($attachments) > 0) {
//$html .= var_export($attachments);
$html .= '<ul class="attachments">';
foreach($attachments as $attachment) {
$html .= '<li>';
$html .= '<a href="' . wp_get_attachment_url( $attachment->ID ) . '" target="_blank">' . htmlspecialchars(basename(get_attached_file($attachment->ID))) . '</a>';
$html .= '</li>';
}
$html .= '</ul>';
}
$attachments_output = true;
}
break;
}
if ($output_label)
$html .= '</span> <!-- .data -->';
}
$html .= '</fieldset>';
}
echo $html;
?>
</article>
</div>
</div><!-- #content -->
</div><!-- #container -->
<?php
get_sidebar();
get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment