Index: webform_report.module
===================================================================
--- webform_report.module	(revision 93)
+++ webform_report.module	(working copy)
@@ -124,100 +124,36 @@
  * Implementation of hook_load
  */
 function webform_report_load($node) {
-  $webform_report = NULL;
-  if(!isset($_POST['wnid'])) {
-    $result = db_query("SELECT r.nid, r.wnid, rc.cid, c.name, r.kcid, r.description, r.sort, r.filter_type, r.filter_value,
-                        r.options, r.results_per_page FROM {webform_report} r LEFT JOIN {webform_report_component} rc
-                        ON r.nid = rc.nid LEFT JOIN {webform_component} c ON rc.cid = c.cid WHERE rc.nid = '$node->nid' ORDER BY
-                        c.weight");
-
-    if (db_num_rows($result) > 0) {
-      while ($row = db_fetch_object($result)) {
-        // check if variables are already set so we don't set them twice
-        if (!isset($webform_report->wnid)) {
-          $webform_report->wnid = $row->wnid;
+  
+    // load current node values
+    if ($webform_report = db_fetch_object(db_query("SELECT * FROM {webform_report} WHERE nid = %d", $node->nid))) {
+        // get options array
+        $webform_report->options = unserialize($webform_report->options);
+        // get components
+        $webform_report->components = array();
+        $rs = db_query("SELECT cid FROM {webform_report_component} WHERE nid = %d", $node->nid);
+        while ($row = db_fetch_object($rs)) {
+            $webform_report->components[] = $row->cid;
         }
-        if (!isset($webform_report->kcid)) {
-          $webform_report->kcid = $row->kcid;
-        }
-        if (!isset($webform_report->sort)) {
-          $webform_report->sort = $row->sort;
-        }
-        if (!isset($webform_report->filter_type)) {
-          $webform_report->filter_type = $row->filter_type;
-        }
-        if (!isset($webform_report->filter_value)) {
-          $webform_report->filter_value = $row->filter_value;
-        }
-        if (!isset($webform_report->description)) {
-          $webform_report->description = $row->description;
-        }
-        // this array will be appended multiple times, so no pre-existence check
-        $webform_report->components[] = $row->cid;
-        if (!isset($webform_report->options)) {
-          $webform_report->options = unserialize(stripslashes($row->options));
-        }
-        if (!isset($webform_report->results_per_page)) {
-          $webform_report->results_per_page = $row->results_per_page;
-        }
-      }
     }
+    // establish defaults for new node
     else {
-      webform_report_prepare($node);
+        $webform_report = new stdClass();
+        $webform_report->wnid = 0;
+        $webform_report->kcid = 0;
+        $webform_report->description = '';
+        $webform_report->sort = 0;
+        $webform_report->options = array();
+        $webform_report->filter_type = 0;
+        $webform_report->filter_value = '';
+        $webform_report->results_per_page = 20;
+        $webform_report->components = array();
     }
-  }
-  else {
-    $webform_report->wnid = $_POST['wnid'];
-    $webform_report->kcid = $_POST['kcid'];
-    $webform_report->sort = $_POST['sort'];
-    $webform_report->filter_type = $_POST['filter_type'];
-    $webform_report->filter_value = $_POST['filter_value'];
-    $webform_report->description = $_POST['description'];
-    $webform_report->components = $_POST['components'];
-    $webform_report->options = $_POST['options'];
-    $webform_report->results_per_page = $_POST['results_per_page'];
-  }
-
-  return $webform_report;
+  
+    return $webform_report;
 }
 
 /**
- * Implementation of hook_prepare
- */
-function webform_report_prepare(&$node) {
-  if (!isset($node->wnid)) {
-    $node->wnid = $_POST['wnid'];
-  }
-  if (!isset($node->kcid)) { 
-    $node->kcid = $_POST['kcid'];
-  }
-  if (!isset($node->kcid)) { 
-    $node->sort = $_POST['sort'];
-  }
-  if (!isset($node->filter_type)) { 
-    $node->filter_type = $_POST['filter_type'];
-  }
-  if (!isset($node->filter_value)) { 
-    $node->filter_value = $_POST['filter_value'];
-  }
-  if (!isset($node->description)) {
-    $node->description = $_POST['description'];
-  }
-  if (!isset($node->components)) {
-    $node->components = $_POST['components'];
-  }
-  if (!isset($node->options)) {
-    $node->options['show_date'] = $_POST['show_date'];
-    $node->options['show_user'] = $_POST['show_user'];
-    $node->options['show_ip'] = $_POST['show_ip'];
-    $node->options['show_edit'] = $_POST['show_edit'];
-  }
-  if (!isset($node->results_per_page)) {
-    $node->results_per_page = $_POST['results_per_page'];
-  }
-}
-
-/**
  * Implementation of hook_view
  */
 function webform_report_view($node, $teaser = 0, $page = 0) {
@@ -227,7 +163,7 @@
   }
 
   $data = _webform_report_get_data($node);
-  $output = "<p>" . $node->description;
+  $output = "<p>" . check_markup($node->description);
   $output .= _webform_report_get_body_content($data, $node);
   $node->content['body'] = array('#value' => check_markup($node->body, $node->format, FALSE));
   $node->content['webform_report'] = array('#value' => $output, '#weight' => 10);
@@ -237,61 +173,71 @@
 /**
  * Implementation of hook_validate
  */
-function webform_report_validate($node, &$form) {
-  if ($_POST['wnid'] == 0) {
-    form_set_error('wnid', t('You must select a form'));
-  } else {
-    if ($_POST['kcid'] == '') {
-      form_set_error('kcid', t('You must select a field'));
-    } else {
-      if($_POST['filter_type'] != 0 && $_POST['filter_value'] == '') {
+function webform_report_validate(&$node) {
+
+    if ($node->wnid <= 0) {
+        form_set_error('wnid', t('You must select a webform in order to continue'));
+    }
+    if ($node->filter_type != 0 && $node->filter_value == '') {
         form_set_error('filter_value', t('You must specify a filter value'));
-      } else {
-        if ($_POST['components'] == '') {
-          form_set_error('components', t('You must select at least one component  '));
-        }
-      }
-    }        
-  }
+    }
 }
 
+
 /**
  * Implementation of hook_insert
  */
-function webform_report_insert($node, $log = TRUE) {
-  webform_report_prepare($node);
-  db_query("INSERT INTO {webform_report} (nid, wnid, kcid, description, sort, filter_type, filter_value, options, results_per_page)
-            VALUES (%d, %d, %d, '%s', %d, '%s', '%s', '%s', %d)", $node->nid, $node->wnid, $node->kcid, $node->description, $node->sort,
-            $node->filter_type, $node->filter_value, addslashes(serialize($node->options)), $node->results_per_page);
-  foreach ($node->components as $cid) {
-    if($cid != 0) { // omit unselected components
-      db_query("INSERT INTO {webform_report_component} (nid, cid) VALUES ('$node->nid', '$cid')");
-    } 
-  }
-  if ($log) {
-    watchdog('webform_report', 'Webform report "'.$node->title.'" added', WATCHDOG_NOTICE);
-  }
+function webform_report_insert($node, $log = TRUE, $insert_components = TRUE) {
+  
+    db_query("INSERT INTO {webform_report} (nid, wnid, kcid, description, sort, filter_type, filter_value, options, results_per_page)
+              VALUES (%d, %d, %d, '%s', %d, '%s', '%s', '%s', %d)", 
+              $node->nid, $node->wnid, $node->kcid, $node->description, $node->sort,
+              $node->filter_type, $node->filter_value, serialize($node->options), $node->results_per_page);
+    if ($insert_components)
+        _webform_report_insert_components($node);
+    if ($log) {
+        watchdog('webform_report', t('Webform report @title added', array('@title' => $node->title)), WATCHDOG_NOTICE);
+    }
 }
 
+function _webform_report_insert_components($node) {
+  
+    foreach ($node->components as $cid) {
+        if($cid != 0) { // omit unselected components
+            db_query("INSERT INTO {webform_report_component} (nid, cid) VALUES (%d, %d)", $node->nid, $cid);
+        } 
+    }
+}
+
 /**
  * Implementation of hook_update
  */
 function webform_report_update($node, $log = TRUE) {
-  webform_report_delete($node, FALSE);
-  webform_report_insert($node, FALSE);
-  if ($log) {
-    watchdog('webform_report', 'Webform report "'.$node->title.'" updated', WATCHDOG_NOTICE);
-  }
+
+    // see if webform node id changed, if so remove components and key component
+    $wnid = db_result(db_query("SELECT wnid FROM {webform_report} WHERE nid = %d", $node->nid));
+    if ($wnid != $node->wnid) {
+        $node->kcid = 0;
+        webform_report_delete($node, FALSE);
+        webform_report_insert($node, FALSE, FALSE);
+    }
+    else {
+        webform_report_delete($node, FALSE);
+        webform_report_insert($node, FALSE);
+    }
+    if ($log) {
+        watchdog('webform_report', t('Webform report @title updated', array('@title' => $node->title)), WATCHDOG_NOTICE);
+    }
 }
 
 /**
  * Implementation of hook_delete
  */
 function webform_report_delete($node, $log = TRUE) {
-  db_query ("DELETE FROM {webform_report} WHERE nid = '$node->nid'");
-  db_query ("DELETE FROM {webform_report_component} WHERE nid = '$node->nid'");
+  db_query ("DELETE FROM {webform_report} WHERE nid = %d", $node->nid);
+  db_query ("DELETE FROM {webform_report_component} WHERE nid = %d", $node->nid);
   if ($log) {
-    watchdog('webform_report', 'Webform report "'.$node->title.'" deleted', WATCHDOG_NOTICE);
+    watchdog('webform_report', t('Webform report @title deleted', array('@title' => $node->title)), WATCHDOG_NOTICE);
   }
 }
 
@@ -299,180 +245,204 @@
  * Implementation of hook_form
  */
 function webform_report_form(&$node) {
-  $form['webform_report'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Webform report'),
-    '#collapsible' => TRUE,
-    '#collapsed' => FALSE,
-    '#weight' => -3);
 
-  $form['webform_report']['title'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Title'),
-    '#required' => TRUE,
-    '#default_value' => t($node->title),
-    '#weight' => -2,
-    '#description' => t('Enter a title for your Webform report page')
-  );
+    $form['webform_report'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Webform report'),
+      '#collapsible' => TRUE,
+      '#collapsed' => FALSE,
+      '#weight' => -3);
 
-  if($_POST['description']) {
-    $value = $_POST['description'];
-  }
-  else {
-    $value = $node->description;
-  }
-  $form['webform_report']['description'] = array(
-    '#type' => 'textarea',
-    '#title' => t('Description'),
-    '#description' => t('The description will be displayed at the top of the report page'),
-    '#value' => t($value),
-    '#required' => FALSE,
-    '#weight' => -1,
-  );
+    $form['webform_report']['title'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Title'),
+      '#required' => TRUE,
+      '#default_value' => $node->title,
+      '#weight' => -2,
+      '#description' => t('Enter a title for your Webform report page')
+    );
 
-  if (isset($node->wnid)) {
-    $default = $node->wnid;
-  }
-  else {
-    $default = array('0' => t('Select a webform'));
-  }
-  $options = _webform_report_get_webforms(t('Select a webform'));
-  $form['webform_report']['wnid'] = array(
-    '#type' => 'select',
-    '#title' => t("Webform"),
-    '#default_value' => $default,
-    '#options' => $options,
-    '#attributes' => array('onchange' => 'this.form.submit()'),
-    '#required' => TRUE,
-    '#weight' => 0,
-  );
-  unset($default, $options);
+    $form['webform_report']['description'] = array(
+      '#type' => 'textarea',
+      '#title' => t('Description'),
+      '#description' => t('The description will be displayed at the top of the report page'),
+      '#default_value' => $node->description,
+      '#required' => FALSE,
+      '#weight' => -1,
+    );
 
-  if (isset($node->wnid) && $node->wnid != 0) {
-    if (isset($node->kcid)) {
-      $default = $node->kcid;
+    if (!isset($node->wnid)) {
+        $options = _webform_report_get_webforms(t('Select a webform'));
+        $desc = t('Select the webform to be used by this webform report - further options will appear after the report is created.');
     }
     else {
-      $default = t('Select a field');
+        $options = _webform_report_get_webforms();
+        $desc = t('If the webform is updated, you must Submit the report in order to update the component list below.');
     }
-    $options = _webform_report_get_components($node->wnid);
-    $form['webform_report']['kcid'] = array(
+    $form['webform_report']['wnid'] = array(
       '#type' => 'select',
-      '#title' => t("Key field"),
-      '#default_value' => $default,
+      '#title' => t("Webform"),
+      '#default_value' => $node->wnid,
       '#options' => $options,
-      '#attributes' => array('onchange' => 'this.form.submit()'),
       '#required' => TRUE,
-      '#weight' => 1,
+      '#weight' => 0,
+      '#description' => $desc,
     );
-    unset($default, $options);
+    unset($options);
 
-    $options = array(SORT_ASC => t('Ascending'), SORT_DESC => t('Descending'));
-    $form['webform_report']['sort'] = array(
-      '#type' => 'select',
-      '#title' => t("Sort order"),
-      '#options' => $options,
-      '#default_value' => $node->sort,
-      '#weight' => 2,
-    );
-  }
-  unset($default, $options);
+    // other options cannot be displayed until webform is known
+    if (isset($node->wnid)) {
 
-  if (isset($node->kcid) && $node->kcid != '0') {
-    $options = array(0 => t('none'), 1 => t('begins with'), 2 => t('does not begin with'), 3 => t('contains'), 4 => t('does not contain'));
-    $form['webform_report']['filter_type'] = array(
-      '#type' => 'select',
-      '#title' => t("Filter Type"),
-      '#options' => $options,
-      '#attributes' => array('onchange' => 'this.form.submit()'),
-      '#default_value' => $node->filter_type,
-      '#weight' => 3,
-    );
-  }
-  unset($options);
+        $options = _webform_report_get_components($node->wnid);
+        $form['webform_report']['kcid'] = array(
+          '#type' => 'select',
+          '#title' => t("Key field"),
+          '#default_value' => $node->kcid,
+          '#options' => $options,
+          '#required' => TRUE,
+          '#weight' => 1,
+        );
+        unset($options);
 
-  if (isset($node->kcid) && $node->kcid != '0' && $node->filter_type != 0) {
-    $form['webform_report']['filter_value'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Filter Value'),
-      '#description' => t('Filter the key field using the specified value'),
-      '#default_value' => t($node->filter_value),
-      '#required' => TRUE,
-      '#weight' => 4
-    );
-  }
+        $options = array(SORT_ASC => t('Ascending'), SORT_DESC => t('Descending'));
+        $form['webform_report']['sort'] = array(
+          '#type' => 'select',
+          '#title' => t("Sort order"),
+          '#options' => $options,
+          '#default_value' => $node->sort,
+          '#weight' => 2,
+        );
+        unset($options);
 
-  if (isset($node->kcid) && $node->kcid != '0') {
-    $options = _webform_report_get_components($node->wnid);
-    $form['webform_report']['components'] = array(
-      '#type' => 'checkboxes',
-      '#title' => t("Include components"),
-      '#description' => t('Select all of the components to include in this report and click Preview to display the results'),
-      '#options' => $options,
-      '#default_value' => $node->components,
-      '#required' => TRUE,
-      '#weight' => 5,
-    );
-  }
-  unset($options);
+        $options = array(0 => t('none'), 1 => t('begins with'), 2 => t('does not begin with'), 3 => t('contains'), 4 => t('does not contain'));
+        $form['webform_report']['filter_type'] = array(
+           '#type' => 'select',
+           '#title' => t("Filter Type"),
+           '#options' => $options,
+           '#default_value' => $node->filter_type,
+           '#weight' => 3,
+        );
+        unset($options);
 
-  if (isset($node->kcid) && $node->kcid != '0') {
-    $options = array('show_date' => t('Show date'), 'show_time' => t('Show time'), 'show_user' => t('Show user'), 'show_ip' => t('Show IP Address'), 'show_edit' => t('Show edit link'));
-    $form['webform_report']['options'] = array(
-      '#type' => 'checkboxes',
-      '#title' => t('Display options'),
-      '#description' => t('Select display options for this report and click Preview to display the results'),
-      '#options' => $options,
-      '#default_value' => $node->options,
-      '#required' => FALSE,
-      '#weight' => 6,
-    );
-  }
-  unset($options);
+        $form['webform_report']['filter_value'] = array(
+            '#type' => 'textfield',
+            '#title' => t('Filter Value'),
+            '#description' => t('Filter the key field using the specified value'),
+            '#default_value' => t($node->filter_value),
+            '#required' => FALSE,
+            '#weight' => 4
+        );
 
-  if (isset($node->kcid) && $node->kcid != '') {
-    if (isset($node->results_per_page)) {
-      $default = $node->results_per_page;
+        $options = _webform_report_get_components($node->wnid);
+        $form['webform_report']['components'] = array(
+            '#type' => 'checkboxes',
+            '#title' => t("Include components"),
+            '#description' => t('Select all of the components to include in this report and click Preview to display the results'),
+            '#options' => $options,
+            '#default_value' => $node->components,
+            '#required' => TRUE,
+            '#weight' => 5,
+        );
+        unset($options);
+
+        $options = array('show_date' => t('Show date'), 'show_time' => t('Show time'), 'show_user' => t('Show user'), 'show_ip' => t('Show IP Address'), 'show_edit' => t('Show edit link'));
+        $form['webform_report']['options'] = array(
+            '#type' => 'checkboxes',
+            '#title' => t('Display options'),
+            '#description' => t('Select display options for this report and click Preview to display the results'),
+            '#options' => $options,
+            '#default_value' => $node->options,
+            '#required' => FALSE,
+            '#weight' => 6,
+        );
+        unset($options);
+
+        $options = array(20 => '20', 40 => '40', 60 => '60', 80 => '80', 100 => '100');
+        $form['webform_report']['results_per_page'] = array(
+            '#type' => 'select',
+            '#title' => t('Results per page'),
+            '#options' => $options,
+            '#default_value' => $node->results_per_page,
+            '#weight' => 7,
+        );
+        unset($options);
+        
     }
     else {
-      $default = 20;
+        $form['webform_report']['next'] = array(
+          '#type' => 'submit',
+          '#value' => t(' Next-> '),
+          '#weight' => 8,
+        );    
     }
-    $options = array(20 => '20', 40 => '40', 60 => '60', 80 => '80', 100 => '100');
-    $form['webform_report']['results_per_page'] = array(
-      '#type' => 'select',
-      '#title' => t('Results per page'),
-      '#options' => $options,
-      '#default_value' => $default,
-      '#weight' => 7,
-    );
-  }
-  unset($options, $default);
+    
+    return $form;
+}
 
-  if (!isset($node->kcid) || $node->kcid == '') {
-    $form['webform_report']['next'] = array(
-      '#type' => 'button',
-      '#value' => t(' Next > '),
-      '#weight' => 8,
-      '#attributes' => array('onclick' => 'this.form.submit()'),      
-    );
-  }
+/**
+ * These next two functions were adapted from the Webform module in order to redirect
+ * the user back to edit form after webform_report node creation and to leave the 
+ * node initially unpublished.
+ */
+ 
+/**
+ * Implementation of hook_form_alter().
+ */
+function webform_report_form_alter($form_id, &$form) {
 
-  return $form;
+    if ($form_id == 'webform_report_node_form' && empty($form['nid']['#value'])) {
+        $form['#submit']['webform_report_form_submit'] = array();
+    }
+
+    if (user_access('administer nodes')) {
+      $form['options']['status']['#default_value'] = FALSE;
+    }
+    else {
+      $form['status']['#value'] = FALSE;
+    }
 }
 
 /**
+ * Implementation of hook_form_submit().
+ */
+function webform_report_form_submit($form_id, $form_values) {
+
+    // get last inserted nodeid
+    $nid = db_result(db_query_range("SELECT nid FROM {node} WHERE type = 'webform_report' ORDER BY nid DESC", 0, 1));
+
+    // remove any messages
+    unset($_SESSION['messages']['status']);
+
+    drupal_set_message(t('The new webform report %title has been created. Select the fields that you would like to appear on the report below.', array('%title' => $form_values['title'])));
+
+    if (!$form_values['status']) {
+        drupal_set_message(t('This webform report is currently unpublished. After finishing your changes to the report, use the <em>Publish</em> button below.'));
+    }
+  
+    // redirect user to the edit form again
+    return 'node/'. $nid .'/edit';
+}
+
+/**
  * Searches for nodes of type webform and returns their nids and titles
  * in an associative array
  */
 function _webform_report_get_webforms($first = NULL) {
-  $result = db_query("SELECT nid FROM {node} WHERE type = 'webform'");
 
-  $webforms = array(0 => $first);
-  while ($row = db_fetch_object($result)) {
-    $webform_report = node_load($row->nid); // load the current version of this node
-    $webforms[$webform_report->nid] = t($webform_report->title);
-  }
-  return $webforms;
+    // only list webforms that have components  
+    $result = db_query("SELECT nid FROM {node} WHERE type = 'webform' AND nid in (SELECT DISTINCT nid FROM {webform_component})");
+
+    // insert an optional first option
+    if (isset($first))
+        $webforms = array(0 => $first);
+    else
+        $webforms = array();
+        
+    while ($row = db_fetch_object($result)) {
+        $webform_report = node_load($row->nid); // load the current version of this node
+        $webforms[$webform_report->nid] = t($webform_report->title);
+    }
+    return $webforms;
 } // end function _webform_report_get_webforms()
 
 /**
@@ -491,7 +461,7 @@
     $components = array();
   }
   
-  $result = db_query("SELECT c.cid, c.name FROM {webform_component} c WHERE c.type <> 'fieldset' AND c.nid = '$nid' ORDER BY c.weight");
+  $result = db_query("SELECT c.cid, c.name FROM {webform_component} c WHERE c.type <> 'fieldset' AND c.nid = %d ORDER BY c.weight", $nid);
   
   while ($row = db_fetch_object($result)) {
     $component_name = substr($row->name, 0, 65);
@@ -525,16 +495,16 @@
       $count++;
     }
     $query .= "'";
-
+    
     $result = db_query("SELECT w.nid, c.name, c.cid, c.type, d.nid, d.sid, d.data, s.uid, u.name as user, s.submitted, s.remote_addr
                         FROM {webform} w
                         LEFT JOIN {webform_submitted_data} d ON w.nid = d.nid
                         LEFT JOIN {webform_component} c ON d.cid = c.cid
                         LEFT JOIN {webform_submissions} s ON d.sid = s.sid
                         LEFT JOIN {users} u ON s.uid = u.uid
-                        WHERE w.nid = '" . $node->wnid . "'
+                        WHERE w.nid = %d
                         AND (" . $query . ")
-                        ORDER BY d.sid, c.cid, c.name, d.data");
+                        ORDER BY d.sid, c.cid, c.name, d.data", $node->wnid);
   }
   return $result;  
 }
@@ -600,7 +570,7 @@
         if ($row->cid == $last_cid && $row->sid == $last_sid && !empty($last_value)) {
           $row->data .= ', ' . $last_value;
         }
-        $values[$row->sid][$row->cid] = array('data' => $row->data);
+        $values[$row->sid][$row->cid] = array('data' => filter_xss($row->data));
       } else {
           $values[$row->sid][$row->cid] = array('data' => '&nbsp;'); // prevents the table cell from being omitted
       }
@@ -698,7 +668,18 @@
     $values = _webform_report_add_data_links($fields, $values);
 
     // display number of rows after description
-    $output .= " (" . count($values) . " " . t('results') . ")</p>";
+    $output .= " (";
+    $cnt = count($values);
+    if ($cnt > 0) {
+        $res = 'result';
+        if ($cnt > 1)
+            $res .= 's';
+        $output .= $cnt . " " . t($res);
+    }
+    else {
+        $output = t("no results");
+    }
+    $output .= ")</p>";
 
     $output .= _webform_report_pager($fields, $values, $node);
   }
@@ -778,7 +759,7 @@
  * @return a uid for the specified user
  */
 function _webform_report_get_uid_for_user($name) {
-  $result = db_query("SELECT uid FROM users WHERE name = '" . $name . "'");
+  $result = db_query("SELECT uid FROM users WHERE name = '%s'", $name);
   $user = db_fetch_object($result);
   return $user->uid;
 } // function webform_report_get_uid_for_user
@@ -805,7 +786,7 @@
     }
   
     $output = theme_table($header, $rows, array('class' => 'webform'));
-    drupal_set_title($node->title);
+    drupal_set_title(check_plain($node->title));
   }
   return $output;
 }
