00001 <?php
00002
00011 class spunQ_FieldGroup extends spunQ_Field implements spunQ_IFieldContainer {
00012
00017 protected $fields = array();
00018
00022 public function show($value = NULL, $template = NULL, $templateParameters = array(), $htmlIdPostFix = '') {
00023 if ($this->useSubmittedValue && $this->container->wasSubmitted() && $this->submittedValueValid()) {
00024 $value = $this->getSubmittedValue();
00025 }
00026 $value = $this->spunQToRaw($value);
00027 if (!is_array($value)) {
00028 throw new spunQ_HtmlException('spunQ.html.form.FieldGroup.spunQToRaw.notArray', spunQ_Type::getDescription($value));
00029 }
00030 $missingKeys = array_diff_key($this->fields, $value);
00031 if (!empty($missingKeys)) {
00032 throw new spunQ_HtmlException('spunQ.html.form.FieldGroup.spunQToRaw.missingKeys', array_keys($missingKeys));
00033 }
00034 if (is_string($template)) {
00035 $templateParameters['value'] = $value;
00036 $templateParameters['field'] = $this;
00037 $templateParameters['htmlIdPostFix'] = $htmlIdPostFix;
00038 template($template, $templateParameters);
00039 } else {
00040 foreach ($this->getFields() as $fieldName => $field) {
00041 $field = $this->getField($fieldName);
00042 $fieldValue = $value[$fieldName];
00043 $fieldTemplate = NULL;
00044 $fieldTemplateParameters = NULL;
00045 if (isset($template[$fieldName])) {
00046 $fieldTemplate = $template[$fieldName];
00047 }
00048 if (isset($templateParameters[$fieldName])) {
00049 $fieldTemplateParameters = $templateParameters[$fieldName];
00050 }
00051 $field->show($fieldValue, $fieldTemplate, $fieldTemplateParameters, $htmlIdPostFix);
00052 echo '<br/>';
00053 }
00054 }
00055 return $this;
00056 }
00057
00062 public function getFields() {
00063 return $this->fields;
00064 }
00065
00072 public function getField($name) {
00073 if (!isset($this->fields[$name])) {
00074 throw new spunQ_HtmlException('spunQ.html.form.FieldDoesNotExist', $this->getName(), $name);
00075 }
00076 return $this->fields[$name];
00077 }
00078
00084 public function __clone() {
00085 foreach ($this->fields as &$field) {
00086 $field = clone($field);
00087 $field->setContainer($this);
00088 }
00089 unset($field);
00090 parent::__clone();
00091 return NULL;
00092 }
00093
00097 public function wasSubmitted() {
00098 return $this->container->wasSubmitted();
00099 }
00100
00104 public function addField(spunQ_IField $field) {
00105 $this->fields[$field->getName()] = $field;
00106 $field->setContainer($this);
00107 return $this;
00108 }
00109
00113 public function removeField(spunQ_IField $field) {
00114 unset($this->fields[$field->getName()]);
00115 return $this;
00116 }
00117
00121 public function getTypeString() {
00122 return $this->getTypePath();
00123 }
00124
00128 public function getFieldHtmlId($field, $postfix) {
00129 return $this->getHtmlId($postfix) . '-' . preg_replace('/[^a-z0-9-]/i', '-', $field->getName());
00130 }
00131
00135 public function getFieldHtmlName($field) {
00136 return $this->getHtmlName() . '[' . $field->getName() . ']';
00137 }
00138
00142 public function submittedValueValid() {
00143 foreach ($this->fields as $field) {
00144 if (!$field->submittedValueValid()) {
00145 return false;
00146 }
00147 }
00148 return parent::submittedValueValid();
00149 }
00150
00154 public function showValidatorsJs() {
00155 foreach ($this->fields as $field) {
00156 $field->showValidatorsJs();
00157 }
00158 return parent::showValidatorsJs();
00159 }
00160
00164 public function getFieldByPath($path) {
00165 if (is_string($path)) {
00166 $path = explode('.', $path);
00167 }
00168 $part = reset($path);
00169 foreach ($this->fields as $field) {
00170 if ($field->getTypePathPart() === $part) {
00171 array_shift($path);
00172 if (empty($path)) {
00173 return $field;
00174 }
00175 return $field->getFieldByPath($path);
00176 }
00177 }
00178 return parent::getFieldByPath($path);
00179 }
00180
00184 public function setUseSubmittedValue($useSubmittedValue = true) {
00185 parent::setUseSubmittedValue($useSubmittedValue);
00186 foreach ($this->fields as $field) {
00187 $field->setUseSubmittedValue($useSubmittedValue);
00188 }
00189 return $this;
00190 }
00191
00195 public function getDisplayValue($originalValue) {
00196 if ($this->getUseSubmittedValue() && $this->container->wasSubmitted()) {
00197 return $this->spunQToRaw($this->getSubmittedValue());
00198 }
00199 return $this->spunQToRaw($originalValue);
00200 }
00201
00205 protected function spunQToRaw($value) {
00206 if ($value === NULL) {
00207 $value = array();
00208 foreach ($this->getFields() as $fieldName => $field) {
00209 $value[$fieldName] = NULL;
00210 }
00211 }
00212 return parent::spunQToRaw($value);
00213 }
00214
00218 protected function rawToSpunQ(&$value) {
00219 if ($value === NULL) {
00220 $value = array();
00221 } elseif (!is_array($value)) {
00222 throw new spunQ_InvalidRawValueException('spunQ.html.form.FieldGroup.processSubmission.NotAnArray', spunQ_Type::getDescription($value));
00223 }
00224 $fieldsSuccess = true;
00225 foreach ($this->fields as $fieldName => $field) {
00226 if (!isset($value[$fieldName])) {
00227 $value[$fieldName] = NULL;
00228 }
00229 $fieldsSuccess = $field->_processSubmission($value[$fieldName]) && $fieldsSuccess;
00230 }
00231 if (!$fieldsSuccess) {
00232 return false;
00233 }
00234 return parent::rawToSpunQ($value);
00235 }
00236
00237 }