/** * Get form for model prefilled with values for specified instance. * * @param string $modelClass * @param mixed $instance * @param array $options * @return Curry_Form_ModelForm */ public function getEditForm($modelClass, $instance = null, $options = array()) { $options = array_merge(array('action' => (string) url('', $_GET), 'method' => 'post', 'class' => isAjax() ? 'dialog-form' : ''), $options); $form = new \Curry_Form_ModelForm($modelClass, $options); if ($instance !== null) { $form->fillForm($instance); } $form->addElement('submit', 'save', array('label' => 'Save')); return $form; }
/** * Save user. * * @param User $user * @param Curry_Form_ModelForm $form */ protected function saveUser(User $user, Curry_Form_ModelForm $form) { $values = $form->getValues(); $password = $values['password']; if ($password || $user->isNew()) { $user->setPlainPassword($password); } $form->removeElement('password'); $form->fillModel($user); $user->save(); $home = self::getUserHome($user, true); if ($values['create_home_folder']) { $folder = Curry_Core::$config->curry->wwwPath . DIRECTORY_SEPARATOR; $folder .= str_replace('/', DIRECTORY_SEPARATOR, rtrim($home->getPath(), '/')); if (!file_exists($folder)) { @mkdir($folder, 0777, true); } if ($home->isNew()) { $home->setWrite(true); $home->save(); } } else { if ($home && !$home->isNew()) { $home->delete(); } } }
/** * Get form from model. * * @param BaseObject $row * @return Curry_Form_ModelForm */ public static function getRowForm(BaseObject $row) { $form = new Curry_Form_ModelForm($_GET['table'], array('ignorePks' => false, 'ignoreFks' => false, 'action' => url('', array("module", "view", "table", "pk")), 'method' => 'post', 'class' => 'dialog-form')); $form->fillForm($row); $displayGroup = 1; foreach ($form->getElementColumns() as $column) { $element = $form->getElement(strtolower($column->getName())); if (!$element) { continue; } // Remove column if it's auto generated if ($row->isNew() && $column->isPrimaryKey() && $column->getTable()->isUseIdGenerator()) { $form->removeElement($element->getName()); continue; } // Make sure we have an option for foreign keys if ($column->isForeignKey() && !$row->isNew()) { $related = $element->getMultioptions(); if (!array_key_exists($element->getValue(), $related)) { $related[$element->getValue()] = $element->getValue(); $element->setMultioptions($related); } } // Add null checkbox if (!$column->isNotNull()) { $isNull = $element->getValue() === null; if (!$column->isNotNull()) { $name = $element->getName() . '__null__'; $form->addElement('checkbox', $name, array('label' => 'Null', 'title' => 'Is Null?', 'class' => 'trigger-change', 'onchange' => "document.getElementById('" . $element->getId() . "').disabled = this.checked;", 'value' => $isNull)); $form->addDisplayGroup(array($element->getName(), $name), 'displayGroup' . $displayGroup++, array('class' => 'horizontal-group', 'legend' => $element->getLabel())); } } } $form->addElement('submit', 'save', array('label' => $row->isNew() ? 'Insert' : 'Save')); return $form; }