1: <?php
2:
3: 4: 5:
6: class Quform_Form_Factory
7: {
8: 9: 10:
11: protected $elementFactory;
12:
13: 14: 15:
16: protected $options;
17:
18: 19: 20:
21: protected $session;
22:
23: 24: 25:
26: protected $tokenReplacer;
27:
28: 29: 30: 31: 32: 33:
34: public function __construct(
35: Quform_Element_Factory $elementFactory,
36: Quform_Options $options,
37: Quform_Session $session,
38: Quform_TokenReplacer $tokenReplacer
39: ) {
40: $this->elementFactory = $elementFactory;
41: $this->options = $options;
42: $this->session = $session;
43: $this->tokenReplacer = $tokenReplacer;
44: }
45:
46: 47: 48: 49: 50: 51:
52: public function create(array $config = array())
53: {
54: $config = apply_filters('quform_form_factory_pre_create', $config);
55:
56: if ( ! array_key_exists('uniqueId', $config) || ! Quform_Form::isValidUniqueId($config['uniqueId'])) {
57: $config['uniqueId'] = Quform_Form::generateUniqueId();
58: }
59:
60: $form = new Quform_Form($config['id'], $config['uniqueId'], $this->session, $this->tokenReplacer, $this->options);
61:
62: $form->setCharset(get_bloginfo('charset'));
63: $form->setIsActive($this->getConfigValue($config, 'active'));
64:
65: if (array_key_exists('dynamicValues', $config)) {
66: $form->setDynamicValues($config['dynamicValues']);
67: }
68:
69:
70: foreach ($this->getConfigValue($config, 'notifications') as $notification) {
71: $form->addNotification(new Quform_Notification($notification, $form, $this->options));
72: }
73:
74:
75: foreach ($this->getConfigValue($config, 'confirmations') as $confirmation) {
76: $form->addConfirmation(new Quform_Confirmation($confirmation, $form));
77: }
78:
79:
80: $elements = $this->getConfigValue($config, 'elements');
81:
82:
83: unset($config['notifications'], $config['confirmations'], $config['elements']);
84:
85: if (array_key_exists('entryId', $config)) {
86: $form->setEntryId($config['entryId']);
87: unset($config['entryId']);
88: }
89:
90: $form->setConfig($config);
91:
92:
93: foreach ($elements as $eConfig) {
94: $page = $this->elementFactory->create($eConfig, $form);
95:
96: if ($page instanceof Quform_Element_Page) {
97: $form->addPage($page);
98: }
99: }
100:
101:
102: $lastPage = $form->getLastPage();
103: if ($form->config('honeypot') && ! in_array($form->config('environment'), array('viewEntry', 'editEntry', 'listEntry')) && $lastPage instanceof Quform_Element_Page) {
104: $lastPage->addElement($this->elementFactory->create(array(
105: 'type' => 'honeypot',
106: 'id' => 0
107: ), $form));
108: }
109:
110: return $form;
111: }
112:
113: 114: 115: 116: 117: 118: 119: 120: 121:
122: protected function getConfigValue(array $config, $key)
123: {
124: $value = Quform::get($config, $key);
125:
126: if ($value === null) {
127: $value = Quform::get(Quform_Form::getDefaultConfig(), $key);
128: }
129:
130: return $value;
131: }
132: }
133: