Cmenu

 view release on metacpan or  search on metacpan

contrib/sqlmenu  view on Meta::CPAN

#!/usr/bin/perl
#
# Last updated Time-stamp: <01/04/07 18:31:56 devel>
#
# Foody Menu
# ----------


# Packages to use
# Curses for colour and character positioning
use Curses;
use Pg;
use Cmenu;


# #### VARIABLES ########################################################

# ---< setup database structures >--------------------------------------
# Setip database variables
$sqlhost=$ENV{PGHOST};
if(length($sqlhost)<1) { $sqlhost="localhost";}
$sqldbase="home";
$sqluser=$ENV{USER};

$conn=Pg::setdbLogin($sqlhost,'','','',$sqldbase,$sqluser,'');
if($conn->status != PG_CONNECTION_OK) {
    print "Failed to connect to Postgres Server\n";
    print "  Server $sqlhost\n";
    print "  User   $sqluser\n";
    print "  Dbase  $sqldbase\n";
    print "Postgres reports ".$conn->errorMessage."\n";
    die;
}

$last_option=0;

# ---< setup variables for addressbook >--------------------------------
# Types of Phone number
@pt = ();

$result=$conn->exec("SELECT * FROM addcat WHERE addcat=1 ORDER BY addctype");
$numrows=$result->ntuples();
for($i=0;$i<$numrows;$i++) {
  @row=$result->fetchrow;
  $pt[$i]=$row[2];
}

# Types of address
@at = ();
$result=$conn->exec("SELECT * FROM addcat WHERE addcat=2 ORDER BY addctype");
$numrows=$result->ntuples();
for($i=0;$i<$numrows;$i++) {
  @row=$result->fetchrow;
  $at[$i]=$row[2];
}

# pgSQL proper field names for UPDATE
@sql=();
$sql[0]="addressid";
$sql[1]="addforename";
$sql[2]="addsurname";
$sql[3]="addorg";
$sql[4]="addstreet";
$sql[5]="addtownland";
$sql[6]="addtown";

contrib/sqlmenu  view on Meta::CPAN

  my @fields = ();

  &menu_init("Edit Phone Number $number","Change the number for $name and indicate its type");

  &menu_item($number,"Number",6,0,"15 0 0"); 
  # Radio buttons for type of phone line
  for($i=0;$i<=3;$i++) {
    if($type == $i ) {
      &menu_item($pt[$i],$i+1,2,1);
    } else {
      &menu_item($pt[$i],$i+1,2,0);
    }
  }

  $set=&menu_display("Edit Phone Number");
  @fields=split(/$Cmenu::menu_sep/,$set);
  if($fields[0] eq "%EMPTY%") {
    $set=$number."~".$type;
  } else {
    # Breakout the number if it has been changed
    if(index($fields[1],$Cmenu::menu_sepn)>0) {
      (undef,$fields[1])=split(/$Cmenu::menu_sepn/,$fields[1]);
      $set=$fields[1]."~".($fields[2]-1);
    } else {
      $set=$number."~".($fields[1]-1);
    }
  }
  return($set);
}

# ---< edittype >---------------------------------------------------
# Edit family type
sub edittype {
  my ($type,$name) = @_;
  my ($set,$i); 
  my @fields = ();

  &menu_init("Edit Address Type","Select the Address Type for $name and press <SPACE>, it is currently $at[$type]");

  # Radio buttons for type of phone line
  for($i=0;$i<=9;$i++) {
    if($type == $i ) {
      &menu_item($at[$i],$i+1,2,1);
    } else {
      &menu_item($at[$i],$i+1,2,0);
    }
  }

  $set=&menu_display("Edit Address Type",$type);
  @fields=split(/$Cmenu::menu_sep/,$set);
  if($fields[0] eq "%EMPTY%") {
    $set=$type;
  } else {
    $set=($fields[1]-1);
  }
  return($set);
}
# ===< end of addressmenu >=============================================

# ===< foodmenu >====================================================
# Main Foody Menu
sub foodmenu {
  my ($cat,$head,$newresult,$i);
  my @hacks = ();

  $last_option=0;

  do {
    &menu_button_set(1,"Select");
    &menu_button_set(2,"");
    &menu_button_set(3,"Exit");

    &menu_init("Food Database","Select Application");
    
    &menu_item("Add to Shopping List","shopadd");
    &menu_item("Show Shopping List","shopshow");
    &menu_item("Print Shopping List","shopprint");
    &menu_item("Empty Shopping List","shopclear");
    &menu_item("Add an Item","foodadd");
    
    
    chop($sel=&menu_display("Choose your option",$last_option));
    
  SUB_MENU: for($sel) {
      /shopadd/ && do {
	do {
	  $cat=&fooditem();
	  if($cat>0) {
	    $head=&getcat($cat);
	    $result=$conn->exec("SELECT * FROM food WHERE foodcat = $cat ORDER BY food");
	    $numrows=$result->ntuples();
	    if($numrows<=0) {
	      &menu_show("No Records","There are no items in the category\n$head","HELP");
	    } else {
	      &menu_init("Shopping Trolley","Select the items to buy for $head");
	      
	      for($i=0;$i<$numrows;$i++) {
		@row=$result->fetchrow;
		&menu_item($row[2],$row[0],3,$row[7]);
	      }
	      $sel=menu_display("Select Items");
	      
	      @hacks=();
	      @hacks=split(/$Cmenu::menu_sep/,$sel);
	      $sel=$hacks[0];
	      if($sel ne "%EMPTY%") {
		# clear old values
		$result=$conn->exec("SELECT foodid FROM food WHERE foodcat = $cat");
		$numrows=$result->ntuples();
		for($i=0;$i<$numrows;$i++) {
		  @row=$result->fetchrow;
		  $newresult=$conn->exec("UPDATE food SET buy = 0 WHERE foodid = $row[0]");
		}
		# now set new values
		if($#hacks>0) {
		  for($i=1;$i<=$#hacks;$i++) {
		    $result=$conn->exec("UPDATE food SET buy = 1 WHERE foodid = $hacks[$i]");
		  }
		}
	      }
	    }



( run in 0.529 second using v1.01-cache-2.11-cpan-39bf76dae61 )