2014-08-26 04:47:07 +08:00
|
|
|
<?php
|
|
|
|
App::uses('Component', 'Controller');
|
|
|
|
|
|
|
|
class ConfigParserComponent extends Component {
|
|
|
|
public function parseHints($hint) {
|
|
|
|
$string = "";
|
|
|
|
$hints = explode('|', $hint);
|
|
|
|
foreach ($hints as $hint) {
|
|
|
|
$string .= "<option value=\"$hint\">$hint</option>";
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2014-09-25 01:24:53 +08:00
|
|
|
public function getLabel($name) {
|
|
|
|
$width = 'col-md-4';
|
|
|
|
|
|
|
|
$string = '<div class="form-group">';
|
|
|
|
$string .= '<label for="%s" class="control-label %s">%s</label>';
|
|
|
|
$label = sprintf($string, $name, $width, $name);
|
|
|
|
$label .= '<div class="col-md-6">';
|
|
|
|
|
|
|
|
return $label;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInput($name, $type, $id) {
|
|
|
|
if ($type == 'checkbox') {
|
|
|
|
$string = '<input id="%s" type="checkbox" ng-change="updateConfig(\'%s\', \'%s\')" ng-model="myModel.configData[\'%s\']" ng-true-value="1" ng-false-value="0"><span class="form-control-feedback"></span></div>';
|
|
|
|
} elseif ($type == 'text') {
|
|
|
|
$string = '<input id="%s" type="text" ng-change="updateConfig(\'%s\', \'%s\')" ng-model="myModel.configData[\'%s\']" class="form-control"><span class="form-control-feedback"></span></div>';
|
|
|
|
} elseif ($type == 'textarea') {
|
|
|
|
$string = '<textarea id="%s" ng-change="updateConfig(\'%s\', \'%s\')" class="form-control" rows="3" ng-model="myModel.configData[\'%s\']"></textarea><span class="form-control-feedback"></span></div>';
|
|
|
|
} elseif ($type == 'select') {
|
|
|
|
$string = '<select id="%s" ng-change="updateConfig(\'%s\', \'%s\')" class="form-control" ng-model="myModel.configData[\'%s\']">';
|
|
|
|
}
|
|
|
|
|
|
|
|
$input = sprintf($string, $name, $id, $name, $name);
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
2014-08-26 04:47:07 +08:00
|
|
|
public function parseOptions($configs) {
|
2014-09-22 22:59:04 +08:00
|
|
|
$category = $configs[0]['Config']['Category'];
|
2014-08-26 04:47:07 +08:00
|
|
|
$string = "";
|
2014-09-22 22:59:04 +08:00
|
|
|
|
2014-08-26 04:47:07 +08:00
|
|
|
foreach ($configs as $option) {
|
|
|
|
|
|
|
|
$type = $option['Config']['Type'];
|
|
|
|
$name = $option['Config']['Name'];
|
|
|
|
$value = $option['Config']['Value'];
|
|
|
|
$hint = $option['Config']['Hint'];
|
2014-09-22 22:59:04 +08:00
|
|
|
$id = $option['Config']['Id'];
|
2014-08-26 04:47:07 +08:00
|
|
|
|
2014-09-25 01:24:53 +08:00
|
|
|
$string .= $this->getLabel($name);
|
|
|
|
|
2014-08-26 04:47:07 +08:00
|
|
|
switch ($type) {
|
|
|
|
case "boolean":
|
2014-09-25 01:24:53 +08:00
|
|
|
$string .= $this->getInput($name, 'checkbox', $id);
|
2014-08-26 04:47:07 +08:00
|
|
|
break;
|
|
|
|
case "text":
|
2014-09-25 01:24:53 +08:00
|
|
|
$string .= $this->getInput($name, 'textarea', $id);
|
2014-08-26 04:47:07 +08:00
|
|
|
break;
|
|
|
|
case "string":
|
|
|
|
if (strpos($hint, '|') === FALSE) {
|
2014-09-25 01:24:53 +08:00
|
|
|
$string .= $this->getInput($name, 'text', $id);
|
2014-08-26 04:47:07 +08:00
|
|
|
} else {
|
2014-09-25 01:24:53 +08:00
|
|
|
$string .= $this->getInput($name, 'select', $id);
|
2014-08-26 04:47:07 +08:00
|
|
|
$string .= $this->parseHints($hint);
|
2014-09-25 01:24:53 +08:00
|
|
|
$string .= '</select> <span class="form-control-feedback"></span> </div>';
|
2014-08-26 04:47:07 +08:00
|
|
|
}
|
|
|
|
break;
|
2014-09-22 22:59:04 +08:00
|
|
|
default:
|
2014-09-25 01:24:53 +08:00
|
|
|
$string .= $this->getInput($name, 'text', $id);
|
2014-08-26 04:47:07 +08:00
|
|
|
}
|
2014-09-25 01:24:53 +08:00
|
|
|
$string .= "</div><!-- End .form-group -->\n";
|
2014-08-26 04:47:07 +08:00
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
2014-09-22 22:59:04 +08:00
|
|
|
|
|
|
|
|
2014-08-26 04:47:07 +08:00
|
|
|
}
|
|
|
|
?>
|