/** * Perform updates for the JS version and return progress. */ function update_do_update_page() { global $conf; // HTTP Post required if ($_SERVER['REQUEST_METHOD'] != 'POST') { drupal_set_message('HTTP Post is required.', 'error'); drupal_set_title('Error'); return ''; } // Error handling: if PHP dies, the output will fail to parse as JSON, and // the Javascript will tell the user to continue to the op=error page. list($percentage, $message) = update_do_updates(); print drupal_to_js(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message)); }
/** * Convert a PHP variable into its Javascript equivalent. Copied from * common.inc, which is only included in a full bootstrap of Drupal. */ function drupal_to_js($var) { switch (gettype($var)) { case 'boolean': return $var ? 'true' : 'false'; // Lowercase necessary! // Lowercase necessary! case 'integer': case 'double': return $var; case 'resource': case 'string': return '"' . str_replace(array("\r", "\n", "<", ">", "&"), array('\\r', '\\n', '\\x3c', '\\x3e', '\\x26'), addslashes($var)) . '"'; case 'array': // Arrays in JSON can't be associative. If the array is empty or if it // has sequential whole number keys starting with 0, it's not associative // so we can go ahead and convert it as an array. if (empty($var) || array_keys($var) === range(0, sizeof($var) - 1)) { $output = array(); foreach ($var as $v) { $output[] = drupal_to_js($v); } return '[ ' . implode(', ', $output) . ' ]'; } // Otherwise, fall through to convert the array as an object. // Otherwise, fall through to convert the array as an object. case 'object': $output = array(); foreach ($var as $k => $v) { $output[] = drupal_to_js(strval($k)) . ': ' . drupal_to_js($v); } return '{ ' . implode(', ', $output) . ' }'; default: return 'null'; } }
function budgets_supplier_get_name($id) { $matches = array(); $string = strtoupper(arg(3)); $qry = db_query('SELECT ' . ' CONCAT(id, "-", title) str ' . 'FROM {supplier} ' . 'WHERE ' . ' (CONCAT(id, "-", upper(title)) ' . ' LIKE "%' . $string . '%") ' . 'ORDER BY title'); $c = 0; while ($value = db_fetch_array($qry) and $c < 50) { $c++; $matches[$value['str']] = $value['str']; } print drupal_to_js($matches); exit; }
<?php print drupal_to_js($response);
/** * Select zone * * URL: http://guifi.net/guifi/js/select-zone/% */ function guifi_ahah_select_zone() { if (arg(4) == 'select') { $cid = 'form_' . $_POST['form_build_id']; $cache = cache_get($cid, 'cache_form'); $fname = arg(3); $zid = $_POST[$fname]; if ($cache) { $form = $cache->data; $form[$fname] = guifi_zone_select_field($zid, $fname); cache_set($cid, $form, 'cache_form', $cache->expire); // Build and render the new select element, then return it in JSON format. $form_state = array(); $form['#post'] = array(); $form = form_builder($form['form_id']['#value'], $form, $form_state); $output = drupal_render($form[$fname]); drupal_json(array('status' => TRUE, 'data' => $output)); } else { drupal_json(array('status' => FALSE, 'data' => '')); } exit; } else { $matches = array(); $string = strtoupper(arg(3)); $query = 'SELECT ' . ' CONCAT(z.id,"-",z.title) str, m.title master ' . 'FROM {guifi_zone} z, {guifi_zone} m ' . 'WHERE z.master=m.id ' . ' AND CONCAT(z.id,"-",upper(z.title)) LIKE "%' . $string . '%"'; $qry = db_query($query); $c = 0; $matches = array(); while ($value = db_fetch_array($qry) and $c < 50) { $c++; $matches[$value['str']] = $value['str'] . ' (' . $value['master'] . ')'; } print drupal_to_js($matches); exit; } }