var _objects = {
interface0: {
data: new Array(),
getString: function () { return JSON.stringify(this); },
extend: function (args) {
var obj = (typeof args == 'string') ? eval('(' + args + ')') : args;
$.extend(this, obj, true);
return this;
},
parse: function (args) {
var i = args.toString().indexOf('{');
if (i == -1) i = args.toString().indexOf('}');
if (i == -1) { return { data: args.toString() }; }
return (typeof args == 'string') ? eval('(' + args + ')') : args;
}
},
create: function () {
var o = {};
if (arguments.length == 1) {
try {
//Long l = Long.parseLong(arguments[0]);
o = $.extend({}, { id: arguments[0] }, true);
} catch (ex) {
o = $.extend({}, { id: 0 }, true);
var obj = (typeof arguments[0] == 'string') ? eval('(' + arguments[0] + ')') : arguments[0];
o = $.extend({}, obj, true);
}
} else if (arguments.length == 2) {
var obj = (typeof arguments[1] == 'string') ? eval('(' + arguments[1] + ')') : arguments[1];
o = $.extend({}, obj, true);
o = $.extend(o, { id: arguments[0] }, true);
} else
o = $.extend({}, { id: 0 }, true);
o = $.extend(o, this.interface0, true);
o.data = new Array();
//usage::
//var n = new MyEntity(10);
//n.hi = "hi boroo";
//n.extend({name:"boroo"});
//var obj = new MyEntity(20, n.getString());
//var n = new MyEntity(10, "{name:'baatar'}");
//n.data["a"]="b";
//alert( n.data["a"] );
return o;
}
//var a = _objects.create(22);
//var b = _objects.create(33);
//b.hi = "welcome, boroo";
//b.id="1";
//a.extend(b.getString());
//alert(a.data.length);
};
(function ($) {
$._Calendar = new Date();
$.extend($._Calendar, {
month: function() { return $._Calendar.getMonth() + 1; },
months: function() { return ("" + ($._Calendar.getMonth() + 1)).length == 1 ? "0" + ($._Calendar.getMonth() + 1) : "" + ($._Calendar.getMonth() + 1); },
year: function() { return $._Calendar.getFullYear(); },
dow: function() { return $._Calendar.getDay(); },
dom: function() { return $._Calendar.getDate() },
doms: function() { return ("" + $._Calendar.getDate()).length == 1 ? "0" + $._Calendar.getDate() : "" + $._Calendar.getDate(); }
}, true);
$.extend($._Calendar, {
dateValue: function () { return $._Calendar.year() + "." + $._Calendar.months() + "." + $._Calendar.doms(); },
nowString: function () {
var d = new Date();
return d.toGMTString();
},
dateString: function () {
var d = new Date();
return d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate();
},
timeString: function () {
var d = new Date();
return d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
}
}, true);
$.extend($.expr[':'], {
'containsi': function (elem, i, match, array) {
//(elem.textContent || elem.innerText || '')
var val = $(elem).html().toLowerCase();
var str = (match[3] || "").toLowerCase();
return (val.indexOf(str) >= 0);
}
});
$.centerPopup = function() {
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#popupContent").height();
var popupWidth = $("#popupContent").width();
//centering
$("#popupContent").css({
"position": "absolute",
"top": windowHeight/2-popupHeight/2,
"left": windowWidth/2-popupWidth/2
});
//only need force for IE6
$("#backgroundPopup").css({
"height": windowHeight
});
};
$.filterTable = function(text, dataTableSelector, targetTableSelector) {
if (typeof $(dataTableSelector).data("Table-Tr-Data") == 'undefined' || $(dataTableSelector).data("Table-Tr-Data") == null) {
$(dataTableSelector).data("Table-Tr-Data", $(dataTableSelector).find("> tbody > tr"));
}
var plus = (text.indexOf('+') > -1);
var trs = $(dataTableSelector).data("Table-Tr-Data");
var target = $(targetTableSelector).find(">tbody");
$(target).children().remove();
if (plus) {
var txts = text.split('+');
for (var i = 0; i < txts.length; i++) {
var txt = txts[i];
trs = $(trs).filter(":containsi('" + txt + "')");
}
$(target).append(trs);
} else {
var txts = text.split(' ');
for (var i = 0; i < txts.length; i++) {
var txt = txts[i];
var trs0 = $(trs).filter(":containsi('" + txt + "')");
$(target).append(trs0);
}
}
return trs;
};
$.fn.makeDatePicker = function (method, format) {
var calendarButtonGif = "/Scripts/jquery/images/calendar.gif";
var target = $(this).datepicker({
showOn: "both",
buttonImage: calendarButtonGif,
buttonImageOnly: true,
showOtherMonths: true,
selectOtherMonths: true,
changeMonth: true,
changeYear: true,
onSelect: method,
dateFormat: ((format == null || !format || typeof format == 'undefined') ? $.datepicker.ATOM : format),
yearRange: 'c-100:c+20'
//constrainInput: false
}).change(method);
var inst = $.datepicker._getInst(target[0]);
var dateFormat = $.datepicker._get(inst, 'dateFormat');
var settings = $.datepicker._getFormatConfig(inst);
if(target.val())
{
//alert($.datepicker.parseDate(dateFormat, new Date(target.val()), settings));
$.datepicker._selectDate("#" + target.attr("id"), $.datepicker._formatDate(inst, new Date(target.val())));
}
return target;
};
$.fn.createDropDownMenu = function (options) {
var a, closetimerHandle = 0, settings = jQuery.extend({
timeout: 100,
item: "li",
query: "> li > a",
subQuery: "ul",
hoverClass: "hoverClass"
}, options),
closeMethod = function () { // close
$(a).removeClass(settings.hoverClass).parent(settings.item).find(settings.subQuery)
.css("visibility", "hidden");
},
closetimer = function () { // go close timer
cancelclosetimer();
closetimerHandle = window.setTimeout(closeMethod, settings.timeout);
},
cancelclosetimer = function () { // cancel close timer
if (closetimerHandle) {
window.clearTimeout(closetimerHandle);
closetimerHandle = null;
}
},
aopen = function () {
cancelclosetimer();
closeMethod();
$(this).addClass(settings.hoverClass).parent(settings.item).find(settings.subQuery)
.mouseover(cancelclosetimer)
.mouseout(closetimer)
.css("visibility", "visible");
};
a = $(this).find(settings.query);
$(a).mouseover(aopen).mouseout(closetimer);
return this;
};
$.fn.ajaxQuery = function (url, callback, options) {
var loadingImageGif = "/Content/admin/images/loading.gif",
params, off = url.indexOf("?"), setting = $.extend({
loadingImage: loadingImageGif,
loadingImageWidth: 12,
loadingImageHeight: 12
}, options);
if (off >= 0) {
params = url.slice(off + 1, url.length);
url = url.slice(0, off);
}
var type = "GET";
var self = this;
// Request the remote document
$.ajax({
url: url,
type: type,
dataType: "html",
data: params,
beforeSend: function () {
self.html($("<img width='" + setting.loadingImageWidth + "' height='" + setting.loadingImageHeight + "'>")
.attr("src", setting.loadingImage)
.css("border", "none"));
},
success: function (data) {
alert(data.toLowerCase() == "true" ? "Амжилттай" : (data.toLowerCase() == "false" ? "Амжилтгүй !!!" : data));
}
});
return this;
};
$.fn.ajaxGet = function (url, options) {
var loadingImageGif = "/Content/admin/images/loading.gif",
params, off = url.indexOf("?"), setting = $.extend({
loadingImage: loadingImageGif,
loadingImageWidth: 36,
loadingImageHeight: 36
}, options);
if (off >= 0) {
params = url.slice(off + 1, url.length);
url = url.slice(0, off);
}
var self = this;
// Request the remote document
$.ajax({
url: url,
type: "GET",
dataType: "html",
data: params,
beforeSend: function () {
self.html($("<img width='" + setting.loadingImageWidth + "' height='" + setting.loadingImageHeight + "'>")
.attr("src", setting.loadingImage)
.css("border", "none"));
},
success: function (data) {
self.html(data);
}
});
return this;
};
$.fn.ajaxPost = function (formIndex, options) {
var loadingImageGif = "/Content/admin/images/loading.gif",
setting = $.extend({
loadingImage: loadingImageGif,
loadingImageWidth: 36,
loadingImageHeight: 36
}, options);
var self = this;
var form = $($(self).find("form").get(formIndex));
// Request the remote document
$.ajax({
url: $(form).attr("action"),
type: "POST",
dataType: "html",
data: $(form).serialize(),
beforeSend: function () {
$(self).html($("<img width='" + setting.loadingImageWidth + "' height='" + setting.loadingImageHeight + "'>")
.attr("src", setting.loadingImage)
.css("border", "none"));
},
success: function (data) {
self.html(data);
// responseText = jqXHR.responseText;
// if (jqXHR.isResolved()) {
// jqXHR.done(function (r) {
// responseText = r;
// });
// self.html(responseText);
// }
// if (callback) {
// self.each(callback, [responseText, status, jqXHR]);
// }
}
});
return this;
};
})( jQuery );
Tuesday, October 29, 2013
Wednesday, October 23, 2013
codeigniter web cache example, custom data cache with file adapter, db cache is automatic
//for db cache
$db['default']['cache_on'] = TRUE;//FALSE;
$db['default']['cachedir'] = APPPATH.'cache/db';//''
//for web cache
$config['cache_path'] = FCPATH.'cache/';
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class items extends HO_Site {
function __construct() {
parent::__construct();
//for web cache
$this->output->cache(intval(config_item('web_cache_minute')));
$this->output->enable_profiler(ENVIRONMENT == 'development');
//for custom cache
$this->load->driver('cache', array('adapter' => 'file'));
}
//web output cache
function show($id) {
$items = array();
array_push($items, $id . ".boroo");
array_push($items, $id . ".boldbaatar");
// Load the subview
$this->view($items);
}
//custom data cache
function ss() {
$this->load->model('category_model');
$data = array();
if (!$data['category'] = $this->cache->get('id')){
$data['category'] = $this->category_model->get_list();
$this->cache->save('id', $data['category'], 300);
} else {
echo 'sql queries cached...';
}
$this->view($data);
}
}
?>
$db['default']['cache_on'] = TRUE;//FALSE;
$db['default']['cachedir'] = APPPATH.'cache/db';//''
//for web cache
$config['cache_path'] = FCPATH.'cache/';
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class items extends HO_Site {
function __construct() {
parent::__construct();
//for web cache
$this->output->cache(intval(config_item('web_cache_minute')));
$this->output->enable_profiler(ENVIRONMENT == 'development');
//for custom cache
$this->load->driver('cache', array('adapter' => 'file'));
}
//web output cache
function show($id) {
$items = array();
array_push($items, $id . ".boroo");
array_push($items, $id . ".boldbaatar");
// Load the subview
$this->view($items);
}
//custom data cache
function ss() {
$this->load->model('category_model');
$data = array();
if (!$data['category'] = $this->cache->get('id')){
$data['category'] = $this->category_model->get_list();
$this->cache->save('id', $data['category'], 300);
} else {
echo 'sql queries cached...';
}
$this->view($data);
}
}
?>
Monday, October 21, 2013
php super xml writer from array objects with attr, cdata
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
/**
* b.boldbaatar
*
* xml tool for array to xml
* @author HiimelOyun Dev Team
* @link boroo_c@yahoo.com
*/
// ------------------------------------------------------------------------
class HO_XmlTool {
protected static $FOLDER = 'xml/';
public function __construct() {
}
// read
// -----------------------------------------------------------------------
public function read_file($file_name) {
$file_path = FILESPATH . self::$FOLDER . trim($file_name, '/');
$reader = new XMLReader();
if (!$reader->open(site_url($file_path))) {
die("Failed to open 'data.xml'");
}
$output = '';
if ($reader->read()) {
$output = $reader->readOuterXml();
}
$reader->close();
return $output;
}
public function load_file($file_name) {
$file_path = FILESPATH . self::$FOLDER . trim($file_name, '/');
$reader = new XMLReader();
if (!$reader->open(site_url($file_path))) {
die("Failed to open 'data.xml'");
}
$node = NULL;
while ($reader->read()) {
$node = $reader->expand();
if ($node->nodeType == XMLReader::ELEMENT) {
break;
}
}
$reader->close();
return $node;
}
public function load_xml($xml) {
$reader = new XMLReader();
if (!$reader->xml($xml)) {
die("Failed to open 'data.xml'");
}
$node = NULL;
while ($reader->read()) {
$node = $reader->expand();
if ($node->nodeType == XMLReader::ELEMENT) {
break;
}
}
$reader->close();
return $node;
}
// write
// -----------------------------------------------------------------------
public function &start($prm_xsltFilePath = '') {
$xml_writer = new XMLWriter();
$xml_writer->openMemory();
$xml_writer->setIndent(true);
$xml_writer->setIndentString(' ');
$xml_writer->startDocument('1.0', 'UTF-8');
if ($prm_xsltFilePath) {
$this->writePi('xml-stylesheet', 'type="text/xsl" href="' . $prm_xsltFilePath . '"');
}
return $xml_writer;
}
public function end(&$xml_writer) {
$xml_writer->endDocument();
return $xml_writer->outputMemory();
}
public function fromArray($prm_array, &$xml_writer) {
foreach ($prm_array as $index => $element) {
if (is_array($element)) {
if (!is_int($index)) {
$this->_startElement($xml_writer, $index, $element);
$this->_endElement($xml_writer);
} else {
$this->fromArray($element, $xml_writer);
}
} else {
$this->_startElement($xml_writer, $index, $element);
$this->_endElement($xml_writer);
}
}
}
private function _startElement(&$xml_writer, $name, $value) {
$cdata = false;
$attrs = array();
$index = json_decode($name);
if (is_array($index) && !empty($index)) {
$list = (array) $index;
foreach ($list as $item) {
if (is_bool($item)) {
$cdata = $item;
} elseif (is_object($item)) {
$attrs = (array) $item;
} else {
$name = $item;
}
}
}
$xml_writer->startElement($name);
if (count($attrs) > 0) {
foreach ($attrs as $attr_name => $attr_value) {
$xml_writer->writeAttribute($attr_name, $attr_value);
}
}
if (!is_array($value)) {
if ($cdata) {
$xml_writer->writeCData($value);
} else {
if (!is_null($value)) {
$xml_writer->text($value);
}
}
} else {
$this->fromArray($value, $xml_writer);
}
}
private function _endElement(&$xml_writer) {
$xml_writer->endElement();
}
}
?>
usage
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class some extends HO_Site {
public function index() {
$this->load->library('XmlTool');
$val = json_encode(array('name', array('role' => 'admin'), TRUE));
$prm_array = array(
'class' => array(
//'["name",{"role":"admin"},true]'
$val => 'computer software',
'employees' => array(
array(
'employee' => array(
'name' => 'boroo',
'tasks' => array(
array(
'task' => array(
'name' => 'com1'
)
),
array(
'task' => array(
'name' => 'com2',
'time' => '3'
)
),
)
)
),
array(
'employee' => array(
'name' => 'odgii',
)
)
)
)
);
$xml = new HO_XmlTool();
$xml_writer = $xml->start();
$xml->fromArray($prm_array, $xml_writer);
header('Content-type: text/xml');
echo $xml->end($xml_writer);
}
}
?>
if (!defined('BASEPATH'))
exit('No direct script access allowed');
/**
* b.boldbaatar
*
* xml tool for array to xml
* @author HiimelOyun Dev Team
* @link boroo_c@yahoo.com
*/
// ------------------------------------------------------------------------
class HO_XmlTool {
protected static $FOLDER = 'xml/';
public function __construct() {
}
// read
// -----------------------------------------------------------------------
public function read_file($file_name) {
$file_path = FILESPATH . self::$FOLDER . trim($file_name, '/');
$reader = new XMLReader();
if (!$reader->open(site_url($file_path))) {
die("Failed to open 'data.xml'");
}
$output = '';
if ($reader->read()) {
$output = $reader->readOuterXml();
}
$reader->close();
return $output;
}
public function load_file($file_name) {
$file_path = FILESPATH . self::$FOLDER . trim($file_name, '/');
$reader = new XMLReader();
if (!$reader->open(site_url($file_path))) {
die("Failed to open 'data.xml'");
}
$node = NULL;
while ($reader->read()) {
$node = $reader->expand();
if ($node->nodeType == XMLReader::ELEMENT) {
break;
}
}
$reader->close();
return $node;
}
public function load_xml($xml) {
$reader = new XMLReader();
if (!$reader->xml($xml)) {
die("Failed to open 'data.xml'");
}
$node = NULL;
while ($reader->read()) {
$node = $reader->expand();
if ($node->nodeType == XMLReader::ELEMENT) {
break;
}
}
$reader->close();
return $node;
}
// write
// -----------------------------------------------------------------------
public function &start($prm_xsltFilePath = '') {
$xml_writer = new XMLWriter();
$xml_writer->openMemory();
$xml_writer->setIndent(true);
$xml_writer->setIndentString(' ');
$xml_writer->startDocument('1.0', 'UTF-8');
if ($prm_xsltFilePath) {
$this->writePi('xml-stylesheet', 'type="text/xsl" href="' . $prm_xsltFilePath . '"');
}
return $xml_writer;
}
public function end(&$xml_writer) {
$xml_writer->endDocument();
return $xml_writer->outputMemory();
}
public function fromArray($prm_array, &$xml_writer) {
foreach ($prm_array as $index => $element) {
if (is_array($element)) {
if (!is_int($index)) {
$this->_startElement($xml_writer, $index, $element);
$this->_endElement($xml_writer);
} else {
$this->fromArray($element, $xml_writer);
}
} else {
$this->_startElement($xml_writer, $index, $element);
$this->_endElement($xml_writer);
}
}
}
private function _startElement(&$xml_writer, $name, $value) {
$cdata = false;
$attrs = array();
$index = json_decode($name);
if (is_array($index) && !empty($index)) {
$list = (array) $index;
foreach ($list as $item) {
if (is_bool($item)) {
$cdata = $item;
} elseif (is_object($item)) {
$attrs = (array) $item;
} else {
$name = $item;
}
}
}
$xml_writer->startElement($name);
if (count($attrs) > 0) {
foreach ($attrs as $attr_name => $attr_value) {
$xml_writer->writeAttribute($attr_name, $attr_value);
}
}
if (!is_array($value)) {
if ($cdata) {
$xml_writer->writeCData($value);
} else {
if (!is_null($value)) {
$xml_writer->text($value);
}
}
} else {
$this->fromArray($value, $xml_writer);
}
}
private function _endElement(&$xml_writer) {
$xml_writer->endElement();
}
}
?>
usage
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class some extends HO_Site {
public function index() {
$this->load->library('XmlTool');
$val = json_encode(array('name', array('role' => 'admin'), TRUE));
$prm_array = array(
'class' => array(
//'["name",{"role":"admin"},true]'
$val => 'computer software',
'employees' => array(
array(
'employee' => array(
'name' => 'boroo',
'tasks' => array(
array(
'task' => array(
'name' => 'com1'
)
),
array(
'task' => array(
'name' => 'com2',
'time' => '3'
)
),
)
)
),
array(
'employee' => array(
'name' => 'odgii',
)
)
)
)
);
$xml = new HO_XmlTool();
$xml_writer = $xml->start();
$xml->fromArray($prm_array, $xml_writer);
header('Content-type: text/xml');
echo $xml->end($xml_writer);
}
}
?>
Monday, October 7, 2013
codeigniter shopping cart items example
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Home extends HO_Controller {
public function index() {
$this->load->library('cart');
$items = $this->cart->contents();
if (!isset($items) || empty($items)) {
$data = array(
'id' => 2546,
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
);
$this->cart->insert($data);
} else {
foreach ($items as $data) {
echo $data['name'].'->'.$data['price'];
}
}
}
}
?>
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Home extends HO_Controller {
public function index() {
$this->load->library('cart');
$items = $this->cart->contents();
if (!isset($items) || empty($items)) {
$data = array(
'id' => 2546,
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
);
$this->cart->insert($data);
} else {
foreach ($items as $data) {
echo $data['name'].'->'.$data['price'];
}
}
}
}
?>
Subscribe to:
Posts (Atom)