1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Community form. |
---|
5 | * |
---|
6 | * @package OpenPNE |
---|
7 | * @subpackage form |
---|
8 | * @author Kousuke Ebihara <ebihara@tejimaya.com> |
---|
9 | */ |
---|
10 | class CommunityForm extends BaseCommunityForm |
---|
11 | { |
---|
12 | protected $configForm; |
---|
13 | |
---|
14 | public function configure() |
---|
15 | { |
---|
16 | unset($this['created_at'], $this['updated_at'], $this['file_id'], $this['id']); |
---|
17 | |
---|
18 | $this->setValidator('name', new sfValidatorString(array('max_length' => 64, 'trim' => true))); |
---|
19 | |
---|
20 | $this->setConfigForm(); |
---|
21 | |
---|
22 | $this->setWidget('file', new sfWidgetFormInputFile()); |
---|
23 | $this->setValidator('file', new opValidatorImageFile(array('required' => false))); |
---|
24 | } |
---|
25 | |
---|
26 | public function save($con = null) |
---|
27 | { |
---|
28 | $community = parent::save($con); |
---|
29 | $oldFile = $community->getFile(); |
---|
30 | $this->saveImageFile($community); |
---|
31 | $community->save(); |
---|
32 | |
---|
33 | if ($oldFile && $oldFile->getName() !== $community->getFile()->getName()) |
---|
34 | { |
---|
35 | $oldFile->delete(); |
---|
36 | } |
---|
37 | |
---|
38 | return $community; |
---|
39 | } |
---|
40 | |
---|
41 | public function updateObject($values = null) |
---|
42 | { |
---|
43 | $object = parent::updateObject($values); |
---|
44 | |
---|
45 | $this->saveMember($object); |
---|
46 | if ($this->configForm->isValid()) |
---|
47 | { |
---|
48 | $this->configForm->save(); |
---|
49 | } |
---|
50 | |
---|
51 | return $object; |
---|
52 | } |
---|
53 | |
---|
54 | public function bind(array $taintedValues = null, array $taintedFiles = null) |
---|
55 | { |
---|
56 | parent::bind($taintedValues, $taintedFiles); |
---|
57 | |
---|
58 | $configs = $this->getValue('config'); |
---|
59 | if (!$configs) |
---|
60 | { |
---|
61 | $configs = array(); |
---|
62 | } |
---|
63 | |
---|
64 | $params = array(); |
---|
65 | foreach ($configs as $key => $value) |
---|
66 | { |
---|
67 | $params['config['.$key.']'] = $value; |
---|
68 | } |
---|
69 | |
---|
70 | $this->configForm->bind($params); |
---|
71 | foreach ($this->configForm->getErrorSchema() as $key => $value) |
---|
72 | { |
---|
73 | $this->getErrorSchema()->addError($value, $key); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | public function setConfigForm() |
---|
78 | { |
---|
79 | $this->configForm = new CommunityConfigForm(array(), array('community' => $this->getObject())); |
---|
80 | $this->mergeForm($this->configForm); |
---|
81 | foreach ($this->configForm->getValidatorSchema()->getFields() as $field => $validator) |
---|
82 | { |
---|
83 | $this->validatorSchema[$field] = new sfValidatorPass(); |
---|
84 | } |
---|
85 | $this->validatorSchema['config'] = new sfValidatorPass(); |
---|
86 | } |
---|
87 | |
---|
88 | public function saveMember(Community $community) |
---|
89 | { |
---|
90 | if ($this->isNew()) |
---|
91 | { |
---|
92 | $member = new CommunityMember(); |
---|
93 | $member->setPosition('admin'); |
---|
94 | $member->setMemberId(sfContext::getInstance()->getUser()->getMemberId()); |
---|
95 | $member->setCommunity($community); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | public function saveImageFile(Community $community) |
---|
100 | { |
---|
101 | if (!$this->getValue('file')) |
---|
102 | { |
---|
103 | return false; |
---|
104 | } |
---|
105 | |
---|
106 | $file = new File(); |
---|
107 | $file->setFromValidatedFile($this->getValue('file')); |
---|
108 | $file->setName('c_'.$community->getId().'_'.$file->getName()); |
---|
109 | $community->setFile($file); |
---|
110 | |
---|
111 | return true; |
---|
112 | } |
---|
113 | } |
---|