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 | * CommunityConfig form. |
---|
13 | * |
---|
14 | * @package OpenPNE |
---|
15 | * @subpackage form |
---|
16 | * @author Kousuke Ebihara <ebihara@tejimaya.com> |
---|
17 | */ |
---|
18 | class CommunityConfigForm extends sfForm |
---|
19 | { |
---|
20 | protected |
---|
21 | $configSettings = array(), |
---|
22 | $category = '', |
---|
23 | $community, |
---|
24 | $isNew = false, |
---|
25 | $isAutoGenerate = true; |
---|
26 | |
---|
27 | public function __construct($defaults = array(), $options = array(), $CSRFSecret = null) |
---|
28 | { |
---|
29 | return parent::__construct($defaults, $options, false); |
---|
30 | } |
---|
31 | |
---|
32 | public function configure() |
---|
33 | { |
---|
34 | $this->setCommunity($this->getOption('community')); |
---|
35 | |
---|
36 | $this->setConfigSettings(); |
---|
37 | |
---|
38 | if ($this->isAutoGenerate) |
---|
39 | { |
---|
40 | $this->generateConfigWidgets(); |
---|
41 | } |
---|
42 | |
---|
43 | $this->widgetSchema->setNameFormat('community_config[%s]'); |
---|
44 | |
---|
45 | $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema); |
---|
46 | } |
---|
47 | |
---|
48 | public function setCommunity($community) |
---|
49 | { |
---|
50 | if (!($community instanceof Community)) |
---|
51 | { |
---|
52 | $community = new Community(); |
---|
53 | } |
---|
54 | $this->community = $community; |
---|
55 | } |
---|
56 | |
---|
57 | public function generateConfigWidgets() |
---|
58 | { |
---|
59 | foreach ($this->configSettings as $key => $value) |
---|
60 | { |
---|
61 | $this->setConfigWidget($key); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | public function setConfigWidget($name) |
---|
66 | { |
---|
67 | $config = $this->configSettings[$name]; |
---|
68 | $this->widgetSchema[$name] = opFormItemGenerator::generateWidget($config); |
---|
69 | $this->widgetSchema->setLabel($name, $config['Caption']); |
---|
70 | $communityConfig = CommunityConfigPeer::retrieveByNameAndCommunityId($name, $this->community->getId()); |
---|
71 | if ($communityConfig) |
---|
72 | { |
---|
73 | $this->setDefault($name, $communityConfig->getValue()); |
---|
74 | } |
---|
75 | $this->validatorSchema[$name] = opFormItemGenerator::generateValidator($config); |
---|
76 | } |
---|
77 | |
---|
78 | public function setConfigSettings($category = '') |
---|
79 | { |
---|
80 | $categories = sfConfig::get('openpne_community_category'); |
---|
81 | $configs = sfConfig::get('openpne_community_config'); |
---|
82 | |
---|
83 | if (!$category) |
---|
84 | { |
---|
85 | $this->configSettings = $configs; |
---|
86 | return true; |
---|
87 | } |
---|
88 | |
---|
89 | foreach ($categories[$category] as $value) |
---|
90 | { |
---|
91 | $this->configSettings[$value] = $configs[$value]; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | public function save() |
---|
96 | { |
---|
97 | foreach ($this->getValues() as $key => $value) |
---|
98 | { |
---|
99 | $config = CommunityConfigPeer::retrieveByNameAndCommunityId($key, $this->community->getId()); |
---|
100 | if (!$config) |
---|
101 | { |
---|
102 | $config = new CommunityConfig(); |
---|
103 | $config->setCommunity($this->community); |
---|
104 | $config->setName($key); |
---|
105 | } |
---|
106 | $config->setValue($value); |
---|
107 | $config->save(); |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|