| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Author form base class.
|
|
|
5 |
*
|
|
|
6 |
* @method Author getObject() Returns the current form's model object
|
|
|
7 |
*
|
|
|
8 |
* @package ##PROJECT_NAME##
|
|
|
9 |
* @subpackage form
|
|
|
10 |
* @author Your name here
|
|
|
11 |
*/
|
|
|
12 |
abstract class BaseAuthorForm extends BaseFormPropel
|
|
|
13 |
{
|
|
|
14 |
public function setup()
|
|
|
15 |
{
|
|
|
16 |
$this->setWidgets(array(
|
|
|
17 |
'id' => new sfWidgetFormInputHidden(),
|
|
|
18 |
'name' => new sfWidgetFormInputText(),
|
|
|
19 |
'author_article_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Article')),
|
|
|
20 |
));
|
|
|
21 |
|
|
|
22 |
$this->setValidators(array(
|
|
|
23 |
'id' => new sfValidatorChoice(array('choices' => array($this->getObject()->getId()), 'empty_value' => $this->getObject()->getId(), 'required' => false)),
|
|
|
24 |
'name' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
|
|
|
25 |
'author_article_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Article', 'required' => false)),
|
|
|
26 |
));
|
|
|
27 |
|
|
|
28 |
$this->widgetSchema->setNameFormat('author[%s]');
|
|
|
29 |
|
|
|
30 |
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
|
|
|
31 |
|
|
|
32 |
parent::setup();
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public function getModelName()
|
|
|
36 |
{
|
|
|
37 |
return 'Author';
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
public function updateDefaultsFromObject()
|
|
|
42 |
{
|
|
|
43 |
parent::updateDefaultsFromObject();
|
|
|
44 |
|
|
|
45 |
if (isset($this->widgetSchema['author_article_list']))
|
|
|
46 |
{
|
|
|
47 |
$values = array();
|
|
|
48 |
foreach ($this->object->getAuthorArticles() as $obj)
|
|
|
49 |
{
|
|
|
50 |
$values[] = $obj->getArticleId();
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$this->setDefault('author_article_list', $values);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
protected function doSave($con = null)
|
|
|
59 |
{
|
|
|
60 |
parent::doSave($con);
|
|
|
61 |
|
|
|
62 |
$this->saveAuthorArticleList($con);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
public function saveAuthorArticleList($con = null)
|
|
|
66 |
{
|
|
|
67 |
if (!$this->isValid())
|
|
|
68 |
{
|
|
|
69 |
throw $this->getErrorSchema();
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
if (!isset($this->widgetSchema['author_article_list']))
|
|
|
73 |
{
|
|
|
74 |
// somebody has unset this widget
|
|
|
75 |
return;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
if (null === $con)
|
|
|
79 |
{
|
|
|
80 |
$con = $this->getConnection();
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$c = new Criteria();
|
|
|
84 |
$c->add(AuthorArticlePeer::AUTHOR_ID, $this->object->getPrimaryKey());
|
|
|
85 |
AuthorArticlePeer::doDelete($c, $con);
|
|
|
86 |
|
|
|
87 |
$values = $this->getValue('author_article_list');
|
|
|
88 |
if (is_array($values))
|
|
|
89 |
{
|
|
|
90 |
foreach ($values as $value)
|
|
|
91 |
{
|
|
|
92 |
$obj = new AuthorArticle();
|
|
|
93 |
$obj->setAuthorId($this->object->getPrimaryKey());
|
|
|
94 |
$obj->setArticleId($value);
|
|
|
95 |
$obj->save();
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
}
|