nid'")); $ingredients = db_query("SELECT ingredient, weight FROM {recipe_ingredients} WHERE nid='$node->nid' ORDER BY weight"); while ($ingredient = db_fetch_object($ingredients)) { $recipe->ingredients[$ingredient->weight] = $ingredient->ingredient; } return $recipe; } function recipe_node_name() { return t('recipe'); } function recipe_node_info() { return array('recipe' => array('name' => t('recipe'), 'base' => 'recipe')); } function recipe_help($section = '') { $output =""; switch ($section) { case 'admin/help#recipe': break; case 'admin/modules/recipe': case 'admin/modules#description': $output = t("Collect and display recipes"); break; case 'node/add#recipe': $output = t("Share your favorite recipes with your fellow cooks."); break; case 'node/add/recipe': $output = variable_get("recipe_help", "");; break; } return $output; } function recipe_nodeapi(&$node, $op, $arg) { if ($node->type == "recipe") { switch ($op) { case 'validate': // $node->teaser = $node->instructions; $body = array($node->instructions, $node->source, $node->yield, $node->preptime, $node->notes); $ingred_array = array (); for ($i = 0; $i < $node->numingredients; $i++) { $ingred = 'ingredients'.$i; $ingred_array [$i] = $node->$ingred; } $body = array_merge($body, (array) $ingred_array); $node->body = '

'. implode('

', $body). '

'; case "fields": break; case "insert": db_query("INSERT INTO {recipe} (nid, source, yield, preptime, notes, instructions) VALUES ('$node->nid', '%s', '%s', '%s', '%s', '%s')", $node->source, $node->yield, $node->preptime, $node->notes, $node->instructions); for ($i = 0; $i < $node->numingredients; $i++) { $ingred = 'ingredients'.$i; $ingredient = $node->$ingred; if ($ingredient){ db_query("INSERT INTO {recipe_ingredients} (nid, ingredient, weight) VALUES (%d, '%s', %d)", $node->nid, $ingredient, $i); } } break; case "update": db_query("UPDATE {recipe} SET source = '%s', yield = '%s', preptime = '%s', notes = '%s', instructions = '%s' WHERE nid = %d", $node->source, $node->yield, $node->preptime, $node->notes, $node->instructions, $node->nid); db_query("DELETE FROM {recipe_ingredients} WHERE nid = '$node->nid'"); for ($i = 0; $i < $node->numingredients; $i++) { $ingred = 'ingredients'.$i; $ingredient = $node->$ingred; if ($ingredient) { db_query("INSERT INTO {recipe_ingredients} (nid, ingredient, weight) VALUES (%d, '%s', %d)", $node->nid, $ingredient, $i); } } break; case "delete": db_query("DELETE FROM {recipe} WHERE nid = %d", $node->nid); db_query("DELETE FROM {recipe_ingredients} WHERE nid = %d", $node->nid); break; } } } function recipe_form(&$node) { $op = arg(1); $edit = $_POST["edit"]; $form['title'] = array( '#type' => 'textfield', '#title' => t('Title'), '#required' => true, '#default_value' => $node->title, ); $form['yield'] = array( '#type' => 'textfield', '#title' => t("Servings"), '#default_value' => $node->yield, '#size' => 60, '#maxlength' => 127, '#description' => t("Describe how many servings this recipe will yield. Feel free to be creative."), ); $form['preptime'] = array( '#type' => 'select', '#title' => t("Preparation time"), '#default_value' => $node->preptime, '#options' => array (15 => "15 minutes", 30 => "30 minutes", 45 => "45 minutes", 60 => "1 hour", 90 => "1 1/2 hours", 120 => "2 hours", 150 => "2 1/2 hours", 180 => "3 hours", 210 => "3 1/2 hours", 240 => "4 hours", 300 => "5 hours", 360 => "6 hours"), '#description' => t("How long does this recipe take to prepare (i.e. elapsed time)"), ); $form['exingr'] = array( '#type' => 'item', '#title' => t("Example Ingredients"), '#description' => t("1 bunch kale or Swiss chard, stems and ribs removed, leaves sliced
3 white potatoes - peeled and diced"), ); $form['recipeingr'] = array( '#type' => 'item', '#title' => t("Recipe Ingredients"), '#default_value' => "", ); $form['ingred']['numingredients'] = array( '#type' => 'hidden', '#default_value' => max(1, count($node->ingredients) ? count($node->ingredients) : 6) ); $form['ingred']['recipe_more_ingredients'] = array( '#type' => 'checkbox', '#title' => t("add more ingredients"), '#return_value' => 1, '#default_value' => 0, '#description' => t("If you need to add more ingredients, check this box and click "). "". t("Preview"). "", '#weight' => 1, ); $form['ingred'] = form_builder('recipe_node_form', $form['ingred']); if ($form['ingred']['recipe_more_ingredients']['#value']) { $form['ingred']['recipe_more_ingredients']['#value'] = 0; $form['ingred']['numingredients']['#value'] += 6; } // if the value was changed in a previous iteration, retain it. $node->numingredients = $form['ingred']['numingredients']['#value']; for ($i = 0; $i < $node->numingredients; $i++) { $form['ingred']['ingredients'.$i]= array( '#type' => 'textfield', '#default_value' => $node->ingredients[$i], '#size' => 60, '#maxlength' => 127, ); } $form['instructions'] = array( '#type' => 'textarea', '#title' => t("Cooking Instructions"), '#default_value' => $node->instructions, '#cols' => 60, '#rows' => 18, '#description' => t("Include a brief description of your recipe at the top of your instructions."), ); if (function_exists("taxonomy_node_form")) { $form['taxonomy'] = implode("", taxonomy_node_form("recipe", $node)); } $form['source'] = array( '#type' => 'textfield', '#title' => t("Source"), '#default_value' => $node->source, '#size' => 60, '#maxlength' => 127, '#description' => t("Optional. Does anyone else deserve credit for this recipe?"), ); $form['notes'] = array( '#type' => 'textarea', '#title' => t("Additional Notes"), '#default_value' => $node->notes, '#cols' => 60, '#rows' => 5, '#description' => t("Optional. Describe a great dining experience relating to this recipe, or note which wine or other dishes complement this recipe"), ); $form['filter'] = filter_form('format', $node->format); return $form; } function recipe_menu($may_cache) { if ($may_cache) { $items[] = array('path' => 'node/add/recipe', 'title' => t('recipe'), 'access' => user_access('create recipes')); $items[] = array('path' => 'recipe', 'title' => t('recipes'), 'callback' => 'recipe_page', 'access' => user_access('access content'), 'type' => MENU_SUGGESTED_ITEM); } else { drupal_set_html_head(recipe_html_head()); } return $items ? $items : array(); } function recipe_access($op, $node) { global $user; if ($op == 'create') { return user_access('create recipes'); } if ($op == 'update' || $op == 'delete') { if (user_access('edit own recipes') && ($user->uid == $node->uid)) { return TRUE; } } } function recipe_block($op = "list", $delta = 0) { if ($op == "list") { $blocks[0]["info"] = t("Newest 10 recipes"); return $blocks; } elseif ($op == 'view') { $block["subject"] = t("Newest Recipes"); $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.uid, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.type='recipe' ORDER BY n.nid DESC"), 0, 10); $block["content"] = node_title_list($result); return $block; } } function recipe_settings() { $form['recipe_help'] = array( '#type' => 'textarea', '#title' => t("Explanation or submission guidelines"), '#default_value' => variable_get("recipe_help", ""), '#cols' => 55, '#rows' => 4, '#description' => t("This text will be displayed at the top of the recipe submission form. Useful for helping or instructing your users."), ); return $form; } function recipe_page() { $op = arg(1); if ($op == "feed") { recipe_feed(); // not yet implemented return; } elseif ($op == "css") { print recipe_css(); } else { print theme("page", recipe_overview()); } } function recipe_overview() { // no longer possible with 4.6 search. need to find a new way /*if (module_exist('search')) { $output .= search_type("recipe", url("search"), NULL, 1); }*/ $output .= recipe_list(); if (module_exist("taxonomy_dhtml") && $content = recipe_directory_dhtml()) { $output .= $content; } return $output; } function recipe_directory_dhtml() { $boxes = taxonomy_dhtml_overview("recipe"); foreach ($boxes as $box) { $output .= "". $box["subject"]. "
". $box["content"]; } return $output; } function recipe_list() { $result = pager_query(db_rewrite_sql("SELECT n.nid, n.title, n.uid, u.name, r.notes FROM {node} n INNER JOIN {users} u ON n.uid=u.uid INNER JOIN {recipe} r ON n.nid = r.nid WHERE n.type = 'recipe' ORDER BY n.created DESC"), 20); $header = array(t("Title"), t("Author"), t('Notes')); while ($node = db_fetch_object($result)) { $rows[] = array( array("data" => l($node->title, "node/$node->nid")), array("data" => format_name($node)), array("data" => $node->notes), // array("data" => ($num = comment_num_all($node->nid)) ? $num : " ") ); } if (!$rows) { $rows[] = array(array("data" => t("No recipes available."), "colspan" => "3")); } $pager = theme("pager", NULL, 20, 0); if (!empty($pager)) { $rows[] = array(array("data" => $pager, "colspan" => "3")); } return theme('table', $header, $rows); } function recipe_view(&$node, $teaser = 0, $page = 0) { $node = recipe_content($node, $teaser); drupal_set_breadcrumb(array(l(t('Home'), ''), l('Recipes', 'recipe'))); } function recipe_content($node, $teaser = 0) { $preptime = t("%n hours", array ("%n" => ($node->preptime / 60))); $output = "
\n"; $output .= "
Servings:
\n"; $output .= "
$node->yield
\n"."
\n"; $output .= "
Preparation Time:
\n"; $output .= "
$preptime
\n"."\n"; $ingr_array = array(); if ($node->preview){ $i=0; $ingred = 'ingredients'.$i; while($node->$ingred ) { $ingr_array[] = $node->$ingred; $i=$i+1; $ingred = 'ingredients'.$i; } } else{ $i=0; while($node->ingredients[$i] ) { $ingr_array[] = $node->ingredients[$i]; $i=$i+1; } } $output .= '
' . theme('item_list', $ingr_array, t('Ingredients')). "
"; $output .="\n"; $output .= "
Cooking Instructions:
\n"; $output .= "
$node->instructions
\n"; if ($node->source) { $output .= "
Source:
\n"; $output .= "
$node->source
\n"; } if ($node->notes) { $output .= "
Notes
\n"; $output .= "
$node->notes
\n"; } $output .= ''; $node->body = $output; $node->teaser = $node->instructions; return node_prepare($node, $teaser); } function recipe_html_head() { $style = ""; return $style; } ?>