-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformulize.module
685 lines (596 loc) · 20.9 KB
/
formulize.module
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<?php
// $Id$
if (module_exists("og")) {
// if the organic groups modules is loaded, then also load our organic group hooks
// note that this is included here, rather than inside _formulize_integration_init() because that
// function may not be called until later, so an og module hook could be called first
include_once(drupal_get_path('module', 'formulize') . "/formulize-og.php");
}
/**
* Project: Formulize: data management rapid application development
* File: formulize.module
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/**
* Formulize integration API inclusion
*/
function _formulize_integration_init()
{
static $formulize_loaded = null; // static var so we can load the include file just once
if (null === $formulize_loaded) {
// Get path of Formulize
$formulize_path = variable_get('formulize_full_path', NULL);
$formulize_path = is_dir($formulize_path) ? rtrim($formulize_path, '/\\') : dirname($formulize_path);
// Get the integration API path
$integration_api_path = $formulize_path . DIRECTORY_SEPARATOR . 'integration_api.php';
// Check if the integration API exists in the path specified
if (file_exists($integration_api_path)) {
// Set user ID for Formulize session
global $user, $formulizeHostSystemUserId;
$formulizeHostSystemUserId = $user->uid;
// Include the Formulize integration API
include_once($integration_api_path);
$formulize_loaded = true;
} else {
$formulize_loaded = false;
}
}
return $formulize_loaded;
}
/**
* include Formulize integration API upon module initialization
*/
function formulize_init()
{
global $user;
// If we can't include the integration API, let administrators know to set the Formulize path
if (!_formulize_integration_init()
&& $user->roles
&& array_key_exists(3, $user->roles)
&& current_path() != 'admin/settings/formulize/path'
) {
drupal_set_message(t('The Formulize path is not set. Please <a href="' . url('admin/settings/formulize/path') .
'">set the Formulize path</a>.'), 'error', FALSE);
}
}
/**
* @file
* Allows users to access to Formulize through Drupal installations.
*/
/**
* Implementation of hook_help().
*
* Throughout Drupal, hook_help() is used to display help text at the top of
* pages.
*/
function formulize_help($path, $arg)
{
switch ($path) {
case 'admin/help#formulize':
$output = '<p>'. t('The Formulize module integrates with Formulize and lets you create nodes in your Drupal website that are based on screens defined in Formulize. This module also ensures synchronization of the users and groups/roles between Drupal and Formulize.') .'</p>';
$output .= '<p>' . t('Behaviours this module supports:') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('Storing the location of Formulize') . '</li>';
$output .= '<li>' . t('Displaying data management screens from Formulize, inside Drupal') . '</li>';
$output .= '<li>' . t('Automatically synchronizing users with Drupal') . '</li>';
$output .= '<li>' . t('With the Formulize Organic Groups Synchronization module, automatically synchronizing Drupal organic groups with Formulize groups') . '</li>';
$output .= '<li>' . t('With the Formulize Roles Synchronization module, automatically synchronizing Drupal roles with Formulize groups') . '</li>';
$output .= '</ul>';
$output .= '<p>'. t('For more information please refer to the <a href="!1">Formulize website</a> which includes project information, support forum, documentation, and more.', array('!1' => 'http://freeformsolutions.ca/formulize')) .'</p>';
return $output;
case 'admin/modules#description':
// This description is shown in the listing at admin/modules.
return t('The Formulize module (Formulize !1) integrates with Formulize and lets you create nodes in your Drupal website that are based on screens defined in <a href="!2">Formulize</a>. This module also ensures synchronization of the users and groups/roles between Drupal and Formulize.', array('!1' => '1.0', '!2' => 'http://freeformsolutions.ca/formulize/'));
case 'admin/settings/formulize':
// This description is shown on the main Formulize page.
return t('The settings for the Formulize module allow you to tell Drupal where Formulize is. So you must have already installed Formulize in your website. Once you have, then you can tell Drupal the full path to Formulize so Drupal can display Formulize content inside your website, and synchronize users and groups/roles.');
case 'admin/settings/formulize/path':
// This description is shown on the Formulize path.
return t('Define the full path to the Formulize mainfile.php.');
}
}
/**
* Implementation of hook_perm()
* @return array An array of valid permissions for the formulize module
* --+MedievalSpawn Drupal7 Update+-- Implemented hook_permission() instead
*/
function formulize_permission()
{
return array(
'access form content' => array(
'title' => t('Access Form content')
)
);
}
/**
* Implementation of hook_access()
* --+MedievalSpawn Drupal7 Update+-- Implemented hook_node_access() instead
*/
function formulize_node_access($node, $op, $account)
{
$type = is_string($node) ? $node : $node->type;
if (in_array($type, node_permissions_get_configured_types())) {
if ($op == 'view') {
if (user_access('access form content', $account) && ($account->uid == $node->uid)) {
return NODE_ACCESS_ALLOW;
}
}
}
// Returning nothing from this function would have the same effect.
return NODE_ACCESS_IGNORE;
}
/**
* Implementation of hook_menu().
*/
function formulize_menu()
{
$items = array();
// Main admin page for Formulize
$items['admin/settings/formulize'] = array(
'title' => 'Formulize',
'description' => 'Setting up integration with Formulize.',
'position' => 'left',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system')
);
// Formulize path settings page
$items['admin/settings/formulize/path'] = array(
'title' => 'Formulize Path',
'page callback' => 'drupal_get_form',
'page arguments' => array('formulize_path_settings'),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'weight' => -5,
'description' => 'Global configuration of Formulize functionality.',
'type' => MENU_NORMAL_ITEM
);
// Formulize users and groups syncing page
$items['admin/settings/formulize/sync'] = array(
'title' => 'Formulize Synchronizing',
'page callback' => 'drupal_get_form',
'page arguments' => array('formulize_sync'),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'weight' => -3,
'description' => 'Synchronizing between Drupal and Formulize.',
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/**
* Menu callback for admin/formulize/path.
*/
function formulize_path_settings($form, &$form_state)
{
$form['formulize_full_path'] = array(
'#type' => 'textfield',
'#title' => t('Formulize full path'),
'#default_value' => variable_get('formulize_full_path', NULL),
'#description' => t('The full path to the mainfile.php file of your Formulize installation. For instance, "/var/www/mysite.com/formulize"')
);
$form = system_settings_form($form);
$form['#submit'][] = 'formulize_path_settings_submit';
return $form;
}
/**
* Form submission handler
*/
function formulize_path_settings_submit($form, &$form_state)
{
if (_formulize_integration_init()) {
drupal_set_message(t('The integration_api.php file was successfully found from the path that was specified.'), 'status', FALSE);
} else {
drupal_set_message(t('The Formulize path is incorrect or the integration_api.php file was not found in the path that was specified. Please correctly specify the path to Formulize below.'), 'error', FALSE);
}
}
/**
* Menu callback for admin/formulize/sync.
*/
function formulize_sync($form, &$form_state)
{
$form['sync_users'] = array(
'#type' => 'checkbox',
'#title' => t('Create users and groups'),
'#default_value' => 0,
'#weight' => '0',
'#description' => t('Check this when you are ready to create Formulize users and groups for every existing Drupal user and role. After the initial manual sync, for every change to your Drupal users and roles, the changes will automatically propagate to Formulize.')
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Synchronize with Formulize'),
'#weight' => '5'
);
return $form;
}
/**
* Form submission handler
*/
function formulize_sync_submit($form, &$form_state)
{
$form_values = $form_state['values'];
if ($form_values['submit'] && $form_values['sync_users']) {
if (!_formulize_integration_init()) {
drupal_set_message(t('Users and groups have not been synchronized with Formulize. This is because the Formulize path has not been set yet. Please <a href="' . url('admin/settings/formulize/path') . '">set the Formulize path</a> before attempting to synchronize users and groups again.'), 'error', FALSE);
} else {
Formulize::create_resource_mapping_table();
_formulize_sync_groups();
_formulize_sync_users();
if (module_exists("og") or function_exists("og_is_group_type")) {
_formulize_sync_organic_groups();
}
drupal_set_message(t('Users and groups have been successfully synchronized with Formulize.'), 'status', FALSE);
}
} else {
drupal_set_message(t('Users and groups have not been synchronized with Formulize. Please check the box labeled "Create users and groups" and try again.'), 'error', FALSE);
}
}
/**
* Implementation of hook_node_info()
* Defines the Formulize node type so that Drupal knows that such a node type exists and will let people create nodes of that type
*/
function formulize_node_info()
{
return array(
'formulize' => array(
'name' => t('Form'),
'base' => 'formulize',
'description' => t('Formulize is a data management and reporting system. You can embed data management applications inside Drupal, once you have configured them in Formulize. See the <a href="/admin/settings/formulize"></a>admin settings</a> for more information.'),
'has_title' => TRUE,
'title_label' => ('Screen Name')
)
);
}
/**
* Implementation of hook_form()
* Defines the custom fields for the editing form for the Formulize node type
* --+MedievalSpawn Drupal7 Update+-- Implemented node_type_get_type() instead of node_get_types()
*/
function formulize_form($node, &$form_state)
{
$type = node_type_get_type($node);
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5
);
$form['screen_id'] = array(
'#type' => 'select',
'#title' => t('Select the Formulize screen that you want to embed in this node'),
'#required' => TRUE,
'#default_value' => isset($node->screen_id) ? $node->screen_id : '0',
'#weight' => -4,
'#options' => Formulize::getScreens()
);
return $form;
}
/**
* Implementation of hook_validate().
*
* Errors should be signaled with form_set_error().
*/
function formulize_validate($node, $form, &$form_state)
{
if ($node->screen_id) {
if (!is_numeric($node->screen_id)) {
form_set_error('screen_id', t('The screen id must be a number.'));
}
}
else {
// Let an empty field mean "zero."
$node->screen_id = 0;
}
}
/**
* Implementation of hook_insert().
*
* As a new node is being inserted into the database, we need to do our own
* database inserts.
* --+MedievalSpawn Drupal7 Update+-- Changed the way it was filling the table to meet new db handlers semantics.
*/
function formulize_insert($node)
{
//db_query("INSERT INTO {formulize} (vid, nid, screen_id) VALUES (%d, %d, %d)", $node->vid, $node->nid, $node->screen_id);
db_insert('formulize') // Table name no longer needs {}
->fields(array(
'vid' => $node->vid,
'nid' => $node->nid,
'screen_id' => $node->screen_id
))
->execute();
}
/**
* Implementation of hook_update().
*
* As an existing node is being updated in the database, we need to do our own
* database updates.
* --+MedievalSpawn Drupal7 Update+-- Changed the way it was updating the table to meet new db handlers semantics.
*/
function formulize_update($node)
{
// if this is a new node or we're adding a new revision,
if ($node->revision) {
formulize_insert($node);
}
else {
db_update('formulize') // Table name no longer needs {}
->fields(array('screen_id' => $node->screen_id))
->condition('vid', $node->vid, '=')
->execute();
}
}
/**
* Implementation of hook_nodeapi().
*
* When a node revision is deleted, we need to remove the corresponding record
* from our table. The only way to handle revision deletion is by implementing
* hook_nodeapi().
* --+MedievalSpawn Drupal7 Update+-- Implemented hook_node_revision_delete() instead
*/
function formulize_node_revision_delete($node)
{
//Notice that we're matching a single revision based on the node's vid.
db_delete('formulize')
->condition('vid', $node->vid)
->execute();
}
/**
* Implementation of hook_delete().
*
* When a node is deleted, we need to remove all related records from out table.
* --+MedievalSpawn Drupal7 Update+-- Changed the way it was deleting from the table to meet new db handlers semantics.
*/
function formulize_delete($node)
{
// Notice that we're matching all revisions, by using the node's nid.
db_delete('formulize')
->condition('nid', $node->nid)
->execute();
}
/**
* Implementation of hook_load().
*
* Now that we've defined how to manage the node data in the database, we
* need to tell Drupal how to get the node back out. This hook is called
* every time a node is loaded, and allows us to do some loading of our own.
* --+MedievalSpawn Drupal7 Update+-- Changed the way it called the data to meet new db handlers semantics.
*/
function formulize_load($nodes)
{
$result = db_query('SELECT nid, screen_id FROM {formulize} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes)));
foreach ($result as $record) {
$nodes[$record->nid]->screen_id = $record->screen_id;
}
}
/**
* Implementation of hook_view().
*
* Runs the node text through output filters.
*/
function formulize_view($node, $view_mode)
{
//connect to Formulize session
if (_formulize_integration_init()) {
drupal_add_css(drupal_get_path('module', 'formulize') . '/formulize.css');
Formulize::init(); // init so that mainfile.php is loaded and the url definition can be used
drupal_add_js(XOOPS_URL . '/modules/formulize/libraries/formulize.js');
ob_start(); // start capturing output from Formulize screens
Formulize::renderScreen($node->screen_id);
$output = ob_get_clean();
$node->content['formulize_screen'] = array(
'#markup' => $output,
'#weight' => 1
);
} else {
$node->content['formulize_screen'] = array(
'#markup' => t('This section is currently unavailable.'),
'#weight' => 1
);
}
return $node;
}
/**
* Theme a Formulize screen
*
* @param $fz_screen
* The screen as an html dump.
* @return
* An HTML themed string.
*/
function theme_formulize_display_screen($formulize_screen_output)
{
return $formulize_screen_output;
}
/**
* Check that Formulize is installed and working
*/
function formulize_exist()
{
return TRUE;
}
/**
* Sync Drupal roles with Formulize's groups
*/
function _formulize_sync_groups()
{
if (!_formulize_integration_init())
return;
// Map Drupal's authenticated user role to Formulize's Registered User group
Formulize::createResourceMapping(Formulize::GROUP_RESOURCE, 2, 2);
// Map Drupal's administrator role to Formulize's Webmasters group
Formulize::createResourceMapping(Formulize::GROUP_RESOURCE, 3, 1);
// Map Drupal's anonymous user role to Formulize's Anonymous User group
Formulize::createResourceMapping(Formulize::GROUP_RESOURCE, 1, 3);
// Map the rest of the Drupal roles
$result = db_query("SELECT r.rid, r.name FROM {role} r WHERE r.rid > 3 ORDER BY r.rid");
foreach ($result as $role) {
// yes, we are using the Drupal group name as the name, description and type for Formulize
Formulize::createGroup($role->rid, $role->name, $role->name, $role->name);
}
}
/**
* Sync Drupal users with Formulize
*/
function _formulize_sync_users()
{
if (!_formulize_integration_init())
return;
// Map Drupal's admin user to Formulize's admin user
Formulize::createResourceMapping(Formulize::USER_RESOURCE, 1, 1);
$result = db_query("SELECT u.uid FROM {users} u WHERE u.status = 1 AND u.uid > 1 ORDER BY u.uid");
foreach ($result as $record) {
formulize_user_insert(null, user_load($record->uid), null);
}
}
/**
* Insert a Drupal user into Formulize
*/
function formulize_user_insert($edit, $account, $category)
{
if (!_formulize_integration_init())
return;
$user_data = array(
'uid' => $account->uid,
'uname' => $account->name,
'login_name' => $account->name,
'name' => $account->name,
'pass' => $account->pass,
'email' => $account->mail,
'timezone_offset' => $account->timezone/60/60,
'language' => _formulize_convert_language($account->language),
'user_avatar' => 'blank.gif',
'theme' => 'impresstheme',
'level' => 1
);
$user = new FormulizeUser($user_data);
Formulize::createUser($user);
// Add user to groups
foreach ($account->roles as $roleid => $rolename) {
Formulize::addUserToGroup($account->uid, $roleid);
}
}
/**
* When a Drupal user changes, update in XOOPS
*/
function formulize_user_update($edit, $account, $category)
{
if (!_formulize_integration_init())
return;
$user_data = array(
'uname' => $account->name,
'login_name' => $account->name,
'name' => $account->name,
'pass' => $account->pass,
'email' => $account->mail,
'timezone_offset' => $account->timezone/60/60,
'language' => _formulize_convert_language($account->language)
);
Formulize::updateUser($account->uid, $user_data);
// Add user to groups they are now a part of
foreach ($account->roles as $roleid => $rolename) {
if (!isset($account->original->roles[$roleid])) {
Formulize::addUserToGroup($account->uid, $roleid);
}
}
// Remove user from groups they no longer belong to
foreach ($account->original->roles as $roleid => $rolename) {
if (!isset($account->roles[$roleid])) {
Formulize::removeUserFromGroup($account->uid, $roleid);
}
}
}
/**
* When a Drupal user is removed, delete in XOOPS
*/
function formulize_user_delete($account)
{
if (!_formulize_integration_init())
return;
Formulize::deleteUser($account->uid);
}
/**
* Convert language format of Drupal to XOOPS
*/
function _formulize_convert_language($account)
{
$languages = array('en'=>'english','fr'=>'french');
if (array_key_exists($account,$languages)) {
return $languages[$account];
}
return 'english';
}
/**
* This hook is called when a role is created in Drupal.
*/
function formulize_user_role_insert($role)
{
if (!_formulize_integration_init())
return;
// yes, we are using the Drupal group name as the name, description and type for Formulize
Formulize::createGroup($role->rid, $role->name, $role->name, $role->name);
}
/**
* When a Drupal role changes, update group in XOOPS
*/
function formulize_user_role_update($role)
{
if (!_formulize_integration_init())
return;
Formulize::renameGroup($role->rid, $role->name);
}
/**
* When a Drupal role is removed, delete the group in XOOPS
*/
function formulize_user_role_delete($role)
{
if (!_formulize_integration_init())
return;
Formulize::deleteGroup($role->rid);
}
/**
* Implements hook_regcode_used().
*
* Add the new user to the right group in Formulize corresponding to the role
*/
function formulize_regcode_used($edit, $account, $regcode)
{
if (!_formulize_integration_init())
return;
// Do nothing if the regcode is not valid
if (!is_object($regcode)) {
return;
}
// Grab applicable roles for category used
$rules = regcode_roles_get_rules($regcode->tags);
// Add user to role-groups the regcode allows
foreach ($rules as $rule) {
Formulize::addUserToGroup($account->uid, $rule['role_id']);
}
$settings = variable_get('regcode_og_settings', array());
// Add user to the og-groups the regcode allows
$tags = array_keys($regcode->tags);
foreach ($settings as $rule) {
if (in_array($rule['tid'], $tags)) {
foreach ($rule['ogs'] as $ogid) {
Formulize::addUserToGroup($account->uid, -1 * $ogid); // use negative group ids for OG groups, so there is no collisions with roles...should be another resource type but whatever
}
}
}
}