Ejemplo n.º 1
0
 /**
  * Get the formatted value of a field
  *
  * This is used for HTML and non-HTML output so HTML should not be added
  * - see printFieldValue below for that.
  */
 public function getFormattedValue($name, $value = null)
 {
     if (!isset($this->fields[$name])) {
         trigger_error('Cannot get value for field ' . ents($name) . ' - field does not exist', E_USER_WARNING);
         return NULL;
     }
     if (is_null($value)) {
         $value = array_get($this->values, $name, NULL);
     }
     $field = $this->fields[$name];
     if (!empty($field['references'])) {
         $obj =& $GLOBALS['system']->getDBObject($field['references'], $value);
         if (!is_null($obj)) {
             if (!array_get($field, 'show_id', true)) {
                 return $obj->toString();
             } else {
                 return $obj->toString() . ' (#' . $value . ')';
             }
         } else {
             if ($value != 0) {
                 return $value;
             }
         }
         return '';
     }
     switch ($field['type']) {
         case 'select':
             return array_get($field['options'], $value, '(Invalid Value)');
             break;
         case 'datetime':
             if (empty($value) && array_get($field, 'allow_empty')) {
                 return '';
             }
             return format_datetime($value);
             break;
         case 'date':
             if (empty($value) && array_get($field, 'allow_empty')) {
                 return '';
             }
             return format_date($value);
             break;
         case 'bibleref':
             require_once 'bible_ref.class.php';
             $br = new bible_ref($value);
             return $br->toShortString();
             break;
         case 'phone':
             return format_phone_number($value, $field['formats']);
             break;
         default:
             if (is_array($value)) {
                 return '<pre>' . print_r($value, 1) . '</pre>';
             } else {
                 return $value;
             }
     }
 }
Ejemplo n.º 2
0
function print_widget($name, $params, $value)
{
    $classes = array_get($params, 'class', '');
    if (!array_get($params, 'allow_empty', 1)) {
        $classes .= ' compulsory';
    }
    $attrs = array();
    foreach (array_get($params, 'attrs', array()) as $attr => $val) {
        $attrs[] = $attr . '="' . ents($val) . '"';
    }
    $attrs = implode(' ', $attrs);
    switch ($params['type']) {
        case 'phone':
            $lengths = get_valid_phone_number_lengths($params['formats']);
            $width = max(get_phone_format_lengths($params['formats']));
            ?>
			<input name="<?php 
            echo $name;
            ?>
" type="tel" size="<?php 
            echo $width;
            ?>
" value="<?php 
            echo format_phone_number($value, $params['formats']);
            ?>
" class="phone-number" validlengths="<?php 
            echo implode(',', $lengths);
            ?>
" <?php 
            echo $attrs;
            ?>
 />
			<?php 
            break;
        case 'bibleref':
            require_once 'bible_ref.class.php';
            $br = new bible_ref($value);
            $value = $br->toShortString();
            $params['class'] = 'bible-ref';
            // fall through
        // fall through
        case 'text':
        case 'email':
            $maxlength_exp = empty($params['maxlength']) ? '' : 'maxlength="' . $params['maxlength'] . '"';
            if (array_get($params, 'height', 1) > 1) {
                $cols_exp = empty($params['width']) ? '' : 'cols="' . $params['width'] . '"';
                ?>
				<textarea name="<?php 
                echo $name;
                ?>
" rows="<?php 
                echo $params['height'];
                ?>
" <?php 
                echo $cols_exp;
                ?>
 class="<?php 
                echo trim($classes);
                ?>
" <?php 
                echo $maxlength_exp;
                ?>
><?php 
                echo ents($value);
                ?>
</textarea>
				<?php 
            } else {
                $width_exp = empty($params['width']) ? '' : 'size="' . $params['width'] . '"';
                $regex_exp = empty($params['regex']) ? '' : 'regex="' . trim($params['regex'], '/ ') . '"';
                $autocomplete_exp = isset($params['autocomplete']) ? 'autocomplete=' . ($params['autocomplete'] ? 'on' : 'off') . '"' : '';
                ?>
				<input type="<?php 
                echo $params['type'];
                ?>
" name="<?php 
                echo $name;
                ?>
" value="<?php 
                echo ents($value);
                ?>
" class="<?php 
                echo trim($classes);
                ?>
" <?php 
                echo implode(' ', array($maxlength_exp, $width_exp, $regex_exp, $autocomplete_exp));
                ?>
 <?php 
                echo $attrs;
                ?>
 />
				<?php 
            }
            break;
        case 'html':
            static $includedCK = false;
            if (!$includedCK) {
                ?>
				<script src="<?php 
                echo BASE_URL . 'resources/ckeditor/ckeditor.js';
                ?>
"></script>
				<?php 
            }
            ?>
			<textarea class="ckeditor" name="<?php 
            echo $name;
            ?>
" <?php 
            echo $attrs;
            ?>
><?php 
            echo $value;
            ?>
</textarea>
			<?php 
            break;
        case 'int':
            $classes .= ' int-box';
            $width_exp = '';
            if (!empty($params['width'])) {
                $width_exp = 'size="' . $params['width'] . '" ';
            } else {
                $width_exp = 'size="3" ';
            }
            ?>
			<input type="text" name="<?php 
            echo $name;
            ?>
" value="<?php 
            echo $value;
            ?>
" class="<?php 
            echo trim($classes);
            ?>
" <?php 
            echo $width_exp;
            ?>
 <?php 
            echo $attrs;
            ?>
 />
			<?php 
            break;
        case 'select':
            $our_val = is_array($value) ? $value : ($value === '' ? array() : array("{$value}"));
            foreach ($our_val as $k => $v) {
                $our_val[$k] = "{$v}";
            }
            if (array_get($params, 'style', 'dropbox') == 'colour-buttons') {
                ?>
				<div class="radio-button-group <?php 
                echo array_get($params, 'class', '');
                ?>
" 
					 <?php 
                if (!SizeDetector::isNarrow()) {
                    echo ' tabindex="1"';
                }
                ?>
				>
				<input type="hidden" name="<?php 
                echo $name;
                ?>
" value="<?php 
                echo reset($our_val);
                ?>
" />
				<?php 
                foreach ($params['options'] as $k => $v) {
                    $classes = 'btn value-' . $k;
                    if (in_array("{$k}", $our_val, true)) {
                        $classes .= ' active';
                    }
                    ?>
					<div 
						class="<?php 
                    echo $classes;
                    ?>
" 
						title="<?php 
                    echo $v;
                    ?>
"
						data-val="<?php 
                    echo $k;
                    ?>
"
					>
						<?php 
                    echo strtoupper($v[0]);
                    ?>
					</div>
					<?php 
                }
                ?>
				</div>
				<?php 
            } else {
                if (array_get($params, 'allow_multiple')) {
                    $height = array_get($params, 'height', min(count($params['options']), 4));
                    if (substr($name, -2) != '[]') {
                        $name .= '[]';
                    }
                    $style = 'height: ' . $height * 1.8 . 'em';
                    $classes .= ' multi-select';
                    // the empty onclick below is to make labels work on iOS
                    // see http://stackoverflow.com/questions/5421659/html-label-command-doesnt-work-in-iphone-browser
                    ?>
				<div class="<?php 
                    echo $classes;
                    ?>
" style="<?php 
                    echo $style;
                    ?>
" tabindex="0" onclick="" <?php 
                    echo $attrs;
                    ?>
 >
					<?php 
                    foreach ($params['options'] as $k => $v) {
                        $checked_exp = in_array("{$k}", $our_val, true) ? ' checked="checked"' : '';
                        $disabled_exp = !empty($params['disabled_prefix']) && strpos($k, $params['disabled_prefix']) === 0 ? ' disabled="disabled" ' : '';
                        ?>
						<label class="checkbox" title="<?php 
                        echo ents($v);
                        ?>
">
							<input type="checkbox" name="<?php 
                        echo $name;
                        ?>
" value="<?php 
                        echo $k;
                        ?>
" <?php 
                        echo $checked_exp . $disabled_exp;
                        ?>
>
							<?php 
                        echo ents($v);
                        ?>
						</label>
						<?php 
                    }
                    ?>
				</div>
				<?php 
                } else {
                    ?>
				<select name="<?php 
                    echo $name;
                    ?>
" class="<?php 
                    echo $classes;
                    ?>
" <?php 
                    echo $attrs;
                    ?>
 >
					<?php 
                    if (array_get($params, 'allow_empty') && !array_get($params, 'allow_multiple')) {
                        ?>
						<option value=""><?php 
                        echo array_get($params, 'empty_text', '(None)');
                        ?>
</option>
						<?php 
                    }
                    foreach (array_get($params, 'options', array()) as $k => $v) {
                        $selected_exp = in_array("{$k}", $our_val, true) ? ' selected="selected"' : '';
                        $disabled_exp = !empty($params['disabled_prefix']) && strpos($k, $params['disabled_prefix']) === 0 ? ' disabled="disabled" ' : '';
                        ?>
						<option value="<?php 
                        echo $k;
                        ?>
"<?php 
                        echo $selected_exp . $disabled_exp;
                        ?>
><?php 
                        echo ents($v);
                        ?>
</option>
						<?php 
                    }
                    ?>
				</select>
				<?php 
                }
            }
            break;
        case 'date':
            $day_year_classes = trim($classes . ' int-box');
            if (FALSE === strpos($name, '[')) {
                $name_template = $name . '%s';
            } else {
                $name_template = substr($name, 0, strpos($name, '[')) . '%s' . substr($name, strpos($name, '['));
            }
            $months = array();
            if (array_get($params, 'allow_empty', false)) {
                $months[''] = '(Month)';
            }
            for ($i = 1; $i < 13; $i++) {
                $months[$i] = date(array_get($params, 'month_format', 'F'), strtotime("2007-{$i}-01"));
            }
            if (empty($value)) {
                $value = date('Y-m-d');
            }
            // blank dates not allowed
            list($year_val, $month_val, $day_val) = explode('-', substr($value, 0, 10));
            ?>
			<span class="nowrap" <?php 
            echo $attrs;
            ?>
 >
			<input type="text" name="<?php 
            printf($name_template, '_d');
            ?>
" class="day-box <?php 
            echo $day_year_classes;
            ?>
" size="2" maxlength="2" value="<?php 
            echo $day_val;
            ?>
" placeholder="DD" /><select name="<?php 
            printf($name_template, '_m');
            ?>
" class="month-box <?php 
            echo $classes;
            ?>
">
				<?php 
            foreach ($months as $i => $month_name) {
                $selected = $i == $month_val ? ' selected="selected"' : '';
                ?>
					<option value="<?php 
                echo $i;
                ?>
"<?php 
                echo $selected;
                ?>
><?php 
                echo $month_name;
                ?>
</option>
					<?php 
            }
            ?>
			</select><input type="text" name="<?php 
            printf($name_template, '_y');
            ?>
" class="year-box <?php 
            echo $day_year_classes;
            ?>
" size="4" maxlength="4" value="<?php 
            echo $year_val;
            ?>
" placeholder="YYYY"/>
			</span>
			<?php 
            break;
        case 'reference':
            if (!empty($params['references'])) {
                $where = array();
                if (!empty($params['filter']) && is_array($params['filter'])) {
                    $where = $params['filter'];
                }
                $where_logic = array_get($params, 'filter_logic', 'AND');
                $options = $GLOBALS['system']->getDBObjectData($params['references'], $where, $where_logic, array_get($params, 'order_by'));
                $dummy = new $params['references']();
                $our_val = is_array($value) ? $value : (empty($value) ? array() : array($value));
                if (!empty($params['filter']) && is_callable($params['filter'])) {
                    foreach ($options as $i => $o) {
                        $dummy->populate($i, $o);
                        if (!in_array($i, $our_val) && !$params['filter']($dummy)) {
                            unset($options[$i]);
                        }
                    }
                }
                $params['options'] = array();
                foreach ($options as $k => $details) {
                    $dummy->populate($k, $details);
                    $params['options'][$k] = $dummy->toString();
                }
                $params['type'] = 'select';
                print_widget($name, $params, $value);
            }
            break;
        case 'bitmask':
            $value = (int) $value;
            ?>
			<div class="bitmask-boxes">
			<?php 
            $percol = false;
            $cols = 3;
            require_once 'include/size_detector.class.php';
            if (SizeDetector::getWidth()) {
                if (SizeDetector::isNarrow()) {
                    $cols = 1;
                } else {
                    if (SizeDetector::isMedium()) {
                        $cols = 2;
                    }
                }
            }
            if ($cols > 1) {
                $percol = ceil(count($params['options']) / $cols);
                ?>
				<div class="bitmask-column" <?php 
                echo $attrs;
                ?>
 >
				<?php 
            }
            $i = 0;
            foreach ($params['options'] as $k => $v) {
                $checked_exp = ($value & (int) $k) == $k ? 'checked="checked"' : '';
                // the empty onclick below is to make labels work on iOS
                // see http://stackoverflow.com/questions/5421659/html-label-command-doesnt-work-in-iphone-browser
                ?>
				<label class="checkbox" onclick="">
					<input type="checkbox" name="<?php 
                echo ents($name);
                ?>
[]" value="<?php 
                echo ents($k);
                ?>
" <?php 
                echo $checked_exp;
                ?>
>
					<?php 
                echo nbsp(ents($v));
                ?>
				</label>
				<?php 
                if ($percol && ++$i % $percol == 0) {
                    ?>
					</div>
					<div>
					<?php 
                }
            }
            if ($percol) {
                ?>
				</div>
				<?php 
            }
            ?>
			</div>
			<?php 
            break;
    }
}
Ejemplo n.º 3
0
function format_value($value, $params)
{
    if (!empty($params['references'])) {
        $obj =& $GLOBALS['system']->getDBObject($params['references'], $value);
        if (!is_null($obj)) {
            if (!array_get($params, 'show_id', true)) {
                return $obj->toString();
            } else {
                return $obj->toString() . ' (#' . $value . ')';
            }
        } else {
            if ($value != 0) {
                return $value;
            }
        }
        return '';
    }
    switch ($params['type']) {
        case 'select':
            return array_get($params['options'], $value, '(Invalid Value)');
            break;
        case 'datetime':
            if (empty($value) && array_get($params, 'allow_empty')) {
                return '';
            }
            return format_datetime($value);
            break;
        case 'date':
            if (empty($value) && array_get($params, 'allow_empty')) {
                return '';
            }
            return format_date($value);
            break;
        case 'bibleref':
            require_once 'bible_ref.class.php';
            $br = new bible_ref($value);
            return $br->toShortString();
            break;
        case 'phone':
            return format_phone_number($value, $params['formats']);
            break;
        default:
            if (is_array($value)) {
                return '<pre>' . print_r($value, 1) . '</pre>';
            } else {
                return $value;
            }
    }
}