Ejemplo n.º 1
0
function smarty_block_view_section($params, $content, &$smarty, $repeat)
{
    if (!empty($content)) {
        $attrs = array();
        $attrs['class'][] = 'heading';
        if (isset($params['class'])) {
            $attrs['class'][] = $params['class'];
        }
        if (isset($params['dont_prettify'])) {
            $heading = $params['heading'];
        } else {
            $heading = prettify($params['heading']);
        }
        if ($heading === 'EGS_HIDDEN_SECTION') {
            return '';
        }
        // convert attrs array to a string
        if (isset($params['expand'])) {
            $attrs['class'][] = 'expand';
            $attrs['class'][] = $params['expand'];
            $data['expand'] = $params['expand'] == 'closed' ? 'hidden' : '';
        }
        $data['attrs'] = build_attribute_string($attrs);
        $data['heading'] = $heading;
        $data['content'] = $content;
        // fetch smarty plugin template
        return smarty_plugin_template($smarty, $data, 'block.view_section');
    }
}
Ejemplo n.º 2
0
function smarty_block_showhidediv_body($params, $content, &$smarty)
{
    // Smarty Plug In to display collapsible structure
    //
    // Parameters -
    //		id		- id to uniquely identify the div to collapse/expand
    //		hide	- false(default) shows structure expanded
    // Opening Tag
    if (empty($content)) {
        return;
    }
    $data = array('content' => $content, 'ul' => array('attrs' => array()));
    if (isset($params['hide']) && $params['hide']) {
        $data['div']['attrs']['style'][] = 'display:none;';
    }
    if (isset($params['class_name'])) {
        $data['ul']['attrs']['class'][] = $params['class_name'];
        $data['div']['attrs']['class'][] = $params['class_name'];
    }
    $data['div']['attrs']['id'] = $params['id'];
    // convert attrs array to a string
    $data['ul']['attrs'] = build_attribute_string($data['ul']['attrs']);
    $data['div']['attrs'] = build_attribute_string($data['div']['attrs']);
    // fetch smarty plugin template
    return smarty_plugin_template($smarty, $data, 'block.showhidediv_body');
}
Ejemplo n.º 3
0
function smarty_block_grid_row($params, $content, &$smarty, $repeat)
{
    $data_attr = build_data_attributes($params);
    // build the attribute string based on the attribute array
    $attr_string = build_attribute_string($data_attr);
    if (!empty($content)) {
        return '<tr ' . $attr_string . '>' . $content . '</tr>' . "\n";
    } else {
        $model = $params['model'];
        $smarty->assign('gridrow_id', $model->{$model->idField});
    }
}
Ejemplo n.º 4
0
function smarty_block_heading_cell($params, $content, &$smarty, $repeat)
{
    if (!empty($content)) {
        // why isn't this being done way sooner?!
        if (substr($params['field'], -2) == 'id') {
            return '';
        }
        // attribute variables
        $attrs = array();
        // merge data attributes with attributes array
        $attrs = array_merge($attrs, build_data_attributes($params));
        if ($smarty->getTemplateVars('no_ordering') !== TRUE) {
            $link = $smarty->getTemplateVars('self');
            $link['value'] = prettify($content);
            $action = $smarty->getTemplateVars('action');
            if (empty($action)) {
                $action = 'index';
            }
            $link['action'] = $action;
            if (isset($params['field'])) {
                $link['orderby'] = $params['field'];
                $attrs['data-column'] = $params['field'];
            }
            $content = link_to($link, $data = true);
            // WTF
        } else {
            $content = prettify($content);
        }
        $model = $params['model'];
        if ($model && $model->getField($params['field'])->type == 'numeric' || $params['field'] == 'right') {
            $attrs['class'][] = 'right';
        }
        if (isset($params['class'])) {
            $attrs['class'][] = $params['class'];
        }
        // build the attribute string based on the attribute array
        $attrs = build_attribute_string($attrs);
        // return the built string
        return '<th ' . $attrs . '>' . $content . '</th>' . "\n";
    }
}
Ejemplo n.º 5
0
function smarty_block_data_table($params, $content, &$smarty, $repeat)
{
    if (!empty($content)) {
        // set initial vars
        $data = array('content' => $content, 'attrs' => array());
        // set id
        $data['attrs']['id'] = 'datagrid1';
        // set classes
        $data['attrs']['class'][] = 'datagrid';
        if (isset($params['class'])) {
            $data['attrs']['class'][] = $params['class'];
        }
        /*
         * Generate and output final HTML
         ***********************************************************/
        // convert attrs array to a string
        $data['attrs'] = build_attribute_string($data['attrs']);
        // fetch smarty plugin template
        return smarty_plugin_template($smarty, $data, 'block.data_table');
    }
}
Ejemplo n.º 6
0
function smarty_block_showhidediv($params, $content, &$smarty)
{
    // Smarty Plug In to display collapsible structure
    //
    // Parameters -
    //		images	- array of images to indicate closed/open focus/nofocus notexpandable
    //		id		- id to uniquely identify the div to collapse/expand
    //		name	- Title of the controlling div
    //		class_name	- css class_name for formatting the structure
    //		hide	- false(default) shows structure expanded
    // opening Tag
    if (empty($content)) {
        return;
    }
    $data = array('content' => $content, 'div' => array('attrs' => array()), 'img' => array('attrs' => array()));
    // Closing tag so wrap content within
    if (isset($params['images'])) {
        $images = $params['images'];
    } else {
        $images = array('open_nofocus' => THEME_URL . THEME . '/graphics/menu_open_nofocus.png', 'open_focus' => THEME_URL . THEME . '/graphics/menu_open_focus.png', 'closed_nofocus' => THEME_URL . THEME . '/graphics/menu_closed_nofocus.png', 'closed_focus' => THEME_URL . THEME . '/graphics/menu_closed_focus.png', 'noexpand' => THEME_URL . THEME . '/graphics/menu_noexpand.png');
    }
    if (isset($params['hide']) && $params['hide']) {
        $data['image']['attrs']['src'] = $images['closed_nofocus'];
        $data['image']['attrs']['class'][] = 'eglet_closed';
    } else {
        $data['image']['attrs']['src'] = $images['open_nofocus'];
        $data['image']['attrs']['class'][] = 'eglet_opened';
    }
    if (isset($params['class_name'])) {
        $data['div']['attrs']['class'][] = $params['class_name'];
    }
    $data['image']['attrs']['id'] = 'image_' . $params['id'];
    // convert attrs array to a string
    $data['image']['attrs'] = build_attribute_string($data['image']['attrs']);
    $data['div']['attrs'] = build_attribute_string($data['div']['attrs']);
    // fetch smarty plugin template
    return smarty_plugin_template($smarty, $data, 'block.showhidediv');
}
Ejemplo n.º 7
0
function smarty_function_datetime($params, &$smarty)
{
    $with =& $smarty->getTemplateVars('with');
    $data['display_tags'] = FALSE;
    $data['display_tags'] = !isset($params['notags']);
    $data['label']['value'] = prettify(isset($params['attribute']) ? $params['attribute'] : '');
    if (!empty($params['model'])) {
        $model =& $params['model'];
    } else {
        $model = $with['model'];
    }
    if (!empty($params['readonly']) && $params['readonly']) {
        $data['date']['attrs']['readonly'] = $params['readonly'];
        $data['hour']['attrs']['readonly'] = $params['readonly'];
        $data['minute']['attrs']['readonly'] = $params['readonly'];
        $data['date']['additional_class'] = '';
    } else {
        $data['date']['additional_class'] = 'datefield';
    }
    $controller_data =& $smarty->getTemplateVars('controller_data');
    #	$html = <<<EOT
    #<input type="text" name="%s" id="%s" class="icon date slim datefield%s" value="%s"/>&nbsp;
    #<input type="text" name="%s" id="%s" class="timefield" value="%s" />:<input type="text" name="%s" id="%s" class="timefield" value="%s" />
    #EOT;
    $basename = $params['attribute'];
    $model_name = get_class($model);
    $field = $model->getField($basename);
    $data['date']['attrs']['type'] = 'text';
    $data['hour']['attrs']['type'] = 'text';
    $data['minute']['attrs']['type'] = 'text';
    $data['date']['attrs']['name'] = $model_name . '[' . $basename . ']';
    $data['hour']['attrs']['name'] = $model_name . '[' . $basename . '_hours]';
    $data['minute']['attrs']['name'] = $model_name . '[' . $basename . '_minutes]';
    $data['date']['attrs']['id'] = strtolower($model_name . '_' . $basename);
    $data['hour']['attrs']['id'] = strtolower($model_name . '_' . $basename . '_hours');
    $data['minute']['attrs']['id'] = strtolower($model_name . '_' . $basename . '_minutes');
    $data['label']['for'] = $data['date']['attrs']['id'];
    $hidden = FALSE;
    require_once __DIR__ . '/function.sessionvalue.php';
    $start_date = smarty_function_sessionvalue($params, $model_name, $basename);
    $start_date_hour = smarty_function_sessionvalue($params, $model_name, $basename . '_hours');
    $start_date_minute = smarty_function_sessionvalue($params, $model_name, $basename . '_minutes');
    if (!empty($start_date)) {
        $value = $start_date;
        $value .= ' ' . (empty($start_date_hour) ? '00' : $start_date_hour);
        $value .= ':' . (empty($start_date_minute) ? '00' : $start_date_minute);
    }
    if (isset($controller_data[$basename])) {
        $hidden = TRUE;
        $value = $controller_data[$basename];
    } elseif (empty($value)) {
        $value = $field->value;
        if (empty($value) && !empty($params['value'])) {
            $value = $params['value'];
        }
        if (empty($value) && $field->has_default == 1) {
            $value = date(DATE_TIME_FORMAT, $field->default_value);
        }
        $data['label']['value'] = $field->tag;
        if ($field->not_null == 1) {
            $data['label']['value'] .= '*';
            $data['date']['additional_class'] .= ' required';
        }
    }
    $data['label']['value'] = isset($params['label']) ? $params['label'] : $data['label']['value'];
    if (!empty($value)) {
        $format = format_for_strptime(DATE_TIME_FORMAT);
        if (strptime($value, $format) !== FALSE) {
            $date_value = array_shift(explode(' ', $value));
            $hour_value = array_shift(explode(':', array_pop(explode(' ', $value))));
            $minute_value = array_pop(explode(':', array_pop(explode(' ', $value))));
        } else {
            list($date_value, $rest) = explode(' ', $value, 2);
            $date_value = date(DATE_FORMAT, strtotime($date_value));
            list($hour_value, $minute_value) = explode(':', $rest);
        }
        $data['date']['attrs']['value'] = $date_value;
        $data['hour']['attrs']['value'] = $hour_value;
        $data['minute']['attrs']['value'] = $minute_value;
    }
    if ($hidden) {
        $data['date']['attrs']['type'] = 'hidden';
        $data['hour']['attrs']['type'] = 'hidden';
        $data['minute']['attrs']['type'] = 'hidden';
    }
    foreach (array('date', 'hour', 'minute') as $type) {
        $data[$type]['attrs'] = build_attribute_string($data[$type]['attrs']);
    }
    return smarty_plugin_template($smarty, $data, 'function.datetime');
}
Ejemplo n.º 8
0
 public function generateTemplate()
 {
     $html = '';
     $data = array();
     $data['attrs']['name'] = '__moduleName';
     $data['attrs']['id'] = '__moduleName_id';
     $data['attrs']['type'] = 'hidden';
     $data['attrs']['value'] = $this->moduleName;
     $data['attrs'] = build_attribute_string($data['attrs']);
     $data['label']['attrs'] = build_attribute_string($data['label']['attrs']);
     $this->view->set('function_input', $data);
     $html .= $this->view->fetch('smarty/function.input');
     $fields = array_merge($this->preferences, $this->additionalFields, $this->handledPreferences);
     usort($fields, array('ModulePreferences', 'sortOnPosition'));
     foreach ($fields as $preference) {
         switch ($preference['type']) {
             case 'select_multiple':
                 $data = array();
                 $data['select']['attrs']['name'] = $preference['name'] . '[]';
                 $data['select']['attrs']['multiple'] = 'multiple';
                 $data['select']['attrs']['id'] = $preference['name'] . '_id';
                 $data['dt']['attrs']['class'] = 'for_multiple';
                 $data['dd']['attrs']['class'] = 'for_multiple';
                 if (isset($preference['display_name'])) {
                     $data['select']['label'] = $preference['display_name'];
                 } else {
                     $data['select']['label'] = $preference['name'];
                 }
                 $data['select']['label'] = $preference['display_name'];
                 $data['select']['id'] = $preference['name'];
                 if (isset($preference['value'])) {
                     $selected = $preference['value'];
                 } else {
                     $selected = '';
                 }
                 foreach ($preference['data'] as $index => $option) {
                     $option_attrs = array();
                     $key = $option['value'];
                     $option_attrs['value'] = h($key, ENT_COMPAT);
                     if (isset($option['selected']) && $option['selected']) {
                         $option_attrs['selected'] = 'selected';
                     }
                     $data['select']['options'][$index]['attrs'] = build_attribute_string($option_attrs);
                     $data['select']['options'][$index]['value'] = h($option['label']);
                 }
                 $data['display_tags'] = TRUE;
                 $data['select']['attrs'] = build_attribute_string($data['select']['attrs']);
                 $data['dt']['attrs'] = build_attribute_string($data['dt']['attrs']);
                 $data['dd']['attrs'] = build_attribute_string($data['dd']['attrs']);
                 $this->view->set('function_select', $data);
                 $html .= $this->view->fetch('smarty/function.select');
                 break;
             case 'select':
                 $data = array();
                 $data['select']['attrs']['name'] = $preference['name'];
                 $data['select']['attrs']['id'] = $preference['name'] . '_id';
                 if (isset($preference['display_name'])) {
                     $data['select']['label'] = $preference['display_name'];
                 } else {
                     $data['select']['label'] = $preference['name'];
                 }
                 $data['select']['id'] = $preference['name'];
                 if (isset($preference['value'])) {
                     $selected = $preference['value'];
                 } else {
                     $selected = '';
                 }
                 foreach ($preference['data'] as $index => $option) {
                     $option_attrs = array();
                     $key = $option['value'];
                     $option_attrs['value'] = h($key, ENT_COMPAT);
                     if (is_array($selected) && in_array($key, $selected) || $selected == $key) {
                         $option_attrs['selected'] = 'selected';
                     }
                     $data['select']['options'][$index]['attrs'] = build_attribute_string($option_attrs);
                     $data['select']['options'][$index]['value'] = h($option['label']);
                 }
                 $data['display_tags'] = TRUE;
                 $data['select']['attrs'] = build_attribute_string($data['select']['attrs']);
                 $this->view->set('function_select', $data);
                 $html .= $this->view->fetch('smarty/function.select');
                 break;
             case 'checkbox':
                 $data = array();
                 $data['attrs']['name'] = $preference['name'];
                 $data['attrs']['id'] = $preference['name'] . '_id';
                 $data['label']['attrs']['for'] = $preference['name'];
                 if (isset($preference['display_name'])) {
                     $data['label']['value'] = $preference['display_name'];
                 }
                 if (isset($preference['status']) && $preference['status'] == 'on') {
                     $data['attrs']['checked'] = 'checked';
                 }
                 $data['attrs']['type'] = 'checkbox';
                 $data['attrs']['class'] = 'checkbox';
                 $data['display_tags'] = TRUE;
                 $data['display_label'] = TRUE;
                 $data['attrs'] = build_attribute_string($data['attrs']);
                 $data['label']['attrs'] = build_attribute_string($data['label']['attrs']);
                 $this->view->set('function_input', $data);
                 $html .= $this->view->fetch('smarty/function.input');
                 break;
             case 'numeric':
             case 'password':
             case 'text':
                 $data = array();
                 $data['attrs']['name'] = $preference['name'];
                 $data['attrs']['id'] = $preference['name'] . '_id';
                 $data['label']['attrs']['for'] = $preference['name'];
                 if (isset($preference['display_name'])) {
                     $data['label']['value'] = $preference['display_name'];
                 }
                 if (isset($preference['value'])) {
                     $data['attrs']['value'] = $preference['value'];
                 }
                 $data['attrs']['type'] = $preference['type'] == 'password' ? 'password' : 'text';
                 $data['attrs']['class'] = $preference['type'] == 'numeric' ? ' class="numeric"' : '';
                 $data['display_tags'] = TRUE;
                 $data['display_label'] = TRUE;
                 $data['attrs'] = build_attribute_string($data['attrs']);
                 $data['label']['attrs'] = build_attribute_string($data['label']['attrs']);
                 $this->view->set('function_input', $data);
                 $html .= $this->view->fetch('smarty/function.input');
         }
     }
     return $html;
 }
Ejemplo n.º 9
0
function smarty_function_interval($params, &$smarty)
{
    $with =& $smarty->getTemplateVars('with');
    $data = array('class' => array());
    if (!empty($params['model'])) {
        $model =& $params['model'];
    } else {
        $model = $with['model'];
    }
    if (!empty($params['readonly']) && $params['readonly']) {
        $data['attrs']['readonly'] = $params['readonly'];
    }
    $controller_data =& $smarty->getTemplateVars('controller_data');
    $modelname = get_class($model);
    $basename = $params['attribute'];
    $unitname = $basename . '_unit';
    $name = $modelname . '[' . $basename . ']';
    $field = $model->getField($basename);
    if (isset($params['postfix'])) {
        $name .= $params['postfix'];
    }
    $data['select_name'] = $modelname . '[' . $unitname . ']';
    if (isset($params['postfix'])) {
        $data['select_name'] .= $params['postfix'];
    }
    $data['attrs']['id'] = strtolower($modelname) . '_' . $basename;
    $data['attrs']['name'] = $name;
    $data['label']['attrs']['for'] = strtolower($modelname) . '_' . $basename;
    $data['label']['value'] = $field->tag;
    $data['hidden'] = FALSE;
    $data['hours_selected'] = '';
    $data['days_selected'] = '';
    if (isset($params['value'])) {
        $data['attrs']['value'] = $params['value'];
    } elseif (isset($controller_data[$basename])) {
        $data['attrs']['value'] = $controller_data[$basename];
        $data['hidden'] = TRUE;
    } elseif (isset($_POST[$modelname][$basename])) {
        $data['attrs']['value'] = $_POST[$modelname][$basename];
    } elseif (!empty($params['group']) && isset($_POST[$params['group']][$modelname][$basename])) {
        $data['attrs']['value'] = $_POST[$params['group']][$modelname][$basename];
    } elseif (!empty($params['number']) && isset($_POST[$modelname][$params['number']][$basename])) {
        $data['attrs']['value'] = $_POST[$modelname][$params['number']][$basename];
    } elseif (isset($_SESSION['_controller_data'][$modelname][$basename])) {
        $data['attrs']['value'] = $_SESSION['_controller_data'][$modelname][$basename];
    } else {
        $data['attrs']['value'] = $field->value;
    }
    if (isset($_POST[$modelname][$unitname])) {
        $units = $_POST[$modelname][$unitname];
    } elseif (!empty($params['group']) && isset($_POST[$params['group']][$modelname][$unitname])) {
        $units = $_POST[$params['group']][$modelname][$unitname];
    } elseif (!empty($params['number']) && isset($_POST[$modelname][$params['number']][$unitname])) {
        $units = $_POST[$modelname][$params['number']][$unitname];
    } elseif (isset($_SESSION['_controller_data'][$modelname][$unitname])) {
        $units = $_SESSION['_controller_data'][$modelname][$unitname];
    } else {
        $units = 'hours';
    }
    $data['days_label'] = prettify('days');
    $data['hours_label'] = prettify('hours');
    $data['minutes_label'] = prettify('minutes');
    if (!isset($data['attrs']['value']) && $field->has_default == 1) {
        $data['attrs']['value'] = $field->default_value;
    }
    if (!empty($data['attrs']['value'])) {
        if (is_array($data['attrs']['value'])) {
            $units = $data['attrs']['value'][1];
            $data['attrs']['value'] = $data['attrs']['value'][0];
        } else {
            // ATTN: setting units twice?
            $data['attrs']['value'] = to_working_days($data['attrs']['value'], false);
            $data['attrs']['value'] = $data['attrs']['value'] * SystemCompanySettings::DAY_LENGTH;
        }
        $data[$units . '_selected'] = 'selected="selected"';
    }
    // processing over, collect vars
    // ATTN: should the above be $value, then converted to $data['attrs']['value']?
    if ($hidden) {
        if (isset($days_selected)) {
            $data['unit_value'] = 'days';
        } else {
            $data['unit_value'] = 'hours';
        }
    }
    $data['class'] = implode(' ', $data['class']);
    // convert attrs array to a string
    $data['attrs'] = build_attribute_string($data['attrs']);
    $data['label']['attrs'] = build_attribute_string($data['label']['attrs']);
    // fetch smarty plugin template
    return smarty_plugin_template($smarty, $data, 'function.interval');
}
Ejemplo n.º 10
0
function smarty_function_textarea($params, &$smarty)
{
    $controller_data =& $smarty->getTemplateVars('controller_data');
    $attribute = $params['attribute'];
    $row_id = '';
    $params['alias'] = '';
    // set default data array
    $data = array('label' => array('position' => 'left', 'attrs' => array(), 'value' => ''), 'textarea' => array('value' => '', 'attrs' => array('name' => $attribute)), 'dt' => array('attrs' => array()), 'dd' => array('attrs' => array()));
    if (!empty($params['model'])) {
        $model =& $params['model'];
    } else {
        $with =& $smarty->getTemplateVars('with');
        $model = $with['model'];
    }
    if (empty($params['alias'])) {
        if (isset($with['alias'])) {
            $params['alias'] = $with['alias'];
        }
    }
    if (isset($params['rowid'])) {
        $row_id = $params['rowid'];
    }
    if (isset($model)) {
        if (!empty($params['alias'])) {
            $alias = $model->getAlias($params['alias']);
            $aliasModelName = $alias['modelName'];
            $newmodel = new $aliasModelName();
            $field = $newmodel->getField($attribute);
            $data['textarea']['attrs']['name'] = get_class($model) . '[' . $params['alias'] . '][' . $attribute . ']';
            $data['textarea']['attrs']['id'] = get_class($model) . '_' . $params['alias'] . '_' . $attribute;
            if (!empty($_POST[get_class($model)][$params['alias']][$attribute])) {
                $data['textarea']['value'] = $_POST[get_class($model)][$params['alias']][$attribute];
            } elseif ($model->isLoaded()) {
                $newmodel = $model->{$params}['alias'];
                $data['textarea']['value'] = $newmodel->{$attribute};
            }
        } else {
            if (!empty($_POST[get_class($model)][$attribute])) {
                $data['textarea']['value'] = $_POST[get_class($model)][$attribute];
            } elseif (!empty($params['group']) && !empty($_POST[$params['group']][$model->get_name()][$attribute])) {
                $data['textarea']['value'] = $_POST[$params['group']][$model->get_name()][$attribute];
            } elseif (!empty($params['number']) && !empty($_POST[$model->get_name()][$params['number']][$attribute])) {
                $data['textarea']['value'] = $_POST[$model->get_name()][$params['number']][$attribute];
            } elseif (!empty($_SESSION['_controller_data'][get_class($model)][$attribute])) {
                $data['textarea']['value'] = $_SESSION['_controller_data'][get_class($model)][$attribute];
            }
            $field = $model->getField($attribute);
            if (!empty($params['group'])) {
                $data['textarea']['attrs']['name'] = $params['group'] . '[' . $model->get_name() . '][' . $attribute . ']';
            } elseif (isset($params['number'])) {
                $data['textarea']['attrs']['name'] = $model->get_name() . '[' . $params['number'] . '][' . $attribute . ']';
            } else {
                $data['textarea']['attrs']['name'] = get_class($model) . '[' . $attribute . ']';
            }
            $data['textarea']['attrs']['id'] = strtolower(get_class($model) . '_' . $attribute . $row_id);
            if ($model->loaded) {
                $data['textarea']['value'] = $model->{$attribute};
            }
            // If force=true then use the avaiable data
            if ($params['force']) {
                $data['textarea']['value'] = $controller_data[$attribute];
            }
        }
        //check whether required field
        if ($field->not_null == 1) {
            $data['label']['value'] .= '*';
        }
    }
    if (isset($params['editor']) && $params['editor'] == 'tinymce') {
        $data['textarea']['attrs']['class'][] = "tinymce";
    }
    if (isset($params['class']) && $params['class'] != '') {
        $data['textarea']['attrs']['class'][] = $params['class'];
    }
    if (!empty($params['label'])) {
        $data['label']['value'] = $params['label'];
    } else {
        $data['label']['value'] = $field->tag;
    }
    if (!empty($params['value'])) {
        $data['textarea']['value'] = $params['value'];
    }
    $data['display_tags'] = !(isset($params['tags']) && $params['tags'] == 'none');
    if (!empty($params['label_position'])) {
        $data['label']['position'] = $params['label_position'];
    }
    // NOTE: these are inline styles... I know it's now ideal :-(
    if (in_array($data['label']['position'], array('top', 'above'))) {
        $data['dt']['attrs']['style'][] = 'text-align: left;';
        $data['dt']['attrs']['style'][] = 'border-bottom: 0;';
        $data['dd']['attrs']['style'][] = 'clear: both;';
        $data['dd']['attrs']['style'][] = 'width: 100%;';
        $data['dd']['attrs']['style'][] = 'padding: 0;';
    }
    // convert attrs array to a string
    $data['textarea']['attrs'] = build_attribute_string($data['textarea']['attrs']);
    $data['dt']['attrs'] = build_attribute_string($data['dt']['attrs']);
    $data['dd']['attrs'] = build_attribute_string($data['dd']['attrs']);
    // fetch smarty plugin template
    return smarty_plugin_template($smarty, $data, 'function.textarea');
}
Ejemplo n.º 11
0
/**
 * Generate a link based on params and permissions
 *
 * @access	public
 * @param	array $params
 * @param	boolean $data
 * @param	boolean $html
 * @return	string
 */
function link_to($params, $data = FALSE, $html = TRUE)
{
    $script = '';
    $url = '?';
    $attr_array = array();
    $attr_string = '';
    $modules = array();
    $module = '';
    $tag = '';
    $ao = AccessObject::Instance();
    if (isset($params['modules'])) {
        $modules = $params['modules'];
        while (empty($module) && count($modules) > 0) {
            $module = array_pop($modules);
        }
    } else {
        $modkey = 'module';
        while (key_exists($modkey, $params)) {
            if (!empty($params[$modkey])) {
                $module = $params[$modkey];
            }
            $modkey = 'sub' . $modkey;
        }
    }
    // loop through each item here, check the params array and set the type variable
    foreach (array('pid', 'controller', 'action') as $type) {
        if (isset($params[$type])) {
            ${$type} = $params[$type];
        } else {
            ${$type} = '';
        }
    }
    // build data attributes, apply them to attributes array
    if (isset($params['data_attrs'])) {
        $attr_array[] = build_attribute_string(build_data_attributes($params['data_attrs']));
    }
    // if we're handling a print link we want to test permissions
    // against the print action and not the dialog action
    if (isset($params['printaction'])) {
        $action = $params['printaction'];
    }
    if (empty($pid)) {
        $pid = $ao->getPermission($module, $controller, $action);
    }
    $allowed = $ao->hasPermission($module, $controller, $action, $pid);
    $modules = array();
    if (!empty($pid)) {
        $modules['pid'] = $pid;
    }
    if (!empty($pid) && !isset($params['value'])) {
        if (!empty($params['action'])) {
            $permission = $params['action'];
        } elseif (!empty($params['controller'])) {
            $permission = $params['controller'];
        } elseif (!empty($module)) {
            $permission = $module;
        }
        $per = DataObjectFactory::Factory('Permission');
        $per->load($pid);
        switch ($per->type) {
            case 'g':
            case 'm':
            case 's':
                if ($per->permission == $permission) {
                    $tag = $per->title;
                }
                break;
            case 'c':
                if ($per->permission == $permission) {
                    $tag = $per->title;
                }
                break;
            case 'a':
                if ($per->permission == $permission) {
                    $tag = $per->title;
                }
        }
    }
    if (empty($tag) && isset($params['value'])) {
        $tag = $params['value'];
    }
    if (isset($params['modules'])) {
        $modulekey = 'module';
        foreach ($params['modules'] as $module) {
            $modules[$modulekey] = $module;
            $modulekey = 'sub' . $modulekey;
        }
        unset($params['modules']);
    }
    $params = array_merge($modules, $params);
    // remove the data attrs item
    unset($params['data_attrs']);
    foreach ($params as $key => $val) {
        //special cases
        if (substr($key, 0, 1) === '_') {
            $attr_array[str_replace('_', '', $key)] = $val;
            continue;
        }
        if ($key == 'value' || $key == 'img' || $key == 'alt' || $key == 'no_prettify') {
            continue;
        }
        if (!is_array($val)) {
            $url .= strtolower($key) . '=' . urlencode($val) . '&amp;';
        }
    }
    //remove last ampersand
    $url = substr($url, 0, -5);
    $url = '/' . $url;
    if (isset($params['link'])) {
        $url = $params['link'];
    }
    $attr_array['href'] = $url;
    // convert the attributes array to string
    $attr_string = build_attribute_string($attr_array);
    if (!$allowed) {
        if (isset($params['img']) || $data) {
            $string = $params['value'];
        } else {
            $string = prettify($tag);
        }
    } else {
        if ($html === TRUE) {
            if ($params['value'] == '') {
                $params['value'] = 'link';
            }
            if (isset($params['img'])) {
                $params['value'] = '<img src="' . $params['img'] . '" alt="' . $params['alt'] . '" />';
                $string = '<a ' . $attr_string . ' >' . $params['value'] . '</a>';
            } else {
                if ($data) {
                    $string = '<a ' . $attr_string . ' >' . $params['value'] . '</a>';
                } elseif (isset($params['no_prettify'])) {
                    $string = '<a ' . $attr_string . ' >' . $tag . '</a>';
                } else {
                    $string = '<a ' . $attr_string . ' >' . prettify($tag) . '</a>';
                }
            }
        } else {
            $string = str_replace('&amp;', '&', $url);
        }
    }
    return $string;
}
Ejemplo n.º 12
0
/** 
 *	(c) 2000-2012 uzERP LLP (support#uzerp.com). All rights reserved. 
 * 
 *	Released under GPLv3 license; see LICENSE. 
 **/
function smarty_block_content_wrapper($params, $content, &$smarty, $repeat)
{
    /**************************************************************
     * Content Wrapper block
     **************************************************************
     * The purpose of the content wrapper block is to encapsulate
     * each page used by uzERP. Its initial purpose was to provide
     * a simple alternative to the page identifier, however it has
     * also been used to house the page title and flash, the reason
     * being is that the page title and flash are content specific 
     * things, not page specific. When ajaxing content the page 
     * title and flash are not dragged along with it.
     * 
     * $Revision: 1.6 $
     * 
     **************************************************************/
    // set initial vars
    $data = array('attrs' => array(), 'title' => '', 'flash' => FALSE, 'content' => $content);
    if (!empty($content)) {
        /*
         * Page Identifier
         ***********************************************************/
        if (isset($params['module']) && !empty($params['module'])) {
            $module = $params['module'];
        } else {
            $module = trim($smarty->getTemplateVars('module'), '_');
        }
        if (isset($params['controller']) && !empty($params['controller'])) {
            $controller = $params['controller'];
        } else {
            $controller = trim($smarty->getTemplateVars('controller'), '_');
        }
        if (isset($params['action']) && !empty($params['action'])) {
            $action = $params['action'];
        } else {
            $action = trim(basename($smarty->getTemplateVars('templateName'), ".tpl"), '_');
        }
        if (!isset($params['page_identifier']) || $params['page_identifier'] !== FALSE) {
            $data['attrs']['id'] = strToLower($module . '-' . $controller . '-' . $action);
        }
        $data['attrs']['data-module'] = $module;
        $data['attrs']['data-controller'] = $controller;
        $data['attrs']['data-action'] = $action;
        $self = $smarty->getTemplateVars('self');
        unset($self['modules']);
        unset($self['module']);
        unset($self['controller']);
        unset($self['action']);
        // This is used by popup dialog forms to refresh the calling form/page
        foreach ($self as $key => $value) {
            if ($key != '_') {
                $data['attrs']['data-' . $key] = $value;
            }
        }
        /*
         * Class
         ***********************************************************/
        $data['attrs']['class'][] = 'content_wrapper';
        if (isset($params['class'])) {
            $data['attrs']['class'][] = $params['class'];
        }
        /*
         * Page Title
         ***********************************************************/
        if (!isset($params['title']) || $params['title'] !== FALSE) {
            if (isset($params['title'])) {
                $data['title'] = prettify($params['title']);
            } else {
                $data['title'] = prettify($smarty->getTemplateVars('page_title'));
            }
        }
        /*
         * Flash
         ***********************************************************/
        if (!isset($params['flash']) || $params['flash'] !== FALSE) {
            $data['flash'] = TRUE;
        }
        /*
         * Generate and output final HTML
         ***********************************************************/
        // convert attrs array to a string
        $data['attrs'] = build_attribute_string($data['attrs']);
        // fetch smarty plugin template
        return smarty_plugin_template($smarty, $data, 'block.content_wrapper');
    }
}
Ejemplo n.º 13
0
function smarty_block_grid_cell($params, $content, &$smarty, $repeat)
{
    if (!empty($content)) {
        // no point continuing if we're dealing with an id field
        if (substr($params['field'], -2) == 'id') {
            return '';
        }
        // set vars
        $attrs = array();
        $id = $smarty->getTemplateVars('gridrow_id');
        $with =& $smarty->getTemplateVars('with');
        if (!empty($params['model'])) {
            $model =& $params['model'];
        } else {
            $model = $with['model'];
        }
        if ($model->getField($params['field'])->type !== 'html' && !isset($params['no_escape'])) {
            $content = h(trim($content));
        }
        if ($params['cell_num'] == 1) {
            if ($smarty->getTemplateVars('clickaction') != 'none') {
                $link = array();
                $self = $smarty->getTemplateVars('self');
                if ($smarty->getTemplateVars('clickmodule')) {
                    $link['modules'] = $smarty->getTemplateVars('clickmodule');
                } else {
                    $link['modules'] = $self['modules'];
                }
                if ($smarty->getTemplateVars('clickcontroller')) {
                    $clickcontroller = $smarty->getTemplateVars('clickcontroller');
                } else {
                    $clickcontroller = $self['controller'];
                }
                if ($params['collection']->clickcontroller) {
                    $clickcontroller = $params['collection']->clickcontroller;
                }
                $link['controller'] = $clickcontroller;
                if ($params['collection']->editclickaction) {
                    $link['action'] = $params['collection']->editclickaction;
                } else {
                    $link['action'] = $smarty->getTemplateVars('clickaction');
                }
                if ($smarty->getTemplateVars('linkfield')) {
                    $linkfield = $smarty->getTemplateVars('linkfield');
                } else {
                    $linkfield = $model->idField;
                }
                if ($smarty->getTemplateVars('linkvaluefield')) {
                    $link[$linkfield] = $model->{$smarty->getTemplateVars('linkvaluefield')};
                } else {
                    $link[$linkfield] = $model->{$linkfield};
                }
                if ($smarty->getTemplateVars('linkdata')) {
                    foreach ($smarty->getTemplateVars('linkdata') as $field => $value) {
                        $link[$field] = $value;
                    }
                }
                foreach ($params as $field => $value) {
                    if (substr($field, 0, 1) == '_' && $model->isField(substr($field, 1))) {
                        $link[substr($field, 1)] = $value;
                    }
                }
                $link['value'] = $content;
                if (empty($link['pid'])) {
                    $ao = AccessObject::Instance();
                    $link['pid'] = $ao->getPermission($link['modules'], $link['controller'], $link['action']);
                }
                if (!empty($link['pid']) && !empty($link['action'])) {
                    $content = link_to($link, $data = true);
                    $_SESSION['cache'][get_class($model)][$params['field']] = array('pid' => $link['pid'], 'modules' => $link['modules'], 'controller' => $link['controller'], 'action' => $link['action']);
                }
            }
        } elseif (!empty($content) && isset($_SESSION['cache'][get_class($model)][$params['field']])) {
            $content = link_to(array('pid' => $_SESSION['cache'][get_class($model)][$params['field']]['pid'], 'module' => $_SESSION['cache'][get_class($model)][$params['field']]['module'], 'controller' => $_SESSION['cache'][get_class($model)][$params['field']]['controller'], 'action' => 'view', $_SESSION['cache'][get_class($model)][$params['field']]['id_field'] => $model->{$_SESSION['cache'][get_class($model)][$params['field']]['fk_field']}, 'value' => $content));
        } elseif (!empty($content)) {
            // If the field is in a belongsTo FK link, then enable the link
            // if the user has access to the target function
            // TODO: this code is copied from view_data so could be moved to generic function?
            $temp_lookups = array();
            if (isset($model->belongsToField[$params['field']])) {
                $belongs_field = strtolower($model->belongsToField[$params['field']]);
                $belongs_model = strtolower($model->belongsTo[$belongs_field]['model']);
                $fk_field = $model->belongsTo[$belongs_field]['field'];
            }
            if (isset($model->belongsTo[$params['field']])) {
                $belongs_model = strtolower($model->belongsTo[$params['field']]['model']);
                $fk_field = $model->belongsTo[$params['field']]['field'];
            }
            if (!empty($belongs_model)) {
                if (!isset($temp_lookups[$belongs_model])) {
                    $modulecomponent = DataObjectFactory::Factory('ModuleComponent');
                    $modulecomponent->loadBy(array('name', 'type'), array($belongs_model . 'scontroller', 'C'));
                    if ($modulecomponent->isLoaded()) {
                        $temp_lookups[$params['field']] = $modulecomponent->module->name;
                    }
                }
            } else {
                $belongs_model = $params['field'];
            }
            if (isset($temp_lookups[$params['field']]) || isset($temp_lookups[$params['fk']])) {
                if (isset($params['fk_field'])) {
                    $fk_field = $params['fk_field'];
                } elseif (empty($fk_field)) {
                    $fk_field = $params['field'] . '_id';
                }
                if (isset($params['fk'])) {
                    $belongs_model = $params['fk'];
                    $module = $temp_lookups[$params['fk']];
                } else {
                    $module = $temp_lookups[$params['field']];
                }
                if (method_exists($belongs_model . 'scontroller', 'view') && !is_null($model->{$fk_field})) {
                    $ao = AccessObject::Instance();
                    $pid = $ao->getPermission($module, $belongs_model . 's', 'view');
                    if (!empty($pid)) {
                        $link_model = DataObjectFactory::Factory($belongs_model);
                        $content = link_to(array('pid' => $pid, 'module' => $module, 'controller' => $belongs_model . 's', 'action' => 'view', $link_model->idField => $model->{$fk_field}, 'value' => $content));
                        $_SESSION['cache'][get_class($model)][$params['field']] = array('pid' => $pid, 'module' => $module, 'controller' => $belongs_model . 's', 'id_field' => $link_model->idField, 'fk_field' => $fk_field);
                    }
                }
            }
        }
        if ($params['field'] == $smarty->getTemplateVars('wide_column')) {
            $attrs['class'][] = 'wide_column';
        }
        if ($model->getField($params['field'])->type == 'numeric') {
            $attrs['class'][] = 'numeric';
        }
        if ($model->getField($params['field'])->type == 'bool') {
            $attrs['class'][] = 'icon';
            $content = '<img src="/themes/default/graphics/' . ($model->{$params['field']} == 't' ? 'true' : 'false') . '.png" alt="' . ($model->{$params['field']} == 't' ? 'true' : 'false') . '" />';
        }
        if (isset($params['class'])) {
            $attrs['class'][] = $params['class'];
        }
        $attrs['class'][] = 'row_' . $params['field'];
        if ($params['field'] == 'email') {
            $email = $model->getField($params['field'])->value;
            if (!empty($email)) {
                $content = '<a href="mailto:' . $email . '">' . $content . '</a>';
            }
        }
        if ($params['field'] == 'company') {
            $field = $model->companydetail->is_lead == 't' ? 'lead' : $params['field'];
        } else {
            $field = $params['field'];
        }
        if (($params['field'] == 'company' || $params['field'] == 'person') && !is_null($model->{$params['field'] . '_id'})) {
            $content = sprintf('<a href="/?module=contacts&controller=%s&action=view&id=%s">%s</a>', $field . 's', $model->{$params['field'] . '_id'}, $content);
        }
        // convert attrs array to a string
        $attrs = build_attribute_string($attrs);
        return '<td ' . $attrs . ' >' . $content . '</td>' . "\n";
    }
}
Ejemplo n.º 14
0
function smarty_function_input($params, &$smarty)
{
    $controller_data =& $smarty->getTemplateVars('controller_data');
    $attribute = $params['attribute'];
    $with =& $smarty->getTemplateVars('with');
    // no point in continuing if we're only going to return false anyway
    if (prettify($attribute) == 'EGS_HIDDEN_FIELD') {
        return '';
    }
    // continue setting vars
    $rowid = '';
    $data = array('attrs' => array('value' => '', 'type' => 'text'), 'display_tags' => !(isset($params['tags']) && $params['tags'] == 'none' || isset($with['tags']) && $with['tags'] == 'none'), 'display_label' => !isset($params['nolabel']) || $params['nolabel'] === FALSE);
    // append any data attributes passed in through params with the attrs array
    $data['attrs'] += build_data_attributes($params);
    if (empty($params['group'])) {
        $params['group'] = isset($with['group']) ? $with['group'] : '';
    }
    if (empty($params['alias'])) {
        $params['alias'] = isset($with['alias']) ? $with['alias'] : '';
    }
    if (empty($params['composite'])) {
        $params['composite'] = isset($with['composite']) ? $with['composite'] : '';
    }
    if (!empty($params['model'])) {
        $model =& $params['model'];
    } else {
        $model = $with['model'];
    }
    if (!empty($params['hidden'])) {
        $params['type'] = 'hidden';
    }
    // ATTN: used anymore?
    if (isset($params['onchange'])) {
        $data['attrs']['onchange'] = $params['onchange'];
    }
    if (isset($params['onclick'])) {
        $data['attrs']['onclick'] = $params['onclick'];
    }
    if (isset($params['label'])) {
        $params['label'] = trim($params['label']);
        if (strtoupper($params['label']) === 'FALSE' || empty($params['label'])) {
            $data['display_label'] = FALSE;
        } else {
            $data['label']['value'] = prettify($params['label']);
        }
    }
    if (isset($params['rowid'])) {
        $rowid = $params['rowid'];
    }
    // ATTN: above set params
    if (empty($data['attrs']['value'])) {
        $data['attrs']['value'] = $params['value'];
    }
    if (isset($model)) {
        if (!empty($params['alias'])) {
            $alias = $model->getAlias($params['alias']);
            $aliasModelName = $alias['modelName'];
            $newmodel = new $aliasModelName();
            $field = $newmodel->getField($attribute);
            if (empty($params['label'])) {
                $data['label']['value'] = $field->tag;
            }
            $data['attrs']['name'] = $model->get_name() . '[' . $params['alias'] . '][' . $attribute . ']';
            $data['attrs']['id'] = $model->get_name() . '_' . $params['alias'] . '_' . $attribute;
            if (isset($_POST[$model->get_name()][$params['alias']][$attribute])) {
                $data['attrs']['value'] = $_POST[$model->get_name()][$params['alias']][$attribute];
            } elseif (isset($_SESSION['_controller_data'][$model->get_name()][$attribute])) {
                $data['attrs']['value'] = $_SESSION['_controller_data'][$model->get_name()][$attribute];
            } elseif ($model->isLoaded()) {
                $newmodel = $model->{$params}['alias'];
                $data['attrs']['value'] = $newmodel->{$attribute};
            }
            if ((!isset($data['attrs']['value']) || empty($data['attrs']['value'])) && isset($params['value'])) {
                $data['attrs']['value'] = $params['value'];
            }
        } elseif (!empty($params['composite'])) {
            $alias = $model->getComposite($params['composite']);
            $aliasModelName = $alias['modelName'];
            $newmodel = new $aliasModelName();
            $field = $newmodel->getField($attribute);
            if (empty($params['label'])) {
                $data['label']['value'] = $field->tag;
            }
            $data['attrs']['name'] = $model->get_name() . '[' . $aliasModelName . '][' . $attribute . ']';
            $data['attrs']['id'] = $model->get_name() . '_' . $aliasModelName . '_' . $attribute;
            if (isset($_POST[$model->get_name()][$aliasModelName][$attribute])) {
                $data['attrs']['value'] = $_POST[$model->get_name()][$aliasModelName][$attribute];
            } elseif (isset($_SESSION['_controller_data'][$model->get_name()][$attribute])) {
                $data['attrs']['value'] = $_SESSION['_controller_data'][$model->get_name()][$attribute];
            } elseif ($model->isLoaded()) {
                $newmodel = $model->{$params}['composite'];
                $data['attrs']['value'] = $newmodel->{$attribute};
            }
            if ((!isset($data['attrs']['value']) || empty($data['attrs']['value'])) && isset($params['value'])) {
                $data['attrs']['value'] = $params['value'];
            }
        } else {
            $field = $model->getField($attribute);
            if ($field !== FALSE) {
                // If data for the field is available and it is not the idfield, make it hidden
                // unless this is overridden (force=true)
                // or it is pop-up dialog form field that is not to be hidden
                if ((!isset($controller_data['dialog']) || isset($params['hidden']) || $params['type'] == 'hidden') && isset($controller_data[$attribute]) && $model->idField != $attribute && $params['force'] != true) {
                    $data['attrs']['value'] = $controller_data[$attribute];
                    $params['type'] = 'hidden';
                }
                // If force=true then use the avaiable data
                if ($params['force']) {
                    $data['attrs']['value'] = $controller_data[$attribute];
                }
                //Set "not editable" fields to DISABLED, but only if not already hidden
                if ($model->isNotEditable($field->name) && $model->isDisplayedField($field->name)) {
                    $params['disabled'] = 'disabled';
                }
                if (empty($params['label'])) {
                    $data['label']['value'] = $field->tag;
                }
                $data['attrs']['name'] = $model->get_name() . '[' . $attribute . ']';
                if (isset($params['postfix'])) {
                    $data['attrs']['name'] .= $params['postfix'];
                }
                if (!empty($params['group'])) {
                    $data['attrs']['name'] = $params['group'] . '[' . $model->get_name() . '][' . $attribute . ']';
                } elseif (isset($params['number'])) {
                    $data['attrs']['name'] = $model->get_name() . '[' . $params['number'] . '][' . $attribute . ']';
                } else {
                    $data['attrs']['name'] = $model->get_name() . '[' . $attribute . ']';
                }
                if (isset($params['postfix'])) {
                    $data['attrs']['name'] .= $params['postfix'];
                }
                $data['attrs']['id'] = $model->get_name() . '_' . $attribute . $rowid;
                // ATTN: wow, big condition
                if ($params['type'] == 'checkbox' && (!isset($_POST[$model->get_name()][$attribute]) && isset($_POST[$model->get_name()]['_checkbox_exists_' . $attribute]) || !isset($_POST[$params['group']][$model->get_name()][$attribute]) && isset($_POST[$params['group']][$model->get_name()]['_checkbox_exists_' . $attribute]) || !isset($_POST[$model->get_name()][$params['number']][$attribute]) && isset($_POST[$model->get_name()][$params['number']]['_checkbox_exists_' . $attribute]) || !isset($_SESSION['_controller_data'][$model->get_name()][$attribute]) && isset($_SESSION['_controller_data'][$model->get_name()]['_checkbox_exists_' . $attribute]))) {
                    $data['attrs']['value'] = 'f';
                } else {
                    if (isset($_POST[$model->get_name()][$attribute])) {
                        $data['attrs']['value'] = $_POST[$model->get_name()][$attribute];
                    } elseif (!empty($params['group']) && isset($_POST[$params['group']][$model->get_name()][$attribute])) {
                        $data['attrs']['value'] = $_POST[$params['group']][$model->get_name()][$attribute];
                    } elseif (!empty($params['number']) && isset($_POST[$model->get_name()][$params['number']][$attribute])) {
                        $data['attrs']['value'] = $_POST[$model->get_name()][$params['number']][$attribute];
                    } elseif (isset($_SESSION['_controller_data'][$model->get_name()][$attribute])) {
                        $data['attrs']['value'] = $_SESSION['_controller_data'][$model->get_name()][$attribute];
                    } elseif ($model->isLoaded()) {
                        $data['attrs']['value'] = $model->{$attribute};
                    }
                }
                if ((empty($data['attrs']['value']) || $params['type'] == 'radio') && $field->has_default == 1 && $field->name != $model->idField && !$model->isLoaded()) {
                    $data['attrs']['value'] = $field->default_value;
                }
                if ((!isset($data['attrs']['value']) || empty($data['attrs']['value'])) && isset($params['value'])) {
                    $data['attrs']['value'] = $params['value'];
                }
            } else {
                if (!empty($params['group'])) {
                    $data['attrs']['name'] = $params['group'] . '[' . $model->get_name() . '][' . $attribute . ']';
                    if (isset($_POST[$params['group']][$model->get_name()][$attribute])) {
                        $data['attrs']['value'] = $_POST[$params['group']][$model->get_name()][$attribute];
                    }
                } elseif (isset($params['number'])) {
                    $data['attrs']['name'] = $model->get_name() . '[' . $params['number'] . '][' . $attribute . ']';
                    if (isset($_POST[$model->get_name()][$params['number']][$attribute])) {
                        $data['attrs']['value'] = $_POST[$model->get_name()][$params['number']][$attribute];
                    }
                } else {
                    $data['attrs']['name'] = $model->get_name() . '[' . $attribute . ']';
                    if (isset($_POST[$model->get_name()][$attribute])) {
                        $data['attrs']['value'] = $_POST[$model->get_name()][$attribute];
                    }
                }
                $data['attrs']['id'] = $model->get_name() . '_' . $attribute . $rowid;
            }
        }
    } else {
        if (!empty($params['group'])) {
            $data['attrs']['name'] = $params['group'] . '[' . $attribute . ']';
        } elseif (isset($params['number'])) {
            $data['attrs']['name'] = $attribute . '[' . $params['number'] . ']';
        } else {
            $data['attrs']['name'] = $attribute;
        }
        $data['attrs']['id'] = $attribute . $rowid;
    }
    // set field (data) attribute
    if (isset($field) && !empty($field)) {
        $data['attrs']['data-field'] = $field->name;
    } else {
        $data['attrs']['data-field'] = $attribute;
    }
    // set row number (data) attribute
    if (isset($params['rowid']) && !empty($params['rowid'])) {
        $data['attrs']['data-row-number'] = $params['rowid'];
    }
    if (strpos($attribute, 'confirmation_') === 0) {
        if (empty($label)) {
            $data['label']['value'] = prettify($attribute);
        }
        $data['attrs']['class'][] = 'confirmation';
    }
    if ($field->not_null == 1 && $params['type'] !== 'checkbox' && empty($params['alias']) && !isset($params['readonly'])) {
        $data['label']['value'] .= ' *';
        $data['attrs']['class'][] = 'required';
    }
    if (isset($params['readonly']) && $params['readonly'] !== FALSE) {
        $data['attrs']['readonly'] = 'readonly';
    }
    if (!empty($params['class']) && $params['class'] !== 'compulsory') {
        $data['attrs']['class'][] = $params['class'];
    }
    if (isset($params['id'])) {
        $data['attrs']['id'] = $params['id'];
    }
    if (isset($params['name'])) {
        $data['attrs']['name'] = $params['name'];
    }
    if (!empty($params['type'])) {
        $data['attrs']['type'] = $params['type'];
    }
    switch ($params['type']) {
        case 'hidden':
            $data['display_tags'] = FALSE;
            $data['display_label'] = FALSE;
            $data['attrs']['type'] = 'hidden';
            break;
        case 'checkbox':
            if (in_array($data['attrs']['value'], array('t', 'true', 'on'))) {
                $data['attrs']['checked'] = 'checked';
            }
            if (isset($params['disabled']) && $params['disabled']) {
                $data['attrs']['disabled'] = 'disabled';
            }
            $data['attrs']['value'] = 'on';
            $data['attrs']['class'][] = 'checkbox';
            $data['attrs_checkbox']['name'] = str_replace('[' . $attribute, '[_checkbox_exists_' . $attribute, $data['attrs']['name']);
            $data['attrs_checkbox']['value'] = 'true';
            $data['attrs_checkbox']['type'] = 'hidden';
            break;
        case 'date':
            if (is_numeric($data['attrs']['value'])) {
                $data['attrs']['value'] = date(DATE_FORMAT, $data['attrs']['value']);
            } else {
                if (!empty($data['attrs']['value'])) {
                    $modelvalue = $data['attrs']['value'];
                } else {
                    $modelvalue = $model->{$attribute};
                }
                if (!empty($modelvalue)) {
                    if (strpos($modelvalue, '/')) {
                        $data['attrs']['value'] = $modelvalue;
                    } else {
                        $data['attrs']['value'] = date(DATE_FORMAT, strtotime($modelvalue));
                    }
                }
            }
            $data['attrs']['type'] = 'text';
            $data['attrs']['class'][] = 'icon date slim datefield';
            break;
        case 'datetime':
            if (is_numeric($data['attrs']['value'])) {
                if ($field->type === 'timestamp') {
                    $data['attrs']['value'] = date(DATE_TIME_FORMAT, $data['attrs']['value']);
                } else {
                    $data['attrs']['value'] = date(DATE_FORMAT, $data['attrs']['value']);
                }
            }
            $data['attrs']['type'] = 'text';
            $data['attrs']['class'][] = 'datetimefield';
            break;
        case 'file':
            $data['attrs']['class'][] = 'file';
            break;
        case 'radio':
            // $data['attrs']['value'] should either be the field default (new) or the field value (edit)
            // check this against the $params['value'] and set checked if match
            if ($data['attrs']['value'] == $params['value'] || $data['attrs']['value'] == ($params['value'] == 't')) {
                $data['attrs']['checked'] = '';
            }
            // The actual value for the radio button is the $params['value']
            if (isset($params['value'])) {
                $data['attrs']['value'] = $params['value'];
            }
            $data['attrs']['class'][] = $attribute;
        default:
            $data['attrs']['value'] = h(trim($data['attrs']['value']), ENT_QUOTES);
            break;
    }
    $data['label']['attrs']['for'] = $data['attrs']['id'];
    $data['label']['attrs']['id'] = $data['attrs']['id'] . '_label';
    // convert attrs array to a string
    if (isset($data['attrs_checkbox'])) {
        $data['attrs_checkbox'] = build_attribute_string($data['attrs_checkbox']);
    }
    $data['attrs'] = build_attribute_string($data['attrs']);
    $data['label']['attrs'] = build_attribute_string($data['label']['attrs']);
    // fetch smarty plugin template
    return smarty_plugin_template($smarty, $data, 'function.input');
}
Ejemplo n.º 15
0
function smarty_function_select($params, &$smarty)
{
    // set a few variables
    $attribute = $params['attribute'];
    $fallback = TRUE;
    $use_autocomplete = FALSE;
    $rowid = '';
    $selected = '';
    $opt_counter = 0;
    $data = array('select' => array('attrs' => array()), 'display_tags' => !(isset($params['tags']) && $params['tags'] == 'none'));
    // append any data attributes passed in through params with the attrs array
    $data['select']['attrs'] += build_data_attributes($params);
    $controller_data =& $smarty->getTemplateVars('controller_data');
    if (empty($params['force']) && (isset($controller_data[$attribute]) || !empty($params['hidden']))) {
        if (isset($controller_data['dialog'])) {
            // force select to be hidden on a dialog
            $params['hidden'] = '';
        }
        return smarty_function_input($params, $smarty);
    }
    $with =& $smarty->getTemplateVars('with');
    // not empty = TRUE, empty = FALSE
    $use_collection = !empty($params['use_collection']);
    if (!empty($params['model'])) {
        $model =& $params['model'];
    } else {
        $model = $with['model'];
    }
    $field = $model->getField($attribute);
    $cc = new ConstraintChain();
    if (!empty($params['constraint'])) {
        $constraint = $params['constraint'];
        if (class_exists($constraint . 'Constraint')) {
            $cname = $constraint . 'Constraint';
            $constraint = new $cname($attribute);
            if (!$constraint instanceof Constraint) {
                throw new Exception($cname . ' is not a valid Constraint');
            }
        } else {
            $exp = explode(',', $constraint);
            $constraint = new Constraint($exp[0], $exp[1], $exp[2]);
        }
        $cc->add($constraint);
    }
    if (empty($params['alias'])) {
        $params['alias'] = isset($with['alias']) ? $with['alias'] : '';
    }
    if (!empty($params['alias'])) {
        $alias = $model->getAlias($params['alias']);
        $aliasModelName = $alias['modelName'];
        $newmodel = new $aliasModelName();
        $name = $model->get_name() . '[' . $params['alias'] . '][' . $attribute . ']';
        $id = $model->get_name() . '_' . $params['alias'] . '_' . $attribute;
        if (isset($_POST[$model->get_name()][$params['alias']][$attribute])) {
            $value = $_POST[$model->get_name()][$params['alias']][$attribute];
        } elseif ($model->isLoaded()) {
            $newmodel = $model->{$params}['alias'];
            $value = $newmodel->{$attribute};
        }
        $model = $newmodel;
    }
    if (empty($params['composite'])) {
        $params['composite'] = isset($with['composite']) ? $with['composite'] : '';
        // DEFAULT
    }
    if (!empty($params['composite'])) {
        $alias = $model->getComposite($params['composite']);
        $aliasModelName = $alias['modelName'];
        $newmodel = new $aliasModelName();
        $name = $model->get_name() . '[' . $aliasModelName . '][' . $attribute . ']';
        $id = $model->get_name() . '_' . $aliasModelName . '_' . $attribute;
        if (isset($_POST[$model->get_name()][$aliasModelName][$attribute])) {
            $value = $_POST[$model->get_name()][$aliasModelName][$attribute];
        } elseif ($model->isLoaded()) {
            $newmodel = $model->{$params}['composite'];
            $value = $newmodel->{$attribute};
        }
        $model = $newmodel;
    }
    if (isset($params['options'])) {
        $get_options = $params['options'];
    } else {
        $get_options = '';
    }
    if (!isset($params['autocomplete']) || empty($get_options)) {
        $use_autocomplete = false;
    } else {
        $use_autocomplete = true;
    }
    if (!is_null($field->options)) {
        $get_options = $field->options->_data;
        $use_autocomplete = $field->options->_autocomplete;
        $text_value = $model->{$model->belongsToField[$attribute]};
        if (trim($text_value) == '' && count($get_options) > 0) {
            $text_value = current($get_options);
        }
        $selected = key($get_options);
    }
    if (!empty($params['label'])) {
        $data['select']['label'] = $params['label'];
    } else {
        $data['select']['label'] = $field->tag;
    }
    if (isset($params['number'])) {
        $name = $model->get_name() . '[' . $params['number'] . '][' . $attribute . ']';
    } elseif (!isset($name)) {
        $name = $model->get_name() . '[' . $attribute . ']';
    }
    if (isset($params['rowid']) && !empty($params['rowid'])) {
        $rowid = $params['rowid'];
        $data['select']['attrs']['data-row-number'] = $params['rowid'];
    }
    $id = $model->get_name() . '_' . $attribute . $rowid;
    if (!isset($name)) {
        $name = $model->get_name() . '[' . $attribute . ']';
        $id = $model->get_name() . '_' . $attribute . $rowid;
    }
    if (isset($params['postfix'])) {
        $name .= $params['postfix'];
    }
    // set field (data) attribute
    if (isset($field) && !empty($field)) {
        $data['select']['attrs']['data-field'] = $field->name;
    } else {
        $data['select']['attrs']['data-field'] = $attribute;
    }
    if (!empty($params['depends'])) {
        $depends = explode(',', $params['depends']);
    } elseif (!is_null($field->options->_depends)) {
        $depends = array_keys($field->options->_depends);
    } else {
        $depends = '';
    }
    //    echo 'Smarty function.select depends='.$depends.'<br>';
    if (!empty($params['constrains'])) {
        $affects = explode(',', $params['constrains']);
        $constrains = true;
    } elseif (!is_null($field->options->_affects)) {
        $affects = array_keys($field->options->_affects);
        $constrains = true;
    } else {
        $affects = '';
        $constrains = false;
    }
    // set the selected value from the value of the field, if present
    if ($model->isLoaded()) {
        $selected = $model->{$attribute};
    } elseif (isset($controller_data[$attribute])) {
        $selected = $controller_data[$attribute];
    }
    // set the selected value from the params value
    // can override the value of the field, if present
    if (isset($params['value'])) {
        $selected = $params['value'];
    }
    $autocomplete_select_limit = get_config('AUTOCOMPLETE_SELECT_LIMIT');
    if (!empty($get_options) && !isset($params['forceselect']) && count($get_options) > $autocomplete_select_limit) {
        $use_autocomplete = true;
    }
    if (isset($model->belongsToField[$attribute])) {
        $x = $model->belongsTo[$model->belongsToField[$attribute]]["model"];
        $controllername = strtolower($x) . 's';
        if (isset($_SESSION['cache']['select'][$controllername])) {
            $data['select']['fk_link']['module'] = $_SESSION['cache']['select'][$controllername];
        } else {
            $component = new ModuleComponent();
            $component->loadBy(array('name', 'type'), array($controllername . 'controller', 'C'));
            if ($component->isLoaded()) {
                $data['select']['fk_link']['module'] = $_SESSION['cache']['select'][$controllername] = $component->module_name;
            }
        }
        if (isset($data['select']['fk_link']['module'])) {
            $data['select']['fk_link']['controller'] = $controllername;
            $data['select']['fk_link']['action'] = 'new';
        }
        if (isset($params['data'])) {
            if ($params['data'] instanceof DataObjectCollection) {
                $options = $params['data']->getAssoc();
            } elseif (is_array($params['data'])) {
                $options = $params['data'];
            } else {
                throw new Exception('"data" paramater should be an associative array, or a DataObjectCollection');
            }
        } elseif (empty($get_options)) {
            if ($model->belongsTo[$model->belongsToField[$attribute]]["cc"] instanceof ConstraintChain) {
                $cc->add($model->belongsTo[$model->belongsToField[$attribute]]["cc"]);
            }
            if (!empty($depends)) {
                foreach ($depends as $depends_field) {
                    if (!is_null($model->{$depends_field})) {
                        $cc->add(new Constraint($depends_field, '=', $model->{$depends_field}));
                    }
                }
            }
            $model->belongsTo[$model->belongsToField[$attribute]]["cc"] = $cc;
            if (!isset($params['forceselect']) && $model->getOptionsCount($attribute) > $autocomplete_select_limit) {
                $use_autocomplete = TRUE;
                $text_value = $model->{$model->belongsToField[$attribute]};
                if (trim($text_value) == '') {
                    if (empty($selected) && !$model->isLoaded() && $field->has_default) {
                        $selected = $field->default_value;
                    }
                    $temp = new $x();
                    $temp->load($selected);
                    $text_value = $temp->getIdentifierValue();
                }
            } else {
                $x = DataObjectFactory::Factory($x);
                if ($model->checkUniqueness($attribute)) {
                    // TODO: this can be inefficient in large data sets
                    //		 needs to be 'not exists' correlated subquery
                    $cc->add(new Constraint($x->idField, 'NOT IN', '(SELECT ' . $attribute . ' FROM ' . $model->getTableName() . ')'));
                    $options = $x->getAll($cc, TRUE, $use_collection);
                    $fallback = FALSE;
                } elseif ($attribute == 'assigned') {
                    $c_user = $smarty->getTemplateVars('current_user');
                    $cc = new ConstraintChain();
                    $db = DB::Instance();
                    if (!is_null($c_user->person_id)) {
                        $cc->add(new Constraint('person_id', 'IN', '(SELECT px.id FROM person px JOIN company cx ON (px.company_id=cx.id)  JOIN person pz ON (pz.company_id=cx.id) WHERE pz.id=' . $db->qstr($c_user->person_id) . ')'));
                    }
                    $options = $x->getAll($cc, TRUE, $use_collection);
                } elseif (get_class($x) == 'User') {
                    $options = $x->getActive($cc, FALSE);
                } else {
                    $options = $x->getAll($cc, FALSE, $use_collection);
                }
            }
        }
    } elseif ($model->hasParentRelationship($attribute) && !isset($params['ignore_parent_rel'])) {
        $db = DB::Instance();
        $x = clone $model;
        if ($model->isLoaded()) {
            $cc->add(new Constraint($model->idField, '<>', $model->{$model->idField}));
        }
        $options = $x->getAll($cc, FALSE, $use_collection);
    } elseif ($model->isEnum($attribute)) {
        //enumeration
        $options = $model->getEnumOptions($attribute);
        foreach ($options as $key => $option) {
            if ($selected == $option) {
                $selected = $key;
                break;
            }
        }
    }
    if ($field->not_null == 1) {
        $data['select']['label'] .= ' *';
        $data['select']['class'][] = 'required';
    }
    if (isset($_POST[$model->get_name()][$attribute])) {
        $selected = $_POST[$model->get_name()][$attribute];
    } elseif (isset($_SESSION['_controller_data'][get_class($model)][$attribute])) {
        $selected = $_SESSION['_controller_data'][get_class($model)][$attribute];
    } elseif (isset($_POST[$model->get_name()][$params['number']])) {
        $selected = $_POST[$model->get_name()][$params['number']][$attribute];
    }
    if (empty($selected) && $field->has_default && !$model->isLoaded()) {
        $selected = $field->default_value;
    }
    if (isset($params['nonone']) && $params['nonone'] == 'true' || !is_null($field->options->_nonone) && $field->options->_nonone) {
        $data['select']['class'][] = "nonone";
    }
    if (!empty($depends)) {
        $data['select']['attrs']['data-depends'] = htmlspecialchars(json_encode($depends));
    }
    if (!empty($affects)) {
        $data['select']['attrs']['data-constrains'] = htmlspecialchars(json_encode($affects));
    }
    //***************************
    //  COMPILE SELECT ATTRIBUTES
    $data['select']['attrs']['name'] = $name;
    $data['select']['attrs']['id'] = $id;
    if (isset($data['select']['fk_link'])) {
        $data['select']['id'] = $id;
    }
    // join the select class items with a space and pop them in the attrs class item
    if (!empty($data['select']['class'])) {
        $data['select']['attrs']['class'] = implode(' ', $data['select']['class']);
    }
    // append any class passed in from params
    if (!empty($params['class'])) {
        $data['select']['attrs']['class'] .= ' ' . $params['class'];
    }
    // For new records, initialise the model attribute with the selected value
    if (!empty($selected)) {
        $model->{$attribute} = $selected;
    }
    if (!$use_autocomplete || isset($params['multiple']) && $params['multiple']) {
        if ($constrains) {
            $data['select']['attrs']['class'] .= ' uz-constrains';
        }
        if (isset($params['multiple']) && $params['multiple']) {
            $data['dd']['attrs']['class'] = 'for_multiple';
            $data['select']['id'] = $id;
            $data['select']['attrs']['name'] = $data['select']['attrs']['name'] . '[]';
            $data['select']['attrs']['multiple'] = 'multiple';
        }
        if (isset($params['onchange'])) {
            $data['select']['attrs']['onchange'] = $params['onchange'];
        }
        if (isset($params['size'])) {
            $data['select']['attrs']['size'] = $params['size'];
        }
        if (isset($params['disabled'])) {
            $data['select']['attrs']['disabled'] = 'disabled';
        }
        // for the sake of it, trim the class string
        $data['select']['attrs']['class'] = trim($data['select']['attrs']['class']);
        // build the attribute string
        // $data['select']['attrs'] is no longer an array!!!
        $data['select']['attrs'] = build_attribute_string($data['select']['attrs']);
        //*****************
        //  COMPILE OPTIONS
        // check whether required field
        if (!$field->not_null == 1 && (!isset($params['nonone']) || $params['nonone'] != 'true') && (is_null($field->options->_nonone) || !$field->options->_nonone)) {
            $opt_counter++;
            $option_attrs = array('value' => '');
            if (empty($selected)) {
                $option_attrs['selected'] = 'selected';
                $selected = 'None';
            }
            $data['select']['options'][$opt_counter]['attrs'] = build_attribute_string($option_attrs);
            $data['select']['options'][$opt_counter]['value'] = 'None';
        }
        if (isset($params['start'])) {
            $opt_counter++;
            $option_attrs = array('value' => '');
            if (empty($selected)) {
                $option_attrs['selected'] = 'selected';
                $selected = $params['start'];
            }
            $data['select']['options'][$opt_counter]['attrs'] = build_attribute_string($option_attrs);
            $data['select']['options'][$opt_counter]['value'] = $params['start'];
        }
        // fallback is a horrible hack for now (for uniqueness constraints on dropdowns)
        if ($fallback && is_array($get_options)) {
            $options = $get_options;
        }
        if (!empty($options)) {
            foreach ($options as $key => $value) {
                $option_attrs = array();
                $opt_counter++;
                $option_attrs['value'] = h($key, ENT_COMPAT);
                if (is_array($selected) && in_array($key, $selected) || $selected == $key) {
                    $option_attrs['selected'] = 'selected';
                }
                $data['select']['options'][$opt_counter]['attrs'] = build_attribute_string($option_attrs);
                $data['select']['options'][$opt_counter]['value'] = h($value);
                $data['autocomplete'] = false;
            }
        }
        $data['dt']['attrs'] = empty($data['dt']['attrs']) ? '' : build_attribute_string($data['dt']['attrs']);
        $data['dd']['attrs'] = empty($data['dd']['attrs']) ? '' : build_attribute_string($data['dd']['attrs']);
    } elseif (is_null($field->options) && !empty($get_options)) {
        if (empty($selected)) {
            $selected = key($get_options);
        }
        $data['autocomplete'] = true;
        $data['data_inline'] = true;
        $data['select']['selected'] = $selected;
        $data['select']['value'] = $get_options[$selected];
        $data['select']['options'] = json_encode(dataObject::toJSONArray($get_options));
        $data['select']['attrs']['class'] = 'uz-autocomplete ui-autocomplete-input icon ' . $data['select']['attrs']['class'];
    } else {
        $data['autocomplete'] = true;
        $data['data_inline'] = false;
        if (isset($params['action'])) {
            $data['select']['attrs']['data-action'] = $params['action'];
        } elseif (!is_null($field->options)) {
            $data['select']['attrs']['data-action'] = $field->options->_action;
        } else {
            $data['select']['attrs']['data-action'] = 'getOptions';
        }
        if (!empty($params['identifierfield'])) {
            $data['select']['attrs']['data-identifierfield'] = json_encode(explode(',', $params['identifierfield']));
        } elseif (!is_null($field->options->_identifierfield)) {
            $data['select']['attrs']['data-identifierfield'] = json_encode(array_keys($field->options->_identifierfield));
        } else {
            $data['select']['attrs']['data-identifierfield'] = '""';
        }
        if (!empty($params['use_collection'])) {
            $data['select']['attrs']['data-use_collection'] = $params['use_collection'] ? 'true' : 'false';
        } elseif (!is_null($field->options->_use_collection)) {
            $data['select']['attrs']['data-use_collection'] = $field->options->_use_collection ? 'true' : 'false';
        } else {
            $data['select']['attrs']['data-use_collection'] = '""';
        }
        if (empty($text_value) && !empty($selected)) {
            $text_value = $selected;
        }
        $text_attribute = $attribute . '_text';
        $text_name = str_replace($attribute, $text_attribute, $name);
        if (isset($_POST[$model->get_name()][$text_attribute])) {
            $text_value = $_POST[$model->get_name()][$text_attribute];
        } elseif (isset($_SESSION['_controller_data'][get_class($model)][$text_attribute])) {
            $text_value = $_SESSION['_controller_data'][get_class($model)][$text_attribute];
        } elseif (isset($_POST[$model->get_name()][$params['number']])) {
            $text_value = $_POST[$model->get_name()][$params['number']][$text_attribute];
        }
        $data['select']['attrs']['data-attribute'] = $attribute;
        $data['select']['attrs']['class'] = 'uz-autocomplete ui-autocomplete-input icon ' . $data['select']['attrs']['class'];
        $attrs = array('name' => $data['select']['attrs']['name'], 'id' => $data['select']['attrs']['id'], 'value' => h($selected, ENT_COMPAT), 'data-text_id' => $data['select']['attrs']['id'] . '_text');
        if (isset($data['select']['attrs']['data-depends'])) {
            $attrs['data-depends'] = $data['select']['attrs']['data-depends'];
        }
        if (isset($data['select']['attrs']['data-constrains'])) {
            $attrs['data-constrains'] = $data['select']['attrs']['data-constrains'];
            unset($data['select']['attrs']['data-constrains']);
        }
        if ($constrains) {
            $attrs['class'] = '"uz-constrains"';
        }
        $data['select']['attrs']['data-id'] = $data['select']['attrs']['id'];
        $data['select']['attrs']['value'] = $text_value;
        $data['select']['attrs']['id'] = $data['select']['attrs']['id'] . '_text';
        $data['select']['attrs']['name'] = $text_name;
        $data['select']['attrs_text'] = build_attribute_string($data['select']['attrs']);
        $data['select']['attrs'] = build_attribute_string($attrs);
    }
    // this should be further up?
    if (prettify($params['attribute']) == 'EGS_HIDDEN_FIELD') {
        return '';
    }
    // fetch smarty plugin template
    return smarty_plugin_template($smarty, $data, 'function.select');
}