Drupal6常用自定义函数
<?php /* * 提取并显示指定node的内容 */ function custom_node_view($nid) { $node = node_load($nid); return node_view($node); } /* * 载入指定的block */ function block_print($module, $delta) { $block = new stdclass; $array = module_invoke($module, 'block', 'view', $delta); if (isset($array) && is_array($array)) { foreach ($array as $k => $v) { $block->$k = $v; } } $block->module = $module; $block->delta = $delta; return theme('block', $block); } /* * 得到记录总数 */ function views_total() { $view = views_get_view( 'VIEWS_NAME' ); $view->set_arguments( array( 1, 2, 3 ) ); // 如果需要设置参数 $view->get_total_rows = TRUE; $view->execute(); // 执行 $view->preview(); // 执行并输出views return $view->total_rows; } /* * ahah时,可以在表单回调时得到表单修改状态 * 由于回调时表单并未提交,此时不能从submit方法中得到表单状态 * @return form handle */ function _callback_helper() { $form_state = array('storage' => NULL, 'submitted' => FALSE); $form_build_id = $_POST['form_build_id']; $form = form_get_cache($form_build_id, $form_state); $args = $form['#parameters']; $form_id = array_shift($args); $form_state['post'] = $form['#post'] = $_POST; // Enable the submit/validate handlers to determine whether AHAH-submittted. $form_state['ahah_submission'] = TRUE; $form['#programmed'] = $form['#redirect'] = FALSE; drupal_process_form($form_id, $form, $form_state); $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id); return $form; } function readcsv($filepath){ //$filepath is the path of a csv file $result=array(); if (($handle = fopen($filepath, "r")) !== FALSE) { while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {//readall $result[]=$data; } fclose($handle); } return $result; } /* * 检查并创建目录 * $filepath 目录地址 */ function custom_file_directory($filepath) { if (!file_check_directory($filepath, FILE_CREATE_DIRECTORY) && !mkdir($filepath, 0775, TRUE)) { watchdog('custom', 'Failed to create directory: %dir', array('%dir' => $filepath), WATCHDOG_ERROR); return false; } else { return true; } } /* * 查询指定content type中的vocabulary name */ function vocabulary_by_node_type($content_type) { $sql = sprintf('SELECT v.* FROM {vocabulary} v JOIN {vocabulary_node_types} node_type ON node_type.vid = v.vid WHERE node_type.type = %s ORDER BY weight', db_placeholders($content_type)); $vocabularys = array(); $result = db_query($sql); while ($vocabulary = db_fetch_object($result)) { $vocabularys[] = $vocabulary; } return $vocabularys; } /* * vocabulary下的term name */ function term_by_vocabulary_id($vid) { $result = db_query(sprintf('SELECT term_data.* FROM {term_data} term_data WHERE term_data.vid = %d ORDER BY weight', $vid)); $terms = array(); while ($term = db_fetch_object($result)) { $terms[] = $term; } return $terms; } /* * 获得CCK字段的allowed values数组,例如dropdown */ function cck_allowed_value($fieldname) { $global_settings = db_result(db_query("SELECT global_settings FROM {content_node_field} WHERE field_name = '%s'", db_escape_string($fieldname))); $global_settings = unserialize($global_settings); $allowed_values = $global_settings['allowed_values']; $allowed_values = str_replace("\r\n", "|", $allowed_values); $allowed_values = str_replace("\n", "|", $allowed_values); return explode('|', $allowed_values); } /* * 把menu tree组装成数组 */ function menu_list($name = 'navigation', $depth = 1, $mlid = 0) { ($depth < 10) or die("depth too big."); $results = array(); $cid = $name.'_menu_list_'.$depth; $cache = $depth > 1 ? false : cache_get($cid); if(!$cache) { $db_result = db_query("SELECT mlid, link_title FROM {menu_links} WHERE hidden<=0 AND depth = %d AND plid = %d AND menu_name = '%s' AND link_title <> '' ORDER BY weight", $depth, $mlid, $name); while($row = db_fetch_object($db_result)) { $childs = menu_list($name, $depth+1, $row->mlid); $results[]= array('data' => $row, 'children' => $childs); } unset($db_result); if($depth == 1) cache_set($cid, $results); } else { $results = $cache->data; } return $results; } /* * 文件上传 * $module module的名称 * $source 文件字段名 * $validators 文件验证信息 */ function file_upload($module, $source, $validators = array( 'file_validate_extensions' => array('jpg', 'png'), //'file_validate_image_resolution' => array($limits['resolution']), 'file_validate_size' => array(204800) )) { $validators = array(); $dest = file_directory_path().'/'.$module.'/'; file_create_directory($module, $dest); $file = file_save_upload($source, $validators, $dest); if ($file != 0) { return array( 'status' => true, 'filepath' => $file->filepath ); } else { return array('status' => true); } } /* * 新建目录 * $module module名称 * $dir 目录路径 */ function file_create_directory($module, $dir) { if (!file_check_directory($dir, FILE_CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) { watchdog($module, 'Failed to create directory: %dir', array('%dir' => $dir), WATCHDOG_ERROR); return false; } } function set_meta($keywords, $description) { drupal_set_html_head("<meta name='keywords' content='$keywords' /><meta name='description' content='$description' />"); } /* * 静态优化的drupal_lookup_path */ function drupal_lookup_path($action, $path ='') { static $map = array(); if ($action == 'wipe') { $map = array(); return FALSE; } if ($path != '') { // Initial load from DB if (count($map) == 0) { $result = db_query('SELECT src, dst FROM {url_alias}'); while ($url = db_fetch_object($result)) { $map[$url->src] = $url->dst; $map[$url->dst] = $url->src; } } // $action == ‘alias’ or $action == ‘source’ if (isset($map[$path])) return $map[$path]; } return FALSE; } /* * 从nid得到图片信息 * @return array(width, height, extension, file_size, mime_type) */ function image_get_info_by_node($nid, $fieldname = 'field_image') { $node = node_load($nid); $results = array(); foreach($node->$fieldname as $image) { $results []= image_get_info($image['filepath']); } return $results; } /* * 用于views template中输出字段 * views_view_fileds_print_field($fields, 'field_website_value') */ function views_view_fileds_print_field(&$fields, $fieldname) { if(!isset($fields[$fieldname])){ return false; } $field = $fields[$fieldname]; if (!empty($field->separator)): ?> <?php print $field->separator; ?> <?php endif; ?> <<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>"> <?php if ($field->label): ?> <label class="views-label-<?php print $field->class; ?>"> <?php print $field->label; ?>: </label> <?php endif; ?> <?php // $field->element_type is either SPAN or DIV depending upon whether or not // the field is a 'block' element type or 'inline' element type. ?> <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>> </<?php print $field->inline_html;?>> <?php unset($fields[$fieldname]); }