getPeer(); if (! method_exists($peer, "getTableMap")) throw new sfException("Object Peer must have getTableMap method."); $this->formname = $name; $this->actionurl = $url; $this->tablemap = $peer->getTableMap(); $schemainfo = sfYaml::load(sfConfig::get('sf_root_dir') . "/config/schema.yml"); $this->schemainfo = $schemainfo['propel'][$this->tablemap->getName()]; $this->obj = $obj; $this->columns = $this->tablemap->getColumns(); $key = array_keys($this->columns); $this->mapcounter = $key[0]; $this->indexcounter = 0; $this->jsvalidation = true; $this->reload = true; $this->MultiPart = false; } public function getActionURL() { return $this->actionurl; } public function getFormName() { if (! $this->formname) $this->formname = "form".rand(1,1000); return $this->formname; } /** * Set the mode of the builder. */ public function setMode($mode) { if ($mode == sfDynamicsFormBuilder::DETAIL_VIEW) $this->mode = sfDynamicsFormBuilder::DETAIL_VIEW; else if ($mode == sfDynamicsFormBuilder::EDIT_VIEW) $this->mode = sfDynamicsFormBuilder::EDIT_VIEW; } /** * Load value from Request object, automatically. * true, by default. * @param bool */ public function setLoadFromRequest($reload) { $this->reload = $reload; } /** * Important method to call. After all the parameter have been set and ready to go. */ public function init() { /** @var ColumnMap **/ $c = null; foreach ($this->columns as $k => $c) { if (isset($this->schemainfo[strtolower($c->getColumnName())]['f_skip']) && ($this->schemainfo[strtolower($c->getColumnName())]['f_skip'] === false)) continue; if ( ($c->isForeignKey() && ! isset($this->schemainfo[strtolower($c->getColumnName())]['displayValue'])) || (isset($this->schemainfo[strtolower($c->getColumnName())]['f_skip']) && $this->schemainfo[strtolower($c->getColumnName())]['f_skip']) || ($c->getPhpName() == "CreatedAt") || ($c->getPhpName() == "UpdatedAt") || (is_array($this->skiplist) && ( array_search($c->getFullyQualifiedName(), $this->skiplist) || array_search($c->getPhpName(), $this->skiplist) || array_search($c->getColumnName(), $this->skiplist) ) ) ) unset($this->columns[$k]); if ( isset($this->schemainfo[strtolower($c->getColumnName())]['f_display_type']) && ($this->schemainfo[strtolower($c->getColumnName())]['f_display_type'] == "image" || $this->schemainfo[strtolower($c->getColumnName())]['f_display_type'] == "file") ) $this->MultiPart = true; } } /** * Set the field to skip. * @param array of fieldname value (BasePeer::TYPE_COLNAME) */ public function setSkipField($list) { $this->skiplist = $list; } /** * Get array of validation rule. You will need to convert is to Java Script code, compliance with JSON. * @return array of validation rule. */ public function getAllValidateJSCode() { $returnvalue = array(); if (! $this->isJSValidateEnaled()) return $returnvalue; foreach ($this->columns as $c=>$v) { $name = $v->getPhpName(); if ($v = $this->getValidateJS(strtolower($c))) $returnvalue[$this->formname][] = array("name"=>$name, "rule"=>$v); } return $returnvalue; } private function getValidateJS($columname) { if (isset($this->validaterule) && isset($this->validaterule[$columname])) { $ruletable = array(); $rule = $this->validaterule[$columname]; if (isset($rule)) { foreach ($rule as $k=>$value) { switch ($k) { case "required": case "sfStringValidator": case "sfNumberValidator": case "sfEmailValidator": case "sfUrlValidator": case "sfRegexValidator": case "sfCompareValidator": case "sfPropelUniqueValidator": case "sfCallbackValidator": $ruletable[$k] = $value; break; default: $ruletable = array_merge($ruletable, $this->getNamedValidator($k)); } } return $ruletable; } } else return FALSE; } private function getNamedValidator($name) { if (! isset($this->validator[$name])) return array(); $v = $this->validator[$name]; $returnvalue[$v['class']] = $v['param']; return $returnvalue; } /** * Enabled Javascript (Client side) Validation. Should be disable for Ajax form. */ public function enableJSValidation() { $this->jsvalidation = true; } /** * Disabled Javascript (Client side) Validation. Should be disable for Ajax form. */ public function disableJSValidation() { $this->jsvalidation = false; } /** * Is it a multipart form? * @return bool */ public function isMultiPartForm() { return $this->MultiPart; } /** * Is the JS Validation Enabled? * @return bool */ public function isJSValidateEnaled() { return $this->jsvalidation; } /** * Set Validation information. This function will use the provided information to construct path to validation file. The validation file is just standard symfony validation file. * @param string Module name * @param string Action name. */ public function setValidationInfo($moduleName, $actionName) { $validateFile = sfConfig::get('sf_app_dir') . '/' . sfConfig::get('sf_app_module_dir_name') . '/' . $moduleName.'/'.sfConfig::get('sf_app_module_validate_dir_name').'/'.$actionName.'.yml'; $this->setValidationFile($validateFile); } /** * Set Validation information by specify the path. The validation file is just standard symfony validation file. * @param string Validation file name. */ public function setValidationFile($filename) { if (! file_exists($filename)) throw new sfConfigurationException($filename . " does not exist."); $validate_array = sfYaml::load($filename); if (isset($validate_array['fields'])) $this->validaterule = $validate_array['fields']; if (isset($validate_array['validators'])) $this->validator = $validate_array['validators']; } /** * Is the field going to be visible * @return bool */ public function visible() { /** @var ColumnMap **/ $c = $this->columns[$this->mapcounter]; if ($c->isPrimaryKey()) return FALSE; return TRUE; } /** * Get the label of the field. * @return string */ public function getLabel() { /** @var ColumnMap **/ $c = $this->columns[$this->mapcounter]; if (! $this->visible()) return FALSE; if (isset($this->schemainfo[strtolower($c->getColumnName())]['f_label'])) return $this->schemainfo[strtolower($c->getColumnName())]['f_label']; return ucwords(strtolower(preg_replace('#_#', ' ', $c->getColumnName()))); } /** * Get the column field * @param array optional, html option */ public function getColumnField($html_array = array()) { /** @var ColumnMap **/ $c = $this->columns[$this->mapcounter]; if (! isset($html_array['id'])) $html_array['id'] = $this->formname . "_" . $c->getPhpName(); $fieldname = "get" . $c->getPhpName(); $obj = $this->obj; $value = $obj->$fieldname(); $fieldname = strtolower($c->getColumnName()); /** Get Value from the Request **/ if ($this->reload && sfContext::getInstance()->getRequest()->getParameter($fieldname)) { $value = sfContext::getInstance()->getRequest()->getParameter($fieldname); } foreach (sfMixer::getCallables('sfDynamicsFormBuilder:getColumnField:valueModifier') as $callable) { $value = call_user_func($callable, $c, $value); } /** Special Treatment for Array value **/ if (is_array($value)) { $value_txt = ""; foreach ($value as $v) $value_txt .= $v . ", "; $value = substr($value_txt, 0, -2); } /** Set the css class **/ if (isset($this->schemainfo[$fieldname]['f_class']) && $this->schemainfo[$fieldname]['f_class']) { $html_array['class'] .= ' ' . $this->schemainfo[$fieldname]['f_class']; } /** Readonly Field ? **/ if (isset($this->schemainfo[$fieldname]['f_readonly']) && $this->schemainfo[$fieldname]['f_readonly']) { return "