&$value) { if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { $merged[$key] = array_merge_recursive_distinct($merged[$key], $value); } else { $merged[$key] = $value; } } } return $merged; } } function navigate_get_form() { $form_id = $_POST['form']; switch ($form_id) { case 'navigate_admin_settings': include_once dirname(__FILE__) . '/admin.inc'; break; } navigate_json(array('content' => drupal_get_form($form_id))); } function navigate_add_js($data) { $libraries = navigate_libraries(); if (!is_array($data)) { $data = array($data); } $file = FALSE; $plugin = TRUE; foreach ($data as $library) { if (isset($libraries[$library])) { if (isset($libraries[$library]['library path']) && !empty($libraries[$library]['library path'])) { $file = $libraries[$library]['library path']; if (!file_exists($file)) { $file = FALSE; } } if ($libraries[$library]['plugin'] == 'navigate') { $plugin = FALSE; } } else { foreach (navigate_library_paths() as $path) { $file = $path . '/' . $library; if (file_exists($file)) { break; } else { $file = FALSE; } } } if ($file) { $scope = $plugin ? 'navigate-plugin' : 'navigate-core'; if (isset($libraries[$library]['scope'])) { $scope = $libraries[$library]['scope']; } $weight = 1; if (isset($libraries[$library]['weight'])) { $weight = $libraries[$library]['weight']; } drupal_add_js($file, $weight, $scope); } } } function navigate_add_css($file, $module = 'navigate', $compress = FALSE) { $file_info = pathinfo($file); $file_name = basename($file, '.' . $file_info['extension']); $base_path = $_SERVER['DOCUMENT_ROOT'] . base_path(); $module_path = drupal_get_path('module', $module); if (file_exists($base_path . $module_path . '/' . $file)) { $file_path = '/'; } else if (file_exists($base_path . $module_path . '/css/' . $file)) { $file_path = '/css/'; } else { return FALSE; } file_check_directory(file_create_path('css'), FILE_CREATE_DIRECTORY); $css_path = file_create_path('css/navigate'); file_check_directory($css_path, FILE_CREATE_DIRECTORY); $output_file = $css_path .'/'. $file_name . '_' . md5_file($base_path . $module_path . $file_path . $file) . '.css'; if (!file_exists($base_path . $output_file)) { require_once('lessphp/lessc.inc'); $less = new lessc(); $less->importDir = $base_path . $module_path . $file_path; $contents = drupal_load_stylesheet($base_path . $module_path . $file_path . $file, false); _drupal_build_css_path(NULL, base_path() . $module_path . $file_path); // Prefix all paths within this CSS file, ignoring external and absolute paths. $data = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_path', $contents); $data = $less->parse($data); if ($compress) { require_once('iceycss/iceycss.inc'); $iceycss = new iceycss(); $data = $iceycss->compress($data); } file_save_data($data, $output_file, FILE_EXISTS_REPLACE); } drupal_add_css($output_file, $module == 'navigate' ? 'module' : 'plugin'); } function navigate_file_info($file, $type = 'navigate') { $file_info = pathinfo($file); // Fix pathinfo['filename'] for PHP < 5.2 if (!isset($file_info['filename'])) $file_info['filename'] = basename($file, '.' . $file_info['extension']); $file_info['basepath'] = $_SERVER['DOCUMENT_ROOT'] . base_path(); if ($type == 'filedir') { $file_info['path'] = file_directory_path(); } else { $file_info['path'] = drupal_get_path('module', $type); } $subdirs = array('/', '/css/', '/css/theme/', '/includes/', '/js/'); $exists = FALSE; foreach ($subdirs as $subdir) { if (file_exists($file_info['basepath'] . $file_info['path'] . $subdir . $file_info['basename'])) { $file_info['path'] .= $subdir; $exists = TRUE; break; } } if (!$exists) return FALSE; $file_info['file'] = $file_info['basepath'] . $file_info['path'] . $file_info['basename']; return $file_info; } function navigate_jquery_ui_process($variables, $image_path) { foreach($variables as $attribute => $value) { switch(TRUE) { case preg_match('/^ui-icon/', $attribute): $output_file = $image_path . '/ui_icon_' . $value . '.png'; if (!file_exists($output_file)) { $contents = file_get_contents('http://jqueryui.com/themeroller/images/?new=' . $value . '&w=256&h=240&f=png&fltr[]=rcd|256&fltr[]=mask|icons/icons.png'); file_save_data($contents, $output_file, FILE_EXISTS_REPLACE); } break; } } } function navigate_theme_load($theme, $plugin = 'navigate', $templates = array(), $compress = TRUE) { require_once('lessphp/lessc.inc'); $less = new lessc(); $theme_name = $theme; $theme = navigate_file_info($theme . '.theme', $plugin); if ($theme) { file_check_directory(file_create_path('css'), FILE_CREATE_DIRECTORY); $css_path = file_create_path('css/navigate'); file_check_directory($css_path, FILE_CREATE_DIRECTORY); file_check_directory(file_create_path('navigate'), FILE_CREATE_DIRECTORY); $jqueryui_image_path = file_create_path('navigate/images'); file_check_directory($jqueryui_image_path, FILE_CREATE_DIRECTORY); $output_file = $css_path .'/'. $theme_name . '_' . md5_file($theme['file']) . '.css'; if (!file_exists($theme['basepath'] . $output_file)) { $theme['contents'] = file_get_contents($theme['file']); $theme['contents'] .= '@JQUERY_UI_IMAGES: "' . base_path() . $jqueryui_image_path . "\";\n"; $theme['contents'] .= '@THEME_PATH: "' . base_path() . $theme['path'] . "\";\n"; $theme['variables'] = $less->parse($theme['contents'], 'variables'); $jqueryui = array(); foreach($theme['variables'] as $attribute => $value) { if (preg_match("/^ui/", $attribute)) { $jqueryui[$attribute] = $value; } } navigate_jquery_ui_process($jqueryui, $jqueryui_image_path); if (empty($templates)) { $templates = array( 'jquery.ui.tpl.css', 'navigate.tpl.css', 'navigate.settings.tpl.css', ); } foreach($templates as $template) { $template = navigate_file_info($template, $plugin); $theme['contents'] .= drupal_load_stylesheet($template['file'], FALSE); } $less->importDir = $theme['basepath'] . $theme['path']; $theme['contents'] = $less->parse($theme['contents']); _drupal_build_css_path(NULL, base_path() . $theme['path']); // Prefix all paths within this CSS file, ignoring external and absolute paths. $data = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_path', $theme['contents']); if ($compress) { require_once('iceycss/iceycss.inc'); $iceycss = new iceycss(); $data = $iceycss->compress($data); } file_save_data($data, $output_file, FILE_EXISTS_REPLACE); } drupal_add_css($output_file, $module == 'navigate' ? 'navigate' : 'plugin'); } } function navigate_less_variables($file, $module = 'navigate') { $file = navigate_file_info($file); $contents = drupal_load_stylesheet($file['file'], false); _drupal_build_css_path(NULL, base_path() . $file['path']); // Prefix all paths within this CSS file, ignoring external and absolute paths. $data = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_path', $contents); require_once('lessphp/lessc.inc'); $less = new lessc(); $less->importDir = $file['basepath'] . $file['path']; $data = $less->parse($data, 'variables'); if (navigate_check_post() && $_POST['op'] == 'less_variables') { navigate_json(array('success' => TRUE, 'data' => $data)); } else { return $data; } } function navigate_check_post() { $post = FALSE; if (!empty($_POST)) { $post = TRUE; foreach ($_POST as $key => $value) { if (is_string($value)) { $_POST[$key] = urldecode($value); } $match = ''; if (is_array($value)) continue; // Convert booleans if (preg_match('/^b:(\d)$/', $value, $match) > 0) { $_POST[$key] = (bool) $match[1]; } } if (!isset($_POST['op'])) { $_POST['op'] = NULL; } if (!isset($_POST['plugin']) || $_POST['plugin'] == 'navigate') { $_POST['plugin'] = FALSE; } } return $post; } function navigate_json($array = NULL) { // We are returning JavaScript, so tell the browser. drupal_set_header('Content-Type: text/javascript; charset=utf-8'); if (isset($array)) { echo navigate_to_js($array); } exit(); } function navigate_settings_get($name = FALSE, $default = NULL, $refresh = FALSE) { global $_navigate; if (!isset($_navigate['settings']) || $refresh) $_navigate['settings'] = variable_get('navigate', array()); return $name ? isset($_navigate['settings'][$name]) ? $_navigate['settings'][$name] : $default : $_navigate['settings']; } function navigate_settings_set($name, $value, $save = TRUE) { global $_navigate; $_navigate['settings'][$name] = $value; if ($save) variable_set('navigate', $_navigate['settings']); return $_navigate['settings']; } // Compare weights function navigate_sort_weights($a, $b) { $a_weight = (is_array($a) && isset($a['weight'])) ? $a['weight'] : 0; $b_weight = (is_array($b) && isset($b['weight'])) ? $b['weight'] : 0; if ($a_weight == $b_weight) { return 0; } return ($a_weight < $b_weight) ? -1 : 1; } function navigate_send_message($bool = TRUE, $success = 'Success!', $error = 'Error!') { return array( 'success' => $bool, 'message' => $bool ? $success : $error, ); } function navigate_to_js($var) { // // Use PHP's json_encode function if it exists // if (function_exists('json_encode')) { // return str_replace(array('<', '>', '&'), array('\u003c', '\u003e', '\u0026'), json_encode($var)); // } // Or use our own (drupal_to_js() with a few modifications) switch (gettype($var)) { case 'boolean': return $var ? 'true' : 'false'; // Lowercase necessary! case 'integer': case 'double': return $var; case 'resource': case 'string': return '"'. str_replace(array("\n", "\r", "<", ">", "&", "'", '"'), array('\n', '\r', '\u003c', '\u003e', '\u0026', '\u0027', '\u0022'), $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[] = navigate_to_js($v); } return '[ '. implode(', ', $output) .' ]'; } // Otherwise, fall through to convert the array as an object. case 'object': $output = array(); foreach ($var as $k => $v) { $output[] = navigate_to_js(strval($k)) .': '. navigate_to_js($v); } return '{ '. implode(', ', $output) .' }'; default: return 'null'; } } /** * Save a user variable */ function navigate_variable_set($name = FALSE, $value = FALSE, $wid = FALSE, $uid = FALSE) { global $_navigate, $user; $ajax = FALSE; $return = FALSE; $save = FALSE; // Overwrite variables with POST data if (navigate_check_post()) { $ajax = TRUE; $json = navigate_send_message(FALSE, NULL, t('Error saving setting.')); $name = $_POST['name']; $value = $_POST['value']; $wid = $_POST['wid']; $uid = $_POST['uid']; } // Set the user to logged user if not specified if (!$uid) $uid = $user->uid; // Ensure user exists and the name is valid if (isset($_navigate['users'][$uid]) && $name) { // Save widget setting if widget exists if ($wid !== FALSE && isset($_navigate['users'][$uid]['widgets'][$wid])) { $setting = &$_navigate['users'][$uid]['widgets'][$wid]['plugin']['settings']; $save = TRUE; } // Set user setting else { $setting = &$_navigate['users'][$uid]['settings']; $save = TRUE; } } if ($save) { $setting[$name] = $value; $return = navigate_user_save($uid); $json = navigate_send_message($return, t('Setting saved successfully.'), t('Was unable to successfully save setting.') ); } if ($ajax) { navigate_json($json); } else { return $return; } } /** * Get a user variable */ function navigate_variable_get($name, $default, $uid = FALSE, $wid = FALSE, $refresh = FALSE) { global $_navigate, $user; // Set the user to logged user if not specified if (!$uid) $uid = $user->uid; // Refresh from database if needed if ($refresh) { navigate_user_load($uid, TRUE); } // Ensure user exists and the name is valid if (isset($_navigate['users'][$uid]) && $name) { // Get widget setting if widget exists if ($wid !== FALSE && isset($_navigate['users'][$uid]['widgets'][$wid])) { $setting = $_navigate['users'][$uid]['widgets'][$wid]['plugin']['settings']; } // Get user setting else { $setting = $_navigate['users'][$uid]['settings']; } // Return setting if (isset($setting[$name])) { return $setting[$name]; } // Return default else { return $default; } } } /** * Delete a user variable */ function navigate_variable_delete($name, $wid=0) { global $user; db_query("DELETE FROM {navigate_user_settings} WHERE name = '%s' AND wid = '%d' AND uid = '%d'", $name, $wid, $user->uid); }