| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Article form base class.
|
|
|
5 |
*
|
|
|
6 |
* @method Article 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 BaseArticleForm extends BaseFormPropel
|
|
|
13 |
{
|
|
|
14 |
public function setup()
|
|
|
15 |
{
|
|
|
16 |
$this->setWidgets(array(
|
|
|
17 |
'id' => new sfWidgetFormInputHidden(),
|
|
|
18 |
'title' => new sfWidgetFormInputText(),
|
|
|
19 |
'body' => new sfWidgetFormTextarea(),
|
|
|
20 |
'Online' => new sfWidgetFormInputCheckbox(),
|
|
|
21 |
'excerpt' => new sfWidgetFormInputText(),
|
|
|
22 |
'category_id' => new sfWidgetFormPropelChoice(array('model' => 'Category', 'add_empty' => false)),
|
|
|
23 |
'created_at' => new sfWidgetFormDateTime(),
|
|
|
24 |
'end_date' => new sfWidgetFormDateTime(),
|
|
|
25 |
'book_id' => new sfWidgetFormPropelChoice(array('model' => 'Book', 'add_empty' => true)),
|
|
|
26 |
'author_article_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Author')),
|
|
|
27 |
));
|
|
|
28 |
|
|
|
29 |
$this->setValidators(array(
|
|
|
30 |
'id' => new sfValidatorChoice(array('choices' => array($this->getObject()->getId()), 'empty_value' => $this->getObject()->getId(), 'required' => false)),
|
|
|
31 |
'title' => new sfValidatorString(array('max_length' => 255)),
|
|
|
32 |
'body' => new sfValidatorString(array('required' => false)),
|
|
|
33 |
'Online' => new sfValidatorBoolean(array('required' => false)),
|
|
|
34 |
'excerpt' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
|
|
|
35 |
'category_id' => new sfValidatorPropelChoice(array('model' => 'Category', 'column' => 'id')),
|
|
|
36 |
'created_at' => new sfValidatorDateTime(array('required' => false)),
|
|
|
37 |
'end_date' => new sfValidatorDateTime(array('required' => false)),
|
|
|
38 |
'book_id' => new sfValidatorPropelChoice(array('model' => 'Book', 'column' => 'id', 'required' => false)),
|
|
|
39 |
'author_article_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Author', 'required' => false)),
|
|
|
40 |
));
|
|
|
41 |
|
|
|
42 |
$this->validatorSchema->setPostValidator(
|
|
|
43 |
new sfValidatorPropelUnique(array('model' => 'Article', 'column' => array('title', 'category_id')))
|
|
|
44 |
);
|
|
|
45 |
|
|
|
46 |
$this->widgetSchema->setNameFormat('article[%s]');
|
|
|
47 |
|
|
|
48 |
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
|
|
|
49 |
|
|
|
50 |
parent::setup();
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public function getModelName()
|
|
|
54 |
{
|
|
|
55 |
return 'Article';
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
public function updateDefaultsFromObject()
|
|
|
60 |
{
|
|
|
61 |
parent::updateDefaultsFromObject();
|
|
|
62 |
|
|
|
63 |
if (isset($this->widgetSchema['author_article_list']))
|
|
|
64 |
{
|
|
|
65 |
$values = array();
|
|
|
66 |
foreach ($this->object->getAuthorArticles() as $obj)
|
|
|
67 |
{
|
|
|
68 |
$values[] = $obj->getAuthorId();
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
$this->setDefault('author_article_list', $values);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
protected function doSave($con = null)
|
|
|
77 |
{
|
|
|
78 |
parent::doSave($con);
|
|
|
79 |
|
|
|
80 |
$this->saveAuthorArticleList($con);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public function saveAuthorArticleList($con = null)
|
|
|
84 |
{
|
|
|
85 |
if (!$this->isValid())
|
|
|
86 |
{
|
|
|
87 |
throw $this->getErrorSchema();
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
if (!isset($this->widgetSchema['author_article_list']))
|
|
|
91 |
{
|
|
|
92 |
// somebody has unset this widget
|
|
|
93 |
return;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
if (null === $con)
|
|
|
97 |
{
|
|
|
98 |
$con = $this->getConnection();
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$c = new Criteria();
|
|
|
102 |
$c->add(AuthorArticlePeer::ARTICLE_ID, $this->object->getPrimaryKey());
|
|
|
103 |
AuthorArticlePeer::doDelete($c, $con);
|
|
|
104 |
|
|
|
105 |
$values = $this->getValue('author_article_list');
|
|
|
106 |
if (is_array($values))
|
|
|
107 |
{
|
|
|
108 |
foreach ($values as $value)
|
|
|
109 |
{
|
|
|
110 |
$obj = new AuthorArticle();
|
|
|
111 |
$obj->setArticleId($this->object->getPrimaryKey());
|
|
|
112 |
$obj->setAuthorId($value);
|
|
|
113 |
$obj->save();
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
}
|