/** "Add new" MENU PAGE */
function pacwtt_settings_new_activities()
{
    // Database Models
    $pacwtt_model_activity = new PACWTT_Model_activity();
    echo '<h1>' . __('New Activity', 'pacwtt-plugin') . '</h1>';
    echo "<div class='wrap'>";
    $name = '';
    $description = '';
    $message = '';
    if (isset($_POST['submit'])) {
        // Form submitted
        // Chech submission origin
        if (check_admin_referer('pacwtt-activity-form')) {
            // Validation of sanitized input
            // Validation rule: no empty activity name.
            $name = sanitize_text_field($_POST['pacwtt-activity-name']);
            $description = sanitize_text_field($_POST['pacwtt-activity-description']);
            if ('' == $name) {
                $message = pacwtt_error_message([__("Field 'name' can't be empty.", 'pacwtt-plugin')]);
            } else {
                $result = $pacwtt_model_activity->insert_item($name, $description);
                if (FALSE === $result) {
                    $message = pacwtt_error_message([__("Database insertion failed.", 'pacwtt-plugin')]);
                } else {
                    // Success
                    $message = pacwtt_updated_message(__('New Activity Added.', 'pacwtt-plugin'));
                    // Reset form
                    $name = '';
                    $description = '';
                }
            }
        } else {
            wp_die(__('Insufficient privileges. Operation aborted.', 'pacwtt-plugin'));
        }
    }
    ?>
	<!--  Message Area -->
	<?php 
    echo $message;
    ?>
	<!--  Activity Form -->
	<form id="pacwtt-activity-form" method="POST" action="<?php 
    echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']);
    ?>
">
		<hr />
		<table class="form-table">
			<tbody>
				<tr valign="top">
					<th scope="row">
						<label for="pacwtt-activity-name">
							<?php 
    _e('Name', 'pacwtt-plugin');
    ?>
:<br />
							<em><?php 
    _e('(HTML tags are not allowed)', 'pacwtt-plugin');
    ?>
</em>
						</label>
					</th>	
					<td>
						<input type='text' id='pacwtt-activity-name' name='pacwtt-activity-name' value='<?php 
    echo $name;
    ?>
' maxlength='32' size='32'>
					</td>
				</tr>
				<tr valign="top">
					<th scope="row">
						<label for="pacwtt-activity-description">
							<?php 
    _e('Description', 'pacwtt-plugin');
    ?>
:<br />
							<em><?php 
    _e('(HTML tags are not allowed)', 'pacwtt-plugin');
    ?>
</em>
						</label>
					</th>
					<td>
						<input type='text' id='pacwtt-activity-description' name='pacwtt-activity-description' value='<?php 
    echo $description;
    ?>
' maxlength='255' size='64'>
					</td>			
				</tr>
			</tbody>
		</table>
		<?php 
    wp_nonce_field('pacwtt-activity-form');
    submit_button('Add', 'primary');
    ?>
	</form>
<?php 
    echo "</div>";
}
Esempio n. 2
0
function pacwtt_shortcode_handler($atts, $content = null)
{
    // Database Models
    $pacwtt_model_activity = new PACWTT_Model_activity();
    $pacwtt_model_interval = new PACWTT_Model_Interval();
    $days_sequence = '';
    if (!isset($atts['id'])) {
        // Insufficient parameters
        return '<b>' . __("Error: no activity's id specified.", 'pacwtt-plugin') . '</b>';
    }
    //Attributes default
    $attributes_default = array('id' => '', 'layout' => get_option('pacwtt_option_layout'), 'dnsize' => 'full');
    $options = shortcode_atts($attributes_default, $atts);
    // "id" parameter validation and fetch
    $activity_data = $pacwtt_model_activity->read_item($options['id']);
    if (NULL === $activity_data) {
        //  Non existent 'id' activity
        return '<b>' . __("Error: requested id not found.", 'pacwtt-plugin') . '</b>';
    }
    $interval_data = $pacwtt_model_interval->get_activity_items($options['id'], false);
    // "size" parameter validation
    $days_sequence = $pacwtt_model_interval->dow_map;
    switch ($options['dnsize']) {
        case 'three':
            $days_sequence = array_map('shortcode_dnsize_three', $days_sequence);
            break;
        case 'full':
            // Do nothing
            break;
        default:
            // Invalid scortcode parameter
            return '<b>' . sprintf(__("Error: dnsize=%s parameter value not allowed.", 'pacwtt-plugin'), $options['dnsize']) . '</b>';
    }
    $days_sequence = array_flip($days_sequence);
    // day_name => day_index
    // "week starts on monday" global option
    if ('on' == get_option('pacwtt_option_monday')) {
        $options['monday'] = true;
    } else {
        $options['monday'] = false;
    }
    if ($options['monday'] == false) {
        // sunday
        // nothing to do
    } elseif ($options['monday'] == true) {
        // Monday first (rotate left)
        $days_sequence = array_assoc_rol($days_sequence);
    } else {
        //  Should not be executed
        return '<b>' . __("Error: global option 'monday' value not valid.", 'pacwtt-plugin') . '</b>';
    }
    // Layout parameter validation
    if ('text' != $options['layout'] && 'time_boxes' != $options['layout']) {
        // Invalid scortcode parametr
        return '<b>' . sprintf(__("Error: layout=%s parameter value not allowed.", 'pacwtt-plugin'), $options['layout']) . '</b>';
    }
    switch ($options['layout']) {
        case 'text':
            $html_output = pacwtt_shortcode_text($options['id'], $interval_data, $days_sequence);
            break;
        case 'time_boxes':
            $html_output = pacwtt_shortcode_time_boxes($options['id'], $interval_data, $days_sequence);
            break;
        default:
            wp_die(sprintf(__('Fatal Error: layout=%s parameter value not allowed.', 'pacwtt-plugin'), $options['layout']));
    }
    //Output
    wp_enqueue_style('pacwtt-css-tables');
    return $html_output;
}