-
Notifications
You must be signed in to change notification settings - Fork 0
/
loft_faq.admin.inc
executable file
·156 lines (136 loc) · 5.02 KB
/
loft_faq.admin.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* @file
* Administration page callbacks for the loft_faq module.
*
* @ingroup loft_faq
* @{
*/
/**
* Form builder. Configure my_module.
*
* @ingroup forms
* @see system_settings_form()
*/
function loft_faq_admin_settings() {
$form = array();
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Settings'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$options = array();
foreach (node_type_get_types() as $type) {
$options[$type->type] = $type->name;
}
$form['settings']['loft_faq_node_type'] = array(
'#type' => 'select',
'#title' => t('FAQ node type'),
'#default_value' => variable_get('loft_faq_node_type', LOFT_FAQ_NODE_TYPE),
'#options' => $options,
'#description' => t('This node type will be used as the FAQ node type.'),
'#required' => TRUE,
);
$field = variable_get('loft_faq_group_fieldname', LOFT_FAQ_GROUP_FIELDNAME);
$form['settings']['loft_faq_group_fieldname'] = array(
'#type' => 'textfield',
'#title' => t('Category Fieldname on the FAQ node'),
'#description' => t('What is the field that is used to store the category?'),
'#default_value' => $field,
'#required' => TRUE,
);
$original = variable_get('loft_faq_group_default', LOFT_FAQ_GROUP_DEFAULT);
$form['settings']['loft_faq_group_default'] = array(
'#type' => 'textfield',
'#title' => t('Default group for questions'),
'#description' => t('Changing this field value <em>after content has been assigned "%default"</em>, will result in said content automatically being updated to the new value.', array('%default' => $original)),
'#default_value' => $original,
);
$form['settings']['loft_faq_set_default'] = array(
'#type' => 'checkbox',
'#title' => t('Prefill group for new FAQs?'),
'#default_value' => variable_get('loft_faq_set_default', LOFT_FAQ_SET_DEFAULT),
'#description' => t('Check this box and the default group will be already entered in the group box on the FAQ node form.'),
);
$form['#old_nid'] = variable_get('loft_faq_faq_page_nid', NULL);
// This will be set if the nids that are in the system come from a hook
// implementation, if they do not then we will allow the use to enter a page
// nid.
$nids = empty($form['#old_nid']) && ($nids = loft_faq_get_pages_info('ids')) ? $nids : array();
if (empty($nids)) {
$form['pages'] = array(
'#type' => 'fieldset',
'#title' => t('Page(s)'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#access' => empty($nids),
);
$form['pages']['loft_faq_faq_page_nid'] = array(
'#type' => 'textfield',
'#description' => t("Create a page node and enter it's node id here. The FAQs will be appended to the content of this node."),
'#title' => t('FAQ page NID'),
'#default_value' => variable_get('loft_faq_faq_page_nid', NULL),
'#required' => FALSE,
'#size' => 20,
);
}
$form['#submit'][] = 'loft_faq_admin_settings_submit';
return system_settings_form($form);
}
/**
* Form submission handler for loft_faq_admin_settings().
*
* @see loft_faq_admin_settings()
*/
function loft_faq_admin_settings_submit($form, &$form_state) {
// If the default type gets changed we are going to update the database directly.
$original = variable_get('loft_faq_group_default', LOFT_FAQ_GROUP_DEFAULT);
if (($new = $form_state['values']['loft_faq_group_default']) !== $original) {
// Load and alter all nodes with the original category to the new one.
$need_update = loft_faq_get_faq_by_category($original, TRUE);
$updated = 0;
foreach ($need_update as $node) {
loft_faq_set_node_category($node, $new);
node_save($node);
++$updated;
}
if ($updated) {
drupal_set_message(t("We updated the category for @count from %old to %new.", array(
'@count' => format_plural($updated, '1 faq', '@count faqs'),
'%old' => $original,
'%new' => $new,
)), 'status', FALSE);
}
}
if (($nid = $form_state['values']['loft_faq_faq_page_nid']) != $form['#old_nid'] &&
($node = node_load($nid))) {
$query = db_select('menu_links', 'm');
$item_exists = $query
->fields('m', array('mlid'))
->condition('link_path', 'node/' . $nid)
->range(0, 1)
->countQuery()
->execute()
->fetchField();
// We have a new node, let's create the menu item for it, but only once per nid
if (!$item_exists && !variable_get('loft_faq_auto_menu_item_' . $nid)) {
variable_set('loft_faq_auto_menu_item_' . $nid, TRUE);
$item = array(
'link_title' => t('FAQ'),
'link_path' => 'node/' . $nid,
'router_path' => 'node/%',
'menu_name' => 'main-menu',
'options' => array(
'attributes' => array(
'title' => t('Review our Frequently Asked Questions'),
),
),
);
$item['mlid'] = menu_link_save($item);
menu_cache_clear_all();
drupal_set_message(t('An FAQ link has been added to the menu.'));
}
}
}
/** @} */ //end of group loft_faq