Skip to content

Instantly share code, notes, and snippets.

@vglebov
Created December 28, 2011 11:58
Show Gist options
  • Select an option

  • Save vglebov/1527714 to your computer and use it in GitHub Desktop.

Select an option

Save vglebov/1527714 to your computer and use it in GitHub Desktop.
WHMCS-4.5.1.Client Area.My Quotes.Customization.
<?php
/**
* User: vglebov
* Date: 4/22/11
* Time: 3:02 PM
*/
class Datasource
{
public $debug = false;
function __construct($config, $debug = false)
{
$this->debug = $debug;
include $config;
$this->log_debug("Load config from path {$config}");
$this->link = mysql_connect($db_host, $db_username, $db_password);
if (!$this->link) {
die($this->log_debug("Couldn't connect: {$db_host}, {$db_username}" . mysql_error()));
}
$db_selected = mysql_select_db($db_name, $this->link);
if (!$db_selected) {
die ($this->log_debug("Can't use {$db_name} : " . mysql_error()));
}
$this->log_debug("Connected to MySql database {$db_name}, host:{$db_host}, user: {$db_username}.");
}
/**
* получить список элементов оценки
* @param $quote_id
* @return array
*/
public function getQuoteItems($uid, $quote_id)
{
$query_get_quote_details_by_id = <<< END
SELECT qi.*
FROM
tblquotes q
INNER JOIN tblquoteitems qi
ON q.id = qi.quoteid
WHERE
q.userid = ?
AND q.id = ?
END;
$res = array();
$resource = $this->mysql_query($query_get_quote_details_by_id, array($uid, $quote_id));
while ($row = mysql_fetch_assoc($resource)) {
$res[] = $row;
}
return $res;
}
public function mysql_query($query, $params = array())
{
foreach ($params as $val) {
if (is_array($val)) {
$val = '(' . implode(', ', array_map(array($this, 'escape_sql'), $val)) . ')';
} else {
$val = $this->escape_sql($val);
}
$query = substr_replace($query, $val, strpos($query, '?'), 1);
}
$result = mysql_query($query, $this->link);
$this->log_debug("mysql_query: {$query}");
if (!$result) {
echo "<pre>";
$error = $this->log_debug('mysql_error: ' . mysql_error());
echo "Couldn't run query: {$query}, {$error}";
echo "</pre>";
exit;
}
return $result;
}
public function escape_sql($val)
{
return isset($val) ? is_numeric($val) ? $val : "'" . mysql_real_escape_string($val) . "'" : "NULL";
}
function log_debug($message){
if($this->debug) {
syslog(LOG_DEBUG, $message);
}
return $message;
}
}
<?php
/**
* User: vglebov
* Date: 12/27/11
* Time: 17:45 AM
* Ticket: CRM-45 WHMCS в табе Quotes для клиентов сделать новую таблицу
*/
session_start();
// для предотвращения уничтожения сессии GC нужно в нее регулярно что-то записывать
if( !isset($_SESSION['last_access']) || (time() - $_SESSION['last_access']) > 60 ){
$_SESSION['last_access'] = time();
}
$controller = new QuoteItemsController();
$controller->dispatch();
class QuoteItemsController
{
public function dispatch()
{
$action = 'indexAction';
$this->$action();
}
function indexAction()
{
include('configuration.php');
$customadminpath = isset($customadminpath) ? $customadminpath : 'admin';
require_once "{$customadminpath}/Datasource.php";
$datasource = new Datasource('configuration.php');
if(!$this->hasParam('quote_id')) {
echo "Can't fetch quote items. Parameter quote_id was empty.";
return;
}
$quote_id = $this->getParam('quote_id');
header('Content-type: application/json');
$quote_items = $datasource->getQuoteItems($_SESSION['uid'], $quote_id);
echo json_encode(array('quote_items' => $quote_items));
}
private function getParam($name, $default = null)
{
if(array_key_exists($name, $_REQUEST)) {
return $_REQUEST[$name];
}
return $default;
}
private function hasParam($name)
{
return array_key_exists($name, $_REQUEST);
}
}
<h2>{$LANG.quotestitle}</h2>
<div class="tablebg">
<table class="datatable" width="100%" border="0" cellspacing="1" cellpadding="3">
<tbody>
<tr>
<th>Quote #</th>
<th>Subject</th>
<th>Create Date</th>
<th>Valid Until</th>
<th>Total</th>
<th>Stage</th>
<th width="20">PDF</th>
</tr>
{foreach from=$quotes key=num item=quote}
<tr>
<td><a name="QuoteItemsHandle" id="{$quote.id}" href="#">{$quote.id}</a></td>
<td>{$quote.subject}</td>
<td>{$quote.datecreated}</td>
<td>{$quote.validuntil}</td>
<td>{$quote.total}</td>
<td>{$quote.stage}</td>
<td>
<a href="dl.php?type=q&id={$quote.id}"><img src="images/pdf.png" align="absmiddle"
border="0"/> {$LANG.quotedownload}</a>
</td>
<td></td>
</tr>
{foreachelse}
<br/>
<tr>
<td>
<p align="center">{$LANG.noquotes}</p>
</td>
</tr>
{/foreach}
</tbody>
</table>
</div>
{literal}
<script type="text/javascript">
quote_id = 0;
String.prototype.format = function () {
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
function loadQuoteItems(id) {
jQuery("#QuoteItemsData").html("Loading...");
jQuery.ajax({
type:"GET",
// dataType: "text",
url:"get-quoteitems.php",
data:{quote_id:id},
error:function (jqXHR, textStatus) {
jQuery("#QuoteItemsData").html("Request failed: " + textStatus);
},
success:function (data) {
jQuery("#QuoteItemsData").html('');
jQuery("#QuoteItemsData").append(
'<table id="QuoteItemsDataTable" width="100%" cellspacing="1" bgcolor="#cccccc" align="center">' +
'<tr><th>Qty</th><th>Description</th><th>Unit Price</th><th>Discount %</th><th>Total</th><th>Taxed</th></tr>' +
'</table>');
jQuery.each(data.quote_items, function (key, item) {
jQuery("#QuoteItemsDataTable").append(
('<tr bgcolor="#ffffff" style="text-align:center;">' +
'<td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td><input type="checkbox" {5} /></td>' +
'</tr>').format(
item.quantity,
item.description,
item.unitprice,
item.discount,
item.discount > 0 ? item.unitprice * item.discount / 100 : item.unitprice,
item.taxable > 0 ? "checked" : ""));
});
}
});
}
jQuery(document).ready(function () {
jQuery.getScript("includes/jscript/jqueryui.js", function () {
jQuery('body').append('<div id="QuoteItemsDialog" title="Quote Items"><div id="QuoteItemsData"><p>Can\'t get Quote Items.</p></div></div>');
if (document.createStyleSheet) {
document.createStyleSheet("includes/jscript/css/ui.all.css");
}
else {
jQuery("head").append(jQuery("<link rel='stylesheet' href='includes/jscript/css/ui.all.css' type='text/css' media='screen' />"));
}
jQuery("#QuoteItemsDialog").dialog({
modal:true,
overlay:{
opacity:0.7,
background:"black"
},
autoOpen:false,
width:500
});
jQuery("a[name=QuoteItemsHandle]").click(
function () {
loadQuoteItems(this.id);
jQuery("#QuoteItemsDialog").dialog('open');
return false;
}
);
});
});
</script>
{/literal}
<br/><br/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment