All pastes #508358 Raw Edit

Someone

public php v1 · immutable
·published 2007-05-24 23:40 UTC
rendered paste body
<?php/*** A small example module of using the form api* to display and process a form* that is not used to extend the basic node content type** In this sample the form collects two numbers* and multiplies them together*//*** Implementation of hook_help().** Throughout Drupal, hook_help() is used to display help text at the top of* pages. Some other parts of Drupal pages get explanatory text from these hooks* as well. We use it here to provide a description of the module on the* module administration page.*/function sold_help($section) {  switch ($section) {    case 'admin/modules#description':      // This description is shown in the listing at admin/modules.      return t('Used to record products sold.');  }}/*** Implementation of hook_perm().**/function sold_perm() {  return array('record products sold');}/*** Implementation of hook_menu().**/function sold_menu($may_cache) {  $items = array();    $access = user_access('record products sold');   if ($may_cache) {      // This determines the path used to show the form and also makes a menu entry      // This first path is for the entry form    $items[] = array('path' => 'sold/record', 'title' => t('Record products sold.'),      'callback' => 'sold_record', 'access' => $access);    // This path is for the routine that actually does the multplication    // It is only setup as a callback (no menu entry)    $items[] = array('path' => 'sold/report', 'title' => t('Products information.'),      'type' => MENU_CALLBACK, 'callback' => 'sold_report', 'access' => $access);   }	$items[] = array('path' => 'sales/history',                                  'title' => t('Sales History'),                                  'callback' => 'sales_history',                                  'access' => user_access('access content'),                                  'type' => MENU_CALLBACK);	$items[] = array('path' => 'test/form',                                  'title' => t('Test Form'),                                  'callback' => 'drupal_get_form',                                  'callback arguments' => 'test_form',                                  'access' => user_access('access content'),                                  'type' => MENU_CALLBACK);     return $items;}function test_form() {    $form = array();     // Build up the form     $form['date1'] = array(      '#type' => 'textfield',      '#width' => 10,    );    $form['date2'] = array(      '#type' => 'textfield',      '#width' => 5,    );     // Make sure we have a submit button    $form['submit'] = array('#type' => 'submit', '#value' => t('Record'));        return ($form);}function test_form_submit($form_id, $form_values) {    global $user;    // Break the textrea into lines    $lines = explode("\n", trim($form_values['products']));	$output.=$lines[0];	$output.="testing";$_SESSION['test']=$output;    return 'sales/history';}function sold_record() {    $form = array();     // Build up the form     $form['products'] = array(      '#type' => 'textarea',      '#title' => t('Products sold'),      '#cols' => 10,      '#rows' => 10,      '#description' => t('Paste in the product information.')    );     // Make sure we have a submit button    $form['submit'] = array('#type' => 'submit', '#value' => t('Record'));    return drupal_get_form('sold_record', $form);}function sold_record_validate($form_id, $form_values) {  if (strlen($form_values['products']) > 30000) {    form_set_error('products', t('Your submission contains more than 30,000 characters. Please break it down into smaller pieces, then resubmit'));  }     }function deeptrim($string) {    $string = trim($string);    $tmp = explode(" ",$string);    foreach($tmp as $key => $val) {        if (!$val == "") {            $return[] = $val;        }    }    return implode(" ",$return);}function sold_record_submit($form_id, $form_values) {    global $user;       // Parse and save the values.    $sql = "INSERT INTO {sold} (uid, date, product, name, price, extra) VALUES(%d, '%s', '%s', '%s', %d, '%s')";    // Break the textrea into lines    $lines = explode("\n", trim($form_values['products']));    // For each line entered    foreach ( $lines as $line_item ) {        // Break the line on the space character		$line_item = str_replace("\t", " ", $line_item);	$line_item = deeptrim($line_item);	$details = explode(" ", $line_item);        // The first item will always be the date        // The last threee items will be name amount extra (in that order)        // The parts in between will be the product name        $cnt = count($details);        $date = $details[0];        $date_parts = explode('/', $date);        $when = mktime(12, 0, 0, $date_parts[1],  $date_parts[0], $date_parts[2]);	$dates = explode("/",$date); //array of values	$when = $dates[2]."-".$dates[1]."-".$dates[0]; //d-m-y to y-m-d        $extra = trim($details[$cnt-1]);  // remove any extra white space from the last part        $amount = str_replace(',', '', $details[$cnt-2]);  // Remove any comma's from the amount        $name = $details[$cnt-3];               // The product starts at index 1 and includes upto everything but the last three parts        //    So we slice out that part of the array        //    And make a string of words separated by a space        $product = implode(' ', array_slice($details, 1, -3));        // Record the data        db_query($sql, $user->uid, $when, $product, $name, $amount, $extra);    }  return 'sold/report';} function sold_report() {    global $user;    // Report on the data just submitted by the user (recorded will be 1)    $results = db_query("SELECT COUNT(sid) as count,price, product FROM {sold} WHERE recorded = 1 AND uid = %d GROUP BY product,price", $user->uid);      $output.="<br><br><table width='400' cellspacing='0' style='margin-left:auto; margin-right:auto;'><td width='200' style='background:#f0f0f0'>Item</td><td width='100' style='background:#f0f0f0'><div style='text-align:right;'>Number Sold</div></td><td width='100' style='background:#f0f0f0'><div style='text-align:right;'>Price</div></td>";    while ( $data = db_fetch_object($results))	{$output .= "<tr><td width='200'>$data->product</td><td width='100' style='text-align:right;'><div style='text-align:right;'>$data->count</div></td><td width='100'> <div style='text-align:right;'>$data->price</div></td></tr>";}    $output.="</table>";     $results = db_query("SELECT SUM(price) as total, name, extra FROM {sold} WHERE recorded = 1 AND uid = %d GROUP BY name, extra", $user->uid);    $output.="<br><br><table width='280' cellspacing='0' style='margin-left:auto; margin-right:auto;'><td style='background:#f0f0f0'>User</td><td style='background:#f0f0f0'><div style='text-align:right;'>NP Spent</div></td>";    while ( $data = db_fetch_object($results) )	{$output .= "<tr><td width='50%'>" . $data->name . "</td><td style='text-align:right;'>" . number_format($data->total) . "</td></tr>";}    $output.="</table>";    $results = db_query("SELECT SUM(price) as total, date FROM {sold} WHERE recorded = 1 AND uid = %d GROUP BY date", $user->uid);    $output.="<br><br><table width='280' cellspacing='0' style='margin-left:auto; margin-right:auto;'><td style='background:#f0f0f0'>Date</td><td style='background:#f0f0f0'><div style='text-align:right;'>NP Spent</div></td>";    while ( $data = db_fetch_object($results) ) 	{$output .= "<tr><td width='50%'>" . $data->date . "</td><td style=text-align:right;>" . number_format($data->total) . "</td></tr>";}    $output.="</table>";    $output.="<a href='?q=sales/history'>Sales History</a>";    // Clear the recorded flag so data is not included on next recording of data    db_query("UPDATE {sold} SET recorded = 0 WHERE uid = %d", $user->uid);    return $output;}function my_search_form(){//filtering section html code$output.='<br><div style="text-align:center">*Sales do not automatically show on this page, you must click Show All*</div><br><br><form action="" name="filter"><input name="q" value="sales/history" type="hidden"> <input name="sid_to_delete_desc_asc" value="true" type="hidden">  <table style="width: 100%; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="0" cellspacing="0">    <tbody>      <tr valign="top">        <td colspan="4" rowspan="1" align="left" width="25">        <table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="0">          <tbody>            <tr>              <td>              <table style="text-align: left;" border="0" cellpadding="0" cellspacing="0">                <tbody>                  <tr valign="top">                    <td style="white-space: nowrap; text-align: left; width: 25px;">		    </td>                    <td style="white-space: nowrap; text-align: right; width: 50px;">			<small>Product:</small>		    </td>                    <td style="width: 10px;">&nbsp;</td>                    <td align="left">                    <table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">                      <tbody>                        <tr>                          <td align="left" valign="top">                          <input name="item_textbox" value="'. $_SESSION['item_textbox'] . '"></td>                          <input type="hidden" name="searchpost" value="true">                          <td align="right" width="20">&nbsp;</td>                          <td width="6"></td>                        </tr>                      </tbody>                    </table>                    <br>                    </td>                  </tr>                </tbody>              </table>              </td>              <td>              <table style="width: 100%; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="0" cellspacing="0">                <tbody>                  <tr valign="top">                    <td nowrap="nowrap" width="25">		    </td>                    <td align="right" nowrap="nowrap" width="50"><small>Name:</small></td>                    <td style="width: 10px;">&nbsp;</td>                    <td align="left">                    <table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">                      <tbody>                        <tr>                          <td align="left" valign="top">                          <input name="username_textbox" value="' . $_SESSION['username_textbox'] . '"></td>                          <td align="right" width="20">&nbsp;</td>                          <td width="6"></td>                        </tr>                      </tbody>                    </table>                    <br>                    </td>                  </tr>                </tbody>              </table>              </td>            </tr>          </tbody>        </table>        </td>      </tr>    </tbody>  </table>  <table style="width: 600px; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="0" cellspacing="0">    <tbody>      <tr valign="top">        <td style="width: 25px;">        </td>        <td style="vertical-align: top; white-space: nowrap; text-align: right; width: 50px;"><label for="from"><small>From:<br>        </small></label></td>        <td style="width: 10px;"><br>        </td>        <td>        <table style="width: 100%;" border="0" cellpadding="0" cellspacing="0">          <tbody>            <tr>              <td style="vertical-align: top; width: 150px;">              <table style="width: 141px; height: 46px;" cellpadding="0" cellspacing="0">                <tbody>                  <tr>                    <td><input maxlength="2" name="date1_from_textbox" size="2" value="' . $_SESSION['date1_from_textbox'] . '" type="text"></td>                    <td>&nbsp;/&nbsp;</td>                    <td><input maxlength="2" name="date2_from_textbox" size="2" value="' . $_SESSION['date2_from_textbox'] . '" type="text"></td>                    <td>&nbsp;/&nbsp;</td>                    <td><input maxlength="4" name="date3_from_textbox" size="2" value="' . $_SESSION['date3_from_textbox'] . '" type="text"></td>                  </tr>                  <tr>                    <td colspan="2"><small>Month</small></td>                    <td colspan="2"><small>Day</small></td>                    <td colspan="2"><small>Year</small></td>                  </tr>                </tbody>              </table>              </td>              <td></td>              <td style="vertical-align: top; white-space: nowrap; text-align: right; width: 30px;">		<small>To:</small>	      </td>              <td>&nbsp;</td>              <td valign="top">              <table>                <tbody>                  <tr>                    <td><input maxlength="2" name="date1_to_textbox" size="2" value="' . $_SESSION['date1_to_textbox'] . '" type="text"></td>                    <td>&nbsp;/&nbsp;</td>                    <td><input maxlength="2" name="date2_to_textbox" size="2" value="' . $_SESSION['date2_to_textbox'] . '" type="text"></td>                    <td>&nbsp;/&nbsp;</td>                     <td><input maxlength="4" name="date3_to_textbox" size="2" value="' . $_SESSION['date3_to_textbox'] . '" type="text"></td>                  </tr>                  <tr>                    <td colspan="2"><small>Month</small></td>                    <td colspan="2"><small>Day</small></td>                    <td colspan="2"><small>Year</small></td>                  </tr>                </tbody>              </table>              </td>            </tr>            <tr>              <td style="text-align: left;" colspan="8"></td>            </tr>            <tr>              <td colspan="8" style="text-align: right;"><br>              </td>            </tr>          </tbody>        </table>        </td>      </tr>    </tbody>  </table>  <table width="100%">  <tr>  <td>    <input onClick="document.filter.clearform.click();document.filter.submit();" value="Show All" type="button">  </td><td>  <div style="text-align: right;">    <input onClick="document.filter.elements[\'item_textbox\'].value=\'\';document.filter.elements[\'username_textbox\'].value=\'\';document.filter.elements[\'date1_from_textbox\'].value=\'\';document.filter.elements[\'date2_from_textbox\'].value=\'\';document.filter.elements[\'date3_from_textbox\'].value=\'\';document.filter.elements[\'date1_to_textbox\'].value=\'\';document.filter.elements[\'date2_to_textbox\'].value=\'\';document.filter.elements[\'date3_to_textbox\'].value=\'\';" name="clearform" value="Clear Form" type="button">    <input name="filtersubmit" value="Search" type="submit"><br>  </div>  </td>  </tr>  </table></form><br>';	return $output;}function sales_history() {    global $user;    //delete row    $sid_to_delete=mysql_real_escape_string($_REQUEST['sid_to_delete']);    if(!($sid_to_delete==""))       db_query("delete from sold where sid=$sid_to_delete");    if( $_REQUEST['delete_all'] == "true" )	db_query("delete from sold where uid='$user->uid'");    //set default page    $page=mysql_real_escape_string($_REQUEST['page']);    if($page=="")	$page=1;    //save filtering information if the page has been submited by form- this will allow clicking of page links to continue working    if($_REQUEST['searchpost']=="true")    {       $_SESSION['item_textbox'] = mysql_real_escape_string($_REQUEST['item_textbox']);       $_SESSION['username_textbox'] = mysql_real_escape_string($_REQUEST['username_textbox']);       $_SESSION['date1_from_textbox']=mysql_real_escape_string($_REQUEST['date1_from_textbox']);       $_SESSION['date2_from_textbox']=mysql_real_escape_string($_REQUEST['date2_from_textbox']);       $_SESSION['date3_from_textbox']=mysql_real_escape_string($_REQUEST['date3_from_textbox']);       $_SESSION['date1_to_textbox']=mysql_real_escape_string($_REQUEST['date1_to_textbox']);       $_SESSION['date2_to_textbox']=mysql_real_escape_string($_REQUEST['date2_to_textbox']);       $_SESSION['date3_to_textbox']=mysql_real_escape_string($_REQUEST['date3_to_textbox']);       $date_from = $_SESSION['date3_from_textbox'] . '-' . $_SESSION['date1_from_textbox'] . '-' . $_SESSION['date2_from_textbox'];       $_SESSION['date_from']=$date_from;       $date_to = $_SESSION['date3_to_textbox'] . '-' . $_SESSION['date1_to_textbox'] . '-' . $_SESSION['date2_to_textbox'];       $_SESSION['date_to']=$date_to;    }    //search form    $output.=my_search_form();    //start sessions    $output.='<?php session_set_cookie_params(900);session_start();header("Cache-control: private"); //IE 6 Fix ?>';    $old_order_by=$_SESSION['order_by'];    //set default if none set allready for ordering table    if(!($_REQUEST['order_by']==""))	$_SESSION['order_by']=mysql_real_escape_string($_REQUEST['order_by']);    if($_SESSION['order_by']=="")	$_SESSION['order_by']="date";    //reverse ordering if link is clicked from same page    $old_page=$_SESSION['old_page'];    $_SESSION['old_page']=$page;    $order_by=$_SESSION['order_by'];        //basically does same thing - checks table should be reversed    if(!($_REQUEST['sid_to_delete_desc_asc']=="true"))    {    	if($old_page==$page)    	{    		if($_SESSION['order_by']==$old_order_by)    		{			if($_SESSION['desc_asc']=="desc")				$_SESSION['desc_asc']="asc";			else				$_SESSION['desc_asc']="desc";	    		}    	}    }    $desc_asc=$_SESSION['desc_asc'];        $results = db_query("select date,product,name,price from sold where uid='$user->uid'");    //determine what row to start at    $start_at=20*($page-1);    //start portion of query string    $db_query_string="select sid, date,product,name,price from sold where uid='$user->uid' ";    //for statistics function    $user_statistics.=$user->uid;    //appends restrictions to mysql query for limiting search results    if(!($_SESSION['username_textbox']==""))        $db_query_restrictions.=  " and name='" . $_SESSION['username_textbox'] . "' ";    if(!($_SESSION['item_textbox']==""))	$db_query_restrictions.=  " and product='" . $_SESSION['item_textbox'] . "' ";    if(!($_SESSION['date_from']=="--"))        $db_query_restrictions.= " and date >= '" . $_SESSION['date_from']  . "' and date <= '" . $_SESSION['date_to'] . "' ";    $db_query_string.=$db_query_restrictions;    //count rows & pages    $db_query_string_num_rows.=$db_query_string . " order by $order_by $desc_asc";    $results = db_query($db_query_string_num_rows);    $num_rows = mysql_num_rows($results);    $num_pages=ceil($num_rows/20);    $db_query_string.=" order by $order_by $desc_asc limit $start_at, 20";    $results = db_query($db_query_string);    //sales history table    $output.="<table width='100%'>";    $output.="<tr style='background:#f0f0f0'><td width='20%'><a href='?q=sales/history&page=$page&order_by=date'>Date</a></td><td width='30%'><a href='?q=sales/history&page=$page&order_by=product'>Product</a></td><td width='25%'><a href='?q=sales/history&page=$page&order_by=name'>Name</a></td><td width='15%'><a href='?q=sales/history&page=$page&order_by=price'>Price</a></td><td width='10%'><div style='text-align:center;'>Delete</div></td></tr>";    $output.="</table>";    //form starts here    $output.='<form action="" name="sales_history_form" onSubmit="var x=confirm(\'Are you sure you want to delete EVERYTHING?\');if(!x) document.sales_history_form.delete_all.value=\'false\';return x;">';    $output.='<input name="q" value="sales/history" type="hidden">';    $output.="<table width='100%'>";    $output.='<input name="page" value="'. $page . '" type="hidden">';    while ( $data = db_fetch_object($results))    {	if($_REQUEST[$data->sid]=="on")	    db_query("delete from sold where sid='$data->sid';");	else	    $output.="<tr><td width='20%'>$data->date</td><td width='30%'>$data->product</td><td width='25%'><a href='http://neopets.com/neomessages.phtml?type=send&recipient=$data->name' target='_blank'>$data->name</a></td><td width='15%'>$data->price</td><td width='10%'><div style='text-align:center;'><input type='checkbox' name='$data->sid'>" . mysql_real_escape_string($_REQUEST[$data->sid]) . "</div></td></tr>";    }    $output.="</table>";    $output.='<input name="sid_to_delete_desc_asc" value="true" type="hidden">';    $output.='<input name="delete_all" value="false" type="hidden">';    $output.='<div style="text-align:right;"><input type="submit" onClick="document.sales_history_form.delete_all.value=\'true\';" value="Delete All">';    $output.='<input type="button" onClick="document.sales_history_form.submit();" value="Delete"></div>';    $output.='</form>';    $output.='<div style="text-align:right;"><a href="?q=sales_history_deletion">Mass Deletion Page</a></div>';    //starts the page number navigation system    $output.="<table width='100%'><tr style='background:#f0f0f0'>";    if(!($page==1))	$output.="<td><a href='?q=sales/history&page=1&order_by=$order_by'>&lt;&lt;</a> </td>";    else	$output.="<td>&lt;&lt;</a> </td>";    if($page>=20)        $index=($page-10);    else	$index=1;    $start_page=$index;    for(;$index<($start_page+20);$index++)	if($index<=$num_pages)	if($page!=$index)		$output.="<td><div style='text-align:center;'><a href='?q=sales/history&page=$index&order_by=$order_by'>$index</a></div></td>";	else		$output.="<td><div style='text-align:center;'>$index</td>";    // >> if page is the last page don't display line    if((!($num_pages==$page)) && (!($num_pages==0)))    	$output.="<td><a href='?q=sales/history&page=$num_pages&order_by=$order_by'><div style='text-align:right;'>&gt;&gt;</div></a></td>";    else	$output.="<td><div style='text-align:right;'>&gt;&gt;</div></td>";    $output.="</tr></table>";    //form to submit sales data    $output.=sold_record();    //statistics    $output.=statistics_for_sales_history($db_query_restrictions, $user_statistics);    return $output;}function statistics_for_sales_history($db_query_restrictions, $user_statistics){	//sum of the amount of items a user has sold and sum of the price of all those items	$sum_results = mysql_query("select count(*) as sum_sold, sum(price) as sum_price from sold where uid='$user_statistics'");	$sum_sold = number_format(mysql_result($sum_results, 0, sum_sold));	$sum_price = number_format(mysql_result($sum_results, 0, sum_price));	$output.= "<div style='text-align:center;'>You have sold " . $sum_sold . " items for a total of " . $sum_price . " NP</div><br>";	//jump down page links	$output.='<center><a href="#total_items_sold">Total Items Sold</a> | ';	$output.='<a href="#sales_total_by_user">Sale Totals by User</a> | ';	$output.='<a href="#sales_total_by_date">Sale Totals by Date</a><br></center>';        $output.="<table style='margin-left:auto; margin-right:auto' width='351'><tr><td>";	//total items sold	$output.="<br><a name='total_items_sold'></a><div style='text-align:center;'>Total Items Sold<br><a href=?q=sales_history_total_sold>(Full List - More in-depeth)</a></div>";	$output.="<table width='350'><tr style='background:#f0f0f0'><td width='150'>Item</td><td width='100'><div style='text-align:right;'>Number Sold</div></td><td width='100'><div style='text-align:right;'>Price</div></td></tr>";   	$results=db_query("select product, count(*) as number_sold, price from sold where uid='$user_statistics' " . $db_query_restrictions . " group by product,price order by number_sold desc limit 200");	while ( $data = db_fetch_object($results))	{		$output.="<tr><td>" . $data->product . "</td>";		$output.="<td><div style='text-align:right;'>" . number_format($data->number_sold) . "</td>";		$output.="<td><div style='text-align:right;'>$data->price</div></td></tr>";	}	$output.="</table>";		$output.="<br><center><a href='#top'>Jump to top</a></center><br>";	//sales total by user	$output.="<br><a name='sales_total_by_user'></a><div style='text-align:center;'>Sale Totals by User<a href='?q=sales_history_users_spent'> (Full List)</a></div>";	$output.="<table style='margin-left:auto; margin-right:auto' width='300'><tr style='background:#f0f0f0'><td width='200'>User</td><td>NP Spent</td></tr>";		$results=db_query("select name,sum(price) as price from sold where uid='$user_statistics' " . $db_query_restrictions .  " group by name order by price desc limit 0, 30;");	while ( $data = db_fetch_object($results))	{		$output.="<tr><td><a href='http://neopets.com/neomessages.phtml?type=send&recipient=" . $data->name . "' target='_blank'>" . $data->name . "</a></td>";		$output.="<td><div style='text-align:right;'>" . number_format($data->price) . "</td></tr>";	}	$output.="</table>";	$output.="<br><center><a href='#top'>Jump to top</a></center><br>";		//sales total by date	$output.="<br><a name='sales_total_by_date'></a><div style='text-align:center;'>Sale Totals by Date <a href=?q=sales_history_totals_date>(Full List)</a></div>";	$output.="<table style='margin-left:auto; margin-right:auto' width='300'><tr style='background:#f0f0f0'><td width='200'>Date</td><td>NP Spent</td></tr>";			$results = db_query("select date,sum(price) as price from sold where uid='$user_statistics' " . $db_query_restrictions . " group by date order by date desc limit 7;");	while ( $data = db_fetch_object($results))	{		$output.="<tr><td>" . $data->date . "</td>";		$output.="<td><div style='text-align:right;'>" . number_format($data->price) . "</td></tr>";	}	$output.="</table>";            $output.="</td></tr></table>";	$output.="<br><center><a href='#top'>Jump to top</a></center><br>";		return $output;}?>