}
else{
$data = $o->getData();
+ foreach($o->getTable()->getRelations() as $rname=>$rel){
+ if (array_search($rname,$this->fields())){
+ if ($rel->getType()==IPF_ORM_Relation::MANY_AGGREGATE){
+ $data[$rname] = array();
+ foreach($rel->fetchRelatedFor($o) as $ri)
+ $data[$rname][] = $ri->id;
+ }
+ }
+ }
$form = $this->_getEditForm($o,&$data,array('user_fields'=>$this->fields()));
$this->_setupEditForm($form);
$this->setInlines($o, &$data);
</div>
<!-- END Container -->
{block scripts}{/block}
+
</body>
</html>
--- /dev/null
+<?php
+
+class IPF_Form_DB_Manytomany extends IPF_Form_DB
+{
+ public $type = 'manytomany';
+
+ function formField($def, $form_field='IPF_Form_Field_ModelMultipleChoice')
+ {
+ //print_r($def);
+ $list_objects = IPF_ORM::getTable($def['model'])->findAll();
+ $choices = array();
+ foreach($list_objects as $o){
+ $choices[$o->__toString()] = $o->id;
+ }
+ $def['choices'] = $choices;
+ if (!isset($def['widget'])) {
+ $def['widget'] = 'IPF_Form_Widget_SelectMultipleInput';
+ }
+ return parent::formField($def, $form_field);
+ }
+}
function __construct($params=array()){
parent::__construct($params);
- $this->model = $params['model'];
+ $this->_model = $params['model'];
if (isset($params['queryset'])){
$choices = array('--------'=>'');
foreach ($params['queryset'] as $item) {
$this->setChoices($choices);
}
}
-
+
public function clean($value){
parent::clean($value);
if (in_array($value, $this->empty_values)) {
//print_r($this->model);
//print $value;
//$this->model->get($value);
- $o = $this->model->getTable()->find($value);
+ $o = $this->_model->getTable()->find($value);
return $o;
}
}
--- /dev/null
+<?php
+
+class IPF_Form_Field_ModelMultipleChoice extends IPF_Form_Field_MultipleChoice{
+ public $widget = 'IPF_Form_Widget_SelectMultipleInput';
+ protected $_model;
+
+ function __construct($params=array()){
+ parent::__construct($params);
+ $this->_model = $params['model'];
+ }
+}
+
--- /dev/null
+<?php
+
+class IPF_Form_Field_MultipleChoice extends IPF_Form_Field_Choice{
+ public $widget = 'IPF_Form_Widget_SelectMultipleInput';
+
+ public function validValue($value){
+ foreach($value as $v){
+ $find = false;
+ foreach($this->_choices as $name=>$val){
+ if ($v==$val){
+ $find = true;
+ break;
+ }
+ }
+ if (!$find)
+ return false;
+ }
+ return true;
+ }
+
+ public function clean($value){
+ parent::clean($value);
+ if (in_array($value, $this->empty_values)) {
+ return '';
+ }
+ if (!$this->validValue($value))
+ throw new IPF_Exception_Form(__('Invalid choice'));
+ return $value;
+ }
+
+}
+
$defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true, 'model'=>$relation->getClass());
$form_field = $db_field->formField($defaults);
$this->fields[$name] = $form_field;
+ return;
+ }
+ if ($relation->getType()==IPF_ORM_Relation::MANY_AGGREGATE){
+ $db_field = new IPF_Form_DB_ManyToMany('',$name);
+ $defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true, 'model'=>$relation->getClass());
+ $form_field = $db_field->formField($defaults);
+ $this->fields[$name] = $form_field;
+ return;
}
}
$this->model->SetFromFormData($this->cleaned_data);
try{
$this->model->save();
+ $rels = $this->model->getTable()->getRelations();
+
+ foreach($rels as $rname=>$rel){
+ //print "$rname<br>";
+ if (isset($this->cleaned_data[$rname])){
+ //print $rel->getAlias();
+ $this->model->unlink($rel->getAlias());
+ if (is_array($this->cleaned_data[$rname]))
+ $this->model->link($rel->getAlias(),$this->cleaned_data[$rname]);
+ }
+ }
return $this->model;
} catch(IPF_ORM_Exception_Validator $e) {
$erecords = $e->getInvalidRecords();
parent::__construct($attrs);
}
- public function render($name, $value, $extra_attrs=array(),
+ public function render($name, $value, $extra_attrs=array(),
$choices=array())
{
$output = array();
if ($value === null) {
$value = array();
}
- $final_attrs = $this->buildAttrs(array('name' => $name.'[]'),
+ $final_attrs = $this->buildAttrs(array('name' => $name.'[]'),
$extra_attrs);
$output[] = '<select multiple="multiple"'
.IPF_Form_Widget_Attrs($final_attrs).'>';
$choices = array_merge($this->choices, $choices);
+
foreach ($choices as $option_label=>$option_value) {
$selected = (in_array($option_value, $value)) ? ' selected="selected"':'';
$output[] = sprintf('<option value="%s"%s>%s</option>',
htmlspecialchars($option_value, ENT_COMPAT, 'UTF-8'),
- $selected,
+ $selected,
htmlspecialchars($option_label, ENT_COMPAT, 'UTF-8'));
}
$record->preSave($event);
$record->getTable()->getRecordListener()->preSave($event);
$state = $record->state();
-
+
if ( ! $event->skipOperation) {
switch ($state) {
case IPF_ORM_Record::STATE_TDIRTY:
if ($record->hasReference($alias)) {
$obj = $record->$alias;
-
// check that the related object is not an instance of IPF_ORM_Null
if ( ! ($obj instanceof IPF_ORM_Null)) {
$obj->save($conn);
$this->saveAssociations($record);
$record->state($state);
-
$conn->commit();
return true;
{
foreach ($record->getReferences() as $k => $v) {
$rel = $record->getTable()->getRelation($k);
-
+ //print get_class($rel);
if ($rel instanceof IPF_ORM_Relation_Association) {
$v->save($this->conn);
$assocTable = $rel->getAssociationTable();
+
foreach ($v->getDeleteDiff() as $r) {
$query = 'DELETE FROM ' . $assocTable->getTableName()
. ' WHERE ' . $rel->getForeign() . ' = ?'
<?php
class IPF_ORM_Exception extends IPF_Exception_Base{
-{
protected static $_errorMessages = array(
IPF_ORM::ERR => 'unknown error',
IPF_ORM::ERR_ALREADY_EXISTS => 'already exists',
}
-
-
-
+
+
+
public function SetFromFormData($cleaned_values)
{
+ $names = $this->_table->getFieldNames();
foreach ($cleaned_values as $key=>$val) {
$validators = $this->getTable()->getFieldValidators($key);
if (
if (($val!==null) && ($val==''))
continue;
}
- $this->$key = $val;
+ if (array_search($key,$names))
+ $this->$key = $val;
}
}
}
\ No newline at end of file