t('None')); $form['menu']['iconify_menu_api'] = array( '#type' => 'radios', '#title' => t('Menu Icon'), '#description' => t('Select an icon for this menu item.'), '#options' => $none + $icons, '#default_value' => $iconify_menu_api_selected, '#attributes' => array('class' => 'iconify_menu_api-radios'), ); $form['#submit'][] = 'iconify_menu_api_menu_item_update'; } } /** * Submit handler for the menu_edit_item form. */ function iconify_menu_api_menu_item_update($form, &$form_state) { // Ensure we have a menu item to work with. if (isset($form_state['values']['menu'])) { $icon = $form_state['values']['menu']['iconify_menu_api']; $mlid = $form_state['values']['menu']['mlid']; _iconify_menu_api_update_item($icon, $mlid); iconify_menu_api_create_css(); } } /** * Create css file */ function iconify_menu_api_create_css() { $contents = iconify_menu_api_css(); $csspath = file_create_path('iconify_menu'); file_check_directory($csspath, FILE_CREATE_DIRECTORY); $file = $csspath . '/iconify_menu-style.css'; // 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); $output = file_save_data($data, $file, FILE_EXISTS_REPLACE); // Set standard file permissions for webserver-generated files. @chmod($file, 0664); return $data; } /** * Update the icon for a menu item that has been inserted * or updated. * */ function _iconify_menu_api_update_item($icon, $mlid) { $existing_icon = _iconify_menu_api_get_item($mlid); if(empty($existing_icon) && !empty($icon)) { //this is the first entry for this menu item db_query("INSERT INTO {iconify_menu_api} (icon, mlid) VALUES ('%s', '%d')", $icon, $mlid); } elseif($icon=='none') { //none was selected so remove menu item db_query("DELETE from {iconify_menu_api} WHERE mlid = %d",$mlid); } else { //menu item already exists, so update existing entry db_query("UPDATE {iconify_menu_api} SET icon = '%s' where mlid = '%d'", $icon, $mlid); } } /** * Optionally insert/update and return a localized menu item title. */ function _iconify_menu_api_get_item($mlid, $update = FALSE) { $icon = db_result(db_query("SELECT icon FROM {iconify_menu_api} WHERE mlid = '%d'", $mlid)); if (!empty($icon)) { return $icon; } else { return ''; } } /** * Add menu_item_link to theme registry */ function iconify_menu_api_theme_registry_alter(&$theme_registry) { if (!empty($theme_registry['menu_item_link'])) { $theme_registry['menu_item_link']['function'] = 'iconify_menu_api_menu_item_link'; $theme_registry['menu_tree']['function'] = 'iconify_menu_api_menu_tree'; } } /** * Get menu name for menu items with an icon */ function iconify_menu_api_get_menu_names() { $result = db_query("SELECT mlid FROM {iconify_menu_api}"); while($mlid = db_fetch_array($result)) { $mlids[]=$mlid['mlid']; } if(isset($mlids)) { foreach($mlids as $key => $item) { $menus[]= db_result(db_query('SELECT menu_name FROM {menu_links} WHERE mlid = "%d"',$item)); } } else { $menus = ''; } return $menus; } /** * Add css class to menu items that have an icon added. */ function iconify_menu_api_menu_item_link($link) { $menus = iconify_menu_api_get_menu_names(); if (is_array($menus)) { $menu_name = $link['menu_name']; if (in_array($menu_name,$menus)) { $icon = _iconify_menu_api_get_item($link['mlid']); } if(!empty($icon)) { $options = array('attributes' => array('class' => 'iconify_menu-item ' . $icon)); } else { if (empty($link['localized_options'])) { $options = array(); } else { $options = $link['localized_options']; } } return l($link['title'], $link['href'],$options ); } else { if (empty($link['localized_options'])) { $link['localized_options'] = array(); } return l($link['title'], $link['href'], $link['localized_options']); } } /** * Generate the HTML output for a menu tree * * This is a hacky way to add a class to the menu if it contains a menu link edited with icon menu. */ function iconify_menu_api_menu_tree($tree) { $icons = iconify_menu_api_icon_list(); $contents = explode('"',$tree); if ($icons) { foreach ($icons as $set => $icons) { foreach ($icons as $name) { if (in_array('iconify_menu_api-item ' . $name, $contents)) { $class = ' iconify_menu_api'; } } } return '
'; } else { return array(); } } /** * Clean up array for menu list */ function iconify_menu_api_icon_list_menuprepare($sets) { //remove uppercase and/or whitespace foreach ($sets as $icon => $name) { $icons[strtolower(str_replace(' ','-', $icon))] = $name; } //merge into single array foreach ($icons as $icon => $name) { foreach ($name as $key => $value) { $prepare[$icon . '-' . $key] = $value; } } //remove .jpg .gif .png extensions $extensions = array(".jpg", ".gif", ".png"); foreach ($prepare as $key => $value) { $prepared[str_ireplace($extensions,'',$key)] = $value; } return $prepared; } /** * Clean up array for css file */ function iconify_menu_api_icon_list_cssprepare($icons, $sets) { $prepared = iconify_menu_api_icon_list_menuprepare($icons); $extensions = array(".jpg", ".gif", ".png"); $i=0; foreach($icons as $setname => $files) { foreach($files as $file => $filename) { $css[$i]['file'] = $file; foreach($sets as $set => $attribute) { if ($setname == $attribute['name']) { $css[$i]['path'] = $attribute['path']; $css[$i]['height'] = $attribute['height']; $css[$i]['width'] = $attribute['width']; $css[$i]['full'] = strtolower(str_replace(' ','-', $attribute['name'])) . '-' . str_ireplace($extensions,'',$file); } } $i++; } } return $css; } /** * Make css file contents */ function iconify_menu_api_css() { $icons = iconify_menu_api_icon_list(); $sets = module_invoke_all('iconify_menu_api_info'); $icons = iconify_menu_api_icon_list_cssprepare($icons, $sets); $css = "div.iconify_menu_api-radios div.form-item { float:left; height:55px; margin-right:10px; width:140px; }\n"; $css.= "div.iconify_menu_api-radios label.option { display:block; text-align:right; width:70px; }\n"; foreach($icons as $icon) { $li = "li a.ICONFULL { background: url(/ICONPATH/ICONFILE) no-repeat; height: ICONHEIGHTpx; padding: ICONQTRWIDTHpx 0 0 ICONWIDTHpx;}\n"; $width = ($icon['width'] / 4) + $icon['width']; $qtrwidth = $icon['width'] / 8; $li = str_replace('ICONWIDTH', $width, $li); $li = str_replace('ICONQTRWIDTH', $qtrwidth, $li); $li = str_replace('ICONFULL', $icon['full'], $li); $li = str_replace('ICONPATH', $icon['path'], $li); $li = str_replace('ICONFILE', $icon['file'], $li); $li = str_replace('ICONHEIGHT', $icon['height'], $li); $div = "#edit-menu-iconify_menu-api-ICONFULL-wrapper { background: url(/ICONPATH/ICONFILE) 80px 5px no-repeat; }\n"; //$div.= "#edit-menu-iconify_menu-api-ICONFULL-wrapper input { margin: 5px ICONWIDTHpx; }\n"; $icon['full'] = str_replace('_', '-', $icon['full']); $div = str_replace('ICONWIDTH', $width, $div); $div = str_replace('ICONFULL', $icon['full'], $div); $div = str_replace('ICONPATH', $icon['path'], $div); $div = str_replace('ICONFILE', $icon['file'], $div); $css.= $li . $div; } return $css; } /** * List available icons */ function iconify_menu_api_icon_list() { $sets = module_invoke_all('iconify_menu_api_info'); foreach ($sets as $key => $set) { $icons[$set['name']] = module_invoke_all('iconify_menu_api_icons', $set['name']); } return $icons; } /** * Implementation of hook_init() * */ function iconify_menu_api_init() { $path = file_create_path('iconify_menu'); $file = $path . '/iconify_menu-style.css'; if (!file_exists($file)) { iconify_menu_api_create_css(); } drupal_add_css($file); }