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