summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChip Kaye2012-11-15 18:08:25 (GMT)
committer Chip Kaye2012-11-15 18:08:25 (GMT)
commit123d6760b4dd2a5b82be0cbd2d04960a10b13963 (patch)
tree631fe843eb279b2599917f317af5bb94dd7886f0
parentb97a52ab258ee91313fc1af39b17cf329167c185 (diff)
Upgrade mm_webform module to D7
-rw-r--r--modules/mm_webform/mm_webform.info7
-rw-r--r--modules/mm_webform/mm_webform.install44
-rw-r--r--modules/mm_webform/mm_webform.module387
3 files changed, 438 insertions, 0 deletions
diff --git a/modules/mm_webform/mm_webform.info b/modules/mm_webform/mm_webform.info
new file mode 100644
index 0000000..fec5de8
--- /dev/null
+++ b/modules/mm_webform/mm_webform.info
@@ -0,0 +1,7 @@
+name = MM Webform
+description = Webform integration for Monster Menus
+package = Monster Menus
+version = "7.x-1.0"
+core = 7.x
+dependencies[] = monster_menus (1.x)
+dependencies[] = webform
diff --git a/modules/mm_webform/mm_webform.install b/modules/mm_webform/mm_webform.install
new file mode 100644
index 0000000..30357a7
--- /dev/null
+++ b/modules/mm_webform/mm_webform.install
@@ -0,0 +1,44 @@
+<?php
+
+/**
+ * @file
+ * mm_webform module install/schema hooks.
+ */
+
+/**
+ * Implementation of hook_schema().
+ */
+function mm_webform_schema() {
+ $schema = array();
+ $schema['webform_submitted_readers'] = array(
+ 'description' => 'Which MM groups are allowed to read a webform\'s submissions.',
+ 'fields' => array(
+ 'id' => array(
+ 'description' => 'Unique ID of this row',
+ 'type' => 'serial',
+ 'size' => 'big',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ ),
+ 'nid' => array(
+ 'description' => 'The node identifier of a webform',
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ 'default' => 0,
+ ),
+ 'gid' => array(
+ 'description' => 'The MM group identifier',
+ 'type' => 'int',
+ 'not null' => TRUE,
+ 'default' => 0,
+ ),
+ ),
+ 'indexes' => array(
+ 'nid' => array('nid'),
+ ),
+ 'primary key' => array('id'),
+ );
+
+ return $schema;
+}
diff --git a/modules/mm_webform/mm_webform.module b/modules/mm_webform/mm_webform.module
new file mode 100644
index 0000000..d077ad5
--- /dev/null
+++ b/modules/mm_webform/mm_webform.module
@@ -0,0 +1,387 @@
+<?php
+// $Id: mm_webform.module
+
+/**
+ * @file
+ * Webform hooks implemented by Monster Menus
+ */
+/**
+ * Implementation of hook_help().
+ */
+function mm_webform_help($section = 'admin/help#webform', $arg = NULL) {
+ switch ($section) {
+ case 'node/add/webform':
+ return t('Complete as much as you can of the information below, and click on Save. Then you will be able to add fields to your form.');
+
+ case 'node/add#webform':
+ return t('A webform can be a questionnaires, contact or request forms. It can be used to let visitors make contact, register for a event or to enable a complex survey.');
+
+ case 'node/%/edit/components':
+ $output = '<p>' . t('This page displays all the components currently configured for this webform node. You may add any number of components to the form, even multiple of the same type. To add a new component, fill in a name and select a type from the fields at the bottom of the table. Submit the form to create the new component or update any changed form values.') . '</p>';
+ $output .= '<p>' . t('Click on any existing component\'s name to edit its settings.') . '</p>';
+ return $output;
+ }
+ return '';
+}
+
+/**
+ * Implementation of hook_menu().
+ */
+function mm_webform_menu() {
+ $items = array();
+ $items['node/%webform_menu/webform/edit'] = array(
+ 'title' => 'Edit',
+ 'page callback' => 'node_page_edit',
+ 'page arguments' => array(1),
+ 'access callback' => 'node_access',
+ 'access arguments' => array('update', 1),
+ 'file' => 'node.pages.inc',
+ 'file path' => drupal_get_path('module', 'node'),
+ 'weight' => 0,
+ 'type' => MENU_LOCAL_TASK);
+ return $items;
+}
+
+/**
+ * Implementation of hook_mm_menu_alter().
+ */
+function mm_webform_mm_menu_alter(&$items) {
+ foreach ($items as $path => $item) {
+ if ($path == 'node/%webform_menu/webform' || strpos($path, 'node/%webform_menu/webform/') === 0) {
+ $items[$path]['weight'] = isset($items[$path]['weight']) ? $items[$path]['weight'] + 1 : 1;
+ }
+ }
+
+ $items['node/%webform_menu/webform/emails-delete/%webform_menu_email'] = $items['node/%webform_menu/webform/emails/%webform_menu_email/delete'];
+ $items['node/%webform_menu/webform/emails-delete/%webform_menu_email']['page callback'] = 'drupal_get_form';
+ $items['node/%webform_menu/webform/components-clone/%webform_menu_component'] = $items['node/%webform_menu/webform/components/%webform_menu_component/clone'];
+ $items['node/%webform_menu/webform/components-delete/%webform_menu_component'] = $items['node/%webform_menu/webform/components/%webform_menu_component/delete'];
+ unset($items['node/%webform_menu/webform/components/%webform_menu_component/clone']);
+ unset($items['node/%webform_menu/webform/components/%webform_menu_component/delete']);
+ unset($items['node/%webform_menu/webform/emails/%webform_menu_email/delete']);
+}
+
+/**
+ * Implementation of hook_mm_url_rewrite_outbound().
+ */
+function mm_webform_mm_url_rewrite_outbound($mmtid, &$path, &$options, $original_path) {
+ $opath = explode('/', $original_path);
+ if (isset($opath[0]) && $opath[0] == 'node' && isset($opath[2]) && $opath[2] == 'webform' && isset($opath[3]) && ($opath[3] == 'components' || $opath[3] == 'emails') && isset($opath[5]) && ($opath[5] == 'clone' || $opath[5] == 'delete')) {
+ $parr = explode('/', $path); //path is prepended with mm/mmtid
+
+ if ($opath[3] == 'emails' && $opath[5] == 'delete') {
+ $parr[5] = 'emails-delete';
+ }
+ else {
+ $parr[5] = $opath[5] == 'delete' ? 'components-delete' : 'components-clone';
+ }
+
+ unset($parr[7]);
+ $path = implode('/', $parr);
+ }
+}
+
+/**
+ * Implementation of hook_form_alter().
+ */
+function mm_webform_form_alter(&$form, &$form_state, $form_id) {
+ if (isset($form['#id']) && $form['#id'] == 'node-form' && isset($form['#node']) &&
+ $form['#node']->type && in_array($form['#node']->type, webform_variable_get('webform_node_types')) &&
+ $form['#node']->type != 'webform' && stripos($_GET['q'], '/edit') + strlen('/edit') == strlen($_GET['q']) &&
+ user_access('create webform content')) {
+ $webform_url = url(str_replace('/edit', '/webform', $_GET['q']));
+ $form['buttons']['edit_webform'] = array(
+ '#type' => 'markup',
+ '#weight' => $form['buttons']['delete']['#weight'] + 1,
+ '#value' => check_plain('<input ' . drupal_attributes(array('type' => 'button', 'value' => t('Attach/Edit Webform'))) . 'class="form-submit" onClick="window.location = \'' . $webform_url . '\';" />'),
+ );
+ }
+
+ if ($form_id == 'webform_configure_form') {
+ $node = node_load($form['nid']['#value']);
+ $everyone = FALSE;
+ $grouplist = array();
+ $userlist = array();
+
+ if ($node->nid) {
+ $everyone = $node->sub_others;
+ $grouplist = $node->sub_groups;
+ $userlist = $node->sub_users;
+ }
+ if (count($userlist) || count($grouplist)) {
+ $everyone = FALSE;
+ }
+
+ $form['submission_readers'] = array(
+ '#type' => 'fieldset',
+ '#weight' => -99,
+ '#title' => t('Who can read data submitted to this webform'),
+ '#description' => t("Only the webform's editors and those people listed here can view what users post using the webform."),
+ '#collapsible' => TRUE,
+ '#collapsed' => FALSE
+ );
+ $form['submission_readers']['sub_groups'] = array(
+ '#type' => 'mm_grouplist',
+ '#mm_list_other_id' => 'edit-sub_others',
+ '#default_value' => $grouplist
+ );
+ $form['#submit'][] = '_mm_webform_node_insert_update';
+
+ _mm_ui_userlist_setup($userlist, $form['submission_readers'],
+ 'sub_users', t('Individual users:'), FALSE, '', 'edit-sub_others');
+
+ $form['submission_readers']['sub_others'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Everyone'),
+ '#default_value' => $everyone,
+ '#attributes' => array('onchange' => "if (this.checked) { this.form['sub_groups'].delAll(); this.form['sub_users'].delAll();} return true;")
+ );
+ }
+}
+
+/**
+ * Implementation of hook_link_alter().
+ */
+function mm_webform_link_alter(&$links, $node, $comment = NULL) {
+ if (isset($node->type) && $node->type === 'webform') {
+ foreach ($links as &$link) {
+ if ($link['title'] == 'Edit') {
+ $link['href'] = str_replace('/edit', '/webform/edit', $link['href']);
+ }
+ }
+ }
+}
+
+/**
+ * Implementation of hook_file_download().
+ */
+function mm_webform_file_download($uri) {
+ $result = db_query(
+ 'SELECT f.*, d.nid FROM {file_managed} f ' .
+ 'INNER JOIN {webform_submitted_data} d ON f.fid = d.data ' .
+ "INNER JOIN {webform_component} c ON d.nid = c.nid AND c.type = 'file' " .
+ "WHERE f.uri = :uri", array(':uri' => $uri));
+ foreach ($result as $filerec) {
+ if (($node = node_load($filerec->nid)) && in_array($node->type, webform_variable_get('webform_node_types'))) {
+ $file_path = drupal_realpath($uri);
+ if (!file_exists($file_path)) {
+ // don't attempt to deliver if file doesn't exist
+ return;
+ }
+ if (webform_results_access($node)) {
+ return array(
+ 'Content-Type' => mime_header_encode($filerec->filemime),
+ 'Content-Disposition' => 'attachment; filename="' . mime_header_encode($filerec->filename) . '"',
+ 'Content-Length' => $filerec->filesize,
+ 'Pragma' => 'cache',
+ 'Cache-Control' => 'private',
+ );
+ }
+ else {
+ return -1;
+ }
+ }
+ }
+}
+
+/**
+ * Implementation of hook_link().
+ * Always add a "view form" link.
+ */
+function mm_webform_link($type, $node = NULL, $teaser = FALSE) {
+ global $user;
+ if ($teaser && isset($node->type) && $node->type != 'webform')
+ return;
+ $links = array();
+ if (isset($node->type) && in_array($node->type, webform_variable_get('webform_node_types'))) {
+ if (webform_results_access($node, $user)) {
+ $links[] = array(
+ 'title' => t('View submissions'),
+ 'href' => "node/$node->nid/webform-results",
+ 'attributes' => array('title' => t('View data posted using this webform'))
+ );
+ }
+ }
+ return $links;
+}
+
+/**
+ * Implementation of hook_webform_results_clear_access().
+ */
+function mm_webform_webform_results_clear_access($node, $user) {
+ return _mm_webform_results_access($node, $user, 'clear');
+}
+
+/**
+ * Implementation of hook_webform_results_access().
+ */
+function mm_webform_webform_results_access($node, $user) {
+ return _mm_webform_results_access($node, $user, 'read');
+}
+
+/**
+ * Implementation of hook_webform_submission_access().
+ */
+function mm_webform_webform_submission_access($node, $submission, $op, $account) {
+ return _mm_webform_results_access($node, $account, 'read');
+}
+
+function _mm_webform_results_access($node, $user, $mode) {
+ static $cache = array();
+ global $user;
+ $nid = is_object($node) ? $node->nid : $node;
+
+ if (!isset($cache[$nid][$mode])) {
+ $cache[$nid][$mode] = FALSE;
+ if (!is_object($node)) {
+ $node = node_load($nid);
+ }
+ if (user_access('administer nodes') || mm_content_node_access('update', $node)) {
+ // admin user or editor of this node
+ $cache[$nid][$mode] = TRUE;
+ }
+ elseif ($mode == 'read' || $mode == 'clear') {
+ if (!user_access($mode == 'read' ? 'access own webform results' : 'delete own webform submissions')) {
+ // user does not have role-based permission
+ $cache[$nid][$mode] = FALSE;
+ }
+ else {
+ // everyone, or this user is in a matching group; can also include the
+ // anonymous user (sr.gid = 0) if "everyone" is checked
+ $cache[$nid]['read'] = $cache[$nid]['clear'] = db_query(
+ 'SELECT COUNT(DISTINCT sr.gid) FROM {webform_submitted_readers} sr ' .
+ 'LEFT JOIN {mm_group} g ON sr.gid = g.gid ' .
+ 'LEFT JOIN {mm_virtual_group} v ON v.vgid = g.vgid ' .
+ 'WHERE sr.nid = :nid AND (sr.gid = 0 OR v.uid = :uid1 OR g.vgid = 0 AND g.uid = :uid2)',
+ array(':nid' => $nid, ':uid1' => $user->uid, ':uid2' => $user->uid))->fetchField() != 0;
+ }
+ }
+ }
+ return $cache[$nid][$mode];
+}
+
+/**
+* Implements hook_node_view().
+*/
+function mm_webform_node_view($node, $view_mode, $langcode) {
+ if (!empty($node->type) && in_array($node->type, webform_variable_get('webform_node_types'))) {
+ if (!mm_content_user_can_node($node, 'r')) {
+ mm_node_load_fail($node, $teaser);
+ }
+ }
+}
+
+function _mm_webform_node_insert_update($form, &$form_state) {
+ if ($form_state['values']['sub_others']) {
+ _mm_webform_delete_submitted_readers($form_state['values']['nid']);
+
+ // INSERT INTO {webform_submitted_readers} (nid, gid)
+ db_insert('webform_submitted_readers')
+ ->fields(array(
+ 'nid' => $form_state['values']['nid'],
+ 'gid' => 0,
+ ))
+ ->execute();
+ //db_query('INSERT INTO {webform_submitted_readers} (nid, gid) VALUES(:val1, :val2)', array(':val1' => $form_state['values']['nid'], ':val2' => 0));
+ }
+ else {
+ _mm_webform_delete_submitted_readers($form_state['values']['nid']);
+ foreach ($form_state['values']['sub_groups'] as $gid => $name)
+ if ($gid) {
+ // INSERT INTO {webform_submitted_readers} (nid, gid)
+ db_insert('webform_submitted_readers')
+ ->fields(array(
+ 'nid' => $form_state['values']['nid'],
+ 'gid' => $gid,
+ ))
+ ->execute();
+ }
+ //if ($gid) db_query('INSERT INTO {webform_submitted_readers} (nid, gid) VALUES(:val1, :val2)', array(':val1' => $form_state['values']['nid'], ':val2' => $gid));
+
+ $adhoc_gid = '';
+ foreach ($form_state['values']['sub_users'] as $uid => $name)
+ if (isset($uid)) _mm_content_ad_hoc_group($adhoc_gid, $uid);
+ if ($adhoc_gid != '') {
+ // INSERT INTO {webform_submitted_readers} (nid, gid)
+ db_insert('webform_submitted_readers')
+ ->fields(array(
+ 'nid' => $form_state['values']['nid'],
+ 'gid' => $adhoc_gid,
+ ))
+ ->execute();
+ }
+ //if ($adhoc_gid != '') db_query('INSERT INTO {webform_submitted_readers} (nid,gid) VALUES(:val1, :val2)', array(':val1' => $form_state['values']['nid'], ':val2' => $adhoc_gid));
+ }
+}
+
+/**
+* Implementation of hook_node_delete().
+*/
+function mm_webform_node_delete($node) {
+ if (!empty($node->type) && in_array($node->type, webform_variable_get('webform_node_types'))) {
+ _mm_webform_delete_submitted_readers($node->nid);
+ }
+}
+
+function _mm_webform_delete_submitted_readers($nid) {
+ // first delete individual users and "Everyone"
+ // DELETE sr,g FROM {webform_submitted_readers} sr LEFT JOIN {mm_group} g ON sr.gid = g.gid WHERE sr.nid = $nid AND sr.gid <= 0
+ $select = db_select('webform_submitted_readers', 'sr');
+ $select->leftJoin('mm_group', 'g', 'g.gid = sr.gid');
+ $select_gids = $select->fields('sr', array('gid'))
+ ->condition('sr.nid', $nid)
+ ->condition('sr.gid', 0, '<=')
+ ->execute()
+ ->fetchCol();
+ if (!empty($select_gids)) {
+ db_delete('webform_submitted_readers')
+ ->condition('gid', $select_gids, 'IN')
+ ->execute();
+ db_delete('mm_group')
+ ->condition('gid', $select_gids, 'IN')
+ ->execute();
+ }
+
+ // then delete any groups if they exist
+ // DELETE FROM {webform_submitted_readers} WHERE nid = $nid
+ db_delete('webform_submitted_readers')
+ ->condition('nid', $nid)
+ ->execute();
+}
+
+/**
+* Implements hook_node_load().
+*/
+function mm_webform_node_load($nodes, $types) {
+ foreach ($nodes as $node) {
+ if (!empty($node->type) && in_array($node->type, webform_variable_get('webform_node_types'))) {
+ $node->sub_others = FALSE;
+ $node->sub_groups = array();
+ $node->sub_users = array();
+ $result = db_query(
+ 'SELECT r.gid,t.name FROM {webform_submitted_readers} r ' .
+ 'LEFT JOIN {mm_tree} t ON r.gid = t.mmtid ' .
+ 'WHERE r.gid >= :gid AND nid = :nid ORDER BY t.name', array(':gid' => 0, ':nid' => $node->nid));
+ foreach ($result as $r) {
+ if ($r->gid == 0) {
+ $node->sub_others = TRUE;
+ }
+ else {
+ $node->sub_groups[$r->gid] = $r->name;
+ }
+ }
+
+ $result = db_query('SELECT gid FROM {webform_submitted_readers} WHERE gid < :gid AND nid = :nid', array(':gid' => 0, ':nid' => $node->nid));
+ foreach ($result as $r) {
+ $users = mm_content_get_users_in_group($r->gid, NULL, TRUE, MM_UI_MAX_USERS_IN_GROUP);
+ if (!is_null($users)) {
+ $node->sub_users = $users;
+ }
+ }
+
+ if (isset($users) && count($users) || count($node->sub_groups)) {
+ $node->sub_others = FALSE;
+ }
+ }
+ }
+}