diff --git a/README b/README deleted file mode 100644 index c0c7ad2..0000000 --- a/README +++ /dev/null @@ -1 +0,0 @@ -Take a look at README.markdown diff --git a/README.markdown b/README.markdown deleted file mode 100644 index 1606d90..0000000 --- a/README.markdown +++ /dev/null @@ -1,73 +0,0 @@ -# nFormEmbedPlugin - -## Introduction -nFormEmbedPlugin is a [Symfony](http://symfony-project.org/) 1.3/1.4 plugin. Created to ease the embedding and saving of related [Doctrine](http://doctrine-project.org) 1.2 forms. -It's key points are - - * Plain & Easy - * embedRelationAndCreate('RelationName'); - * easy i18n embedding. Just supply the languages. - - -## Setup -Extend nBaseEmbedForm from your BaseFormDoctrine. - -before: - class BaseFormDoctrine extends sfFormDoctrine { - ... - -becomes: - class BaseFormDoctrine extends nBaseEmbedForm { - ... - -## Usage -### Embed a form -In the setup() or configure() method of a form call $this->embedRelationAndCreate('RelationName'); that's all. -Suppose we have a Doctrine model Author and a model Book. An Author has one or more Books. - - --- schema.yml - Author: - columns: - name: string(63) - - Book: - columns: - name: string(63) - author_id: integer - relations: - Author: - local: author_id - foreignAlias: Books - -![Diagram](http://yuml.me/diagram/scruffy/class/[Author]1-0...*[Book]) - -In the Author form we call the embedRelationAndCreate() method. This creates a sub form with all the related books and a way to add new books. - - class AuthorForm extends BaseAuthorForm { - public function configure() { - // other stuff you might want to do... - - $this->embedRelationAndCreate('Books'); - } - -### Embed i18n -On any form with i18n fields, just call embedI18n(cultures); The i18n subforms will be included and saved. -Example: - $form = new BookForm(); - $form->embedI18n(array('en', 'nl')); - - - - -## Known limitations - - * No form will be embedded if the parent form doesn't already exists. Otherwise a Doctrine error will occure. The form first tries to save all embeddedForms but doesn't have a corresponding id for the relation field. - * The saveManyToMany() method tries it's best to save all list fields. However if your fields have strange names it might not work. - * A bit more testing with multi-part forms is needed. Not sure if the uploaded file is correctly saved. Just create a bug report. - -## Credits - * [Nathan Bijnens](http://twitter.com/nathan_gs) ([Servs](http://servs.eu)) - * Erik Van Kelst ([4levels](http://4levels.org)) - -## License -This work is published under the [Symfony License](http://www.symfony-project.org/license). diff --git a/index.html b/index.html new file mode 100644 index 0000000..3fc0e5d --- /dev/null +++ b/index.html @@ -0,0 +1,85 @@ + + + + + + + Codestin Search App + + + + + + + Fork me on GitHub + +
+ +
+ + + + +
+ +

nFormEmbedPlugin + by servs

+ +
+ nFormEmbedPlugin is a symfony 1.3/1.4 plugin. Created to ease the embedding and saving of related Doctrine 1.2 forms. +
+ +

Dependencies

+

Symfony 1.3/1.4 +Doctrine 1.2

+

License

+

Symfony License

+

Authors

+

Nathan Bijnens (nbijnens@servs.eu)
Erik Van Kelst

+

Contact

+

(nbijnens@servs.eu)

+ + +

Download

+

+ You can download this project in either + zip or + tar formats. +

+

You can also clone the project with Git + by running: +

$ git clone git://github.com/servs/nFormEmbedPlugin
+

+ + + +
+ + + + diff --git a/lib/form/nBaseEmbedForm.class.php b/lib/form/nBaseEmbedForm.class.php deleted file mode 100644 index 25d49f2..0000000 --- a/lib/form/nBaseEmbedForm.class.php +++ /dev/null @@ -1,325 +0,0 @@ -_relations; - } - - /** - * - * @param boolean $translationForm - */ - public function setIsTranslationForm($translationForm = true) { - $this->_isTranslationForm = $translationForm; - } - - /** - * - * @return boolean Is it a translation form? - */ - public function getIsTranslationForm() { - return $this->_isTranslationForm; - } - - /** - * - * @param array $cultures - * @param $decorator - */ - public function embedI18n($cultures, $decorator = null) { - if (!$this->isI18n()) { - throw new sfException(sprintf('The model "%s" is not internationalized.', $this->getModelName())); - } - - $class = $this->getI18nFormClass(); - foreach ($cultures as $culture) { - $i18nObject = $this->getObject()->Translation[$culture]; - $i18n = new $class($i18nObject); - $i18n->setIsTranslationForm(); - - if (false === $i18nObject->exists()) { - unset($i18n['id'], $i18n['lang']); - } - - $this->_translations[] = $culture; - - $this->embedForm($culture, $i18n, $decorator); - } - } - - /** - * Embed a Doctrine_Collection relationship in to a form - * - * [php] - * $userForm = new UserForm($user); - * $userForm->embedRelationAndCreate('Groups'); - * - * @param string $relationName The name of the relation - * @param string $formClass The name of the form class to use - * @param array $formArguments Arguments to pass to the constructor (related object will be shifted onto the front) - * - * @throws InvalidArgumentException If the relationship is not a collection - */ - public function embedRelationAndCreate($relationName, $parentForm = null, $formClass = null, $formArgs = array(), $formatter = 'table') { - - if ($this->isNew()) { - return ; - } - - if ($parentForm == null) { - $parentForm = $this; - } - - $this->_relations[] = $relationName; - - $relation = $this->getObject()->getTable()->getRelation($relationName); - - if ($relation->getType() !== Doctrine_Relation::MANY) { - throw new InvalidArgumentException('You can only embed a relationship that is a collection.'); - } - - $r = new ReflectionClass(null === $formClass ? $relation->getClass().'Form' : $formClass); - - $subForm = new BaseForm(); - - $relations = $parentForm->getObject()->getTable()->getRelations(); - $relationColumn = $relations[$relationName]['foreign']; - - - foreach ($this->getObject()->$relationName as $index => $childObject) { - $r = new ReflectionClass(null === $formClass ? get_class($childObject) .'Form' : $formClass); - $form = $r->newInstanceArgs(array_merge(array($childObject), $formArgs)); - - unset($form[$relationColumn]); - - $form->setWidget('delete', new sfWidgetFormInputCheckbox()); - - $subForm->embedForm($index, $form); - - $subForm->getWidgetSchema()->setLabel($index, (string) $childObject); - - $subForm->getWidgetSchema()->setFormFormatterName($formatter); - } - - - $object = $relation->getClass(); - $childObject = new $object(); - - $childObject->$relationColumn = $parentForm->getObject()->id; - - $r = new ReflectionClass(null === $formClass ? $relation->getClass() .'Form' : $formClass); - $form = $r->newInstanceArgs(array_merge(array($childObject), $formArgs)); - - $form->setWidget('create', new sfWidgetFormInputCheckbox()); - $form->setValidator('create', new sfValidatorPass()); - //$form->isNew(true); - unset($form[$relationColumn]); - - $subForm->getWidgetSchema()->setFormFormatterName($formatter); - - $subForm->embedForm('new', $form); - - $this->embedForm($relationName, $subForm); - } - - protected function cleanBindCreate ($relationName) { - unset($this->widgetSchema[$relationName]['new'], - $this->validatorSchema[$relationName]['new'], - $this->embeddedForms[$relationName]['new'], - $taintedValues[$relationName]['new'], - $taintedFiles[$relationName]['new']); - } - - protected function cleanEmbedded(&$taintedValues, $taintedFiles = null, &$widgetSchema = null, &$validatorSchema = null, &$form = null) { - - if ($form == null) { - $form = $this; - } - if ($widgetSchema == null) { - $widgetSchema = $this->widgetSchema; - } - if ($validatorSchema == null) { - $validatorSchema = $this->validatorSchema; - } - - foreach ($form->getRelations() as $index=>$relationName) { - if (isset($taintedValues[$relationName])) { - - if (isset($taintedValues[$relationName]['new'])) { - if(isset($taintedValues[$relationName]['new']['create'])) { - - $this->cleanEmbedded($taintedValues[$relationName]['new'], - @$taintedFiles[$relationName]['new'], - $widgetSchema[$relationName]['new'], - $validatorSchema[$relationName]['new'], - $form->embeddedForms[$relationName]->embeddedForms['new']); - - - } else { - unset($taintedValues[$relationName]['new'], - $taintedFiles[$relationName]['new'], - $validatorSchema[$relationName]['new'], - $widgetSchema[$relationName]['new'], - $form->embeddedForms[$relationName]->embeddedForms['new']); - } - - - } - - foreach ($taintedValues[$relationName] as $index=>$subFormValue) { - if(isset($subFormValue['delete'])) { - - - - $form->embeddedForms[$relationName]->embeddedForms[$index]->getObject()->delete(); - $form->getObject()->unlink($relationName, $form->embeddedForms[$relationName]->embeddedForms[$index]->getObject()->id); - - unset( - $widgetSchema[$relationName][$index], - $validatorSchema[$relationName][$index], - $form->embeddedForms[$relationName]->widgetSchema[$index], - $form->embeddedForms[$relationName]->validatorSchema[$index], - $form->embeddedForms[$relationName]->embeddedForms[$index], - $taintedValues[$relationName][$index], - $taintedFiles[$relationName][$index]); - - - } else { - $this->cleanEmbedded($taintedValues[$relationName][$index], - @$taintedFiles[$relationName][$index], - $widgetSchema[$relationName][$index], - $validatorSchema[$relationName][$index], - $form->embeddedForms[$relationName]->embeddedForms[$index]); - } - } - - - } - } - - - - } - - - public function bind(array $taintedValues = null, array $taintedFiles = null) { - $this->cleanEmbedded($taintedValues, $taintedFiles); - - parent::bind($taintedValues, $taintedFiles); - - } - - /** - * Saves embedded form objects. - * - * @param mixed $con An optional connection object - * @param array $forms An array of forms - */ - public function saveEmbeddedForms($con = null, $forms = null) { - if (null === $con) { - $con = $this->getConnection(); - } - - if (null === $forms) { - $forms = $this->embeddedForms; - } - - foreach ($forms as $name => $form) { - if ($form instanceof sfFormObject) { - $form->saveEmbeddedForms($con); - - if ($form->getIsTranslationForm() == false) { - - $form->getObject()->save($con); - } - } else { - $this->saveEmbeddedForms($con, $form->getEmbeddedForms()); - } - } - } - - - /** - * @see sfFormObject - */ - protected function doUpdateObject($values) { - foreach ($values as $index => &$value) { - if (array_key_exists($index, array_flip($this->_relations))) { - if ($this->isI18n()) { - if (!array_key_exists($index, $this->_translations)) { - unset($values[$index]); - } - } else { - foreach ($value as $subIndex => $subValue) { - if ($subIndex === 'new') { - unset($values[$index][$subIndex]); - - } else { - unset($values[$index][$subIndex]); - } - } - } - - } elseif (strpos($index, '_list') !== false) { - $this->saveManyToMany($index, $value); - } - } - - $this->getObject()->fromArray($values); - - } - - /** - * An alternative for the sfFormDoctrine Many list save class. - * This tries to emulate it's behaviour. - * - * @param $key - * @param $value - */ - public function saveManyToMany($key, $value) { - $relation = str_replace('_list', '', $key); - - $relationName = $this->camelize($relation); - - $existing = $this->object->$relationName->getPrimaryKeys(); - $values = $value; - if (!is_array($values)) { - $values = array(); - } - - $unlink = array_diff($existing, $values); - if (count($unlink)) { - $this->object->unlink($relationName, array_values($unlink)); - } - - $link = array_diff($values, $existing); - if (count($link)) { - $this->object->link($relationName, array_values($link)); - } - } -}