Skip to content

Instantly share code, notes, and snippets.

View marekkalnik's full-sized avatar
😁

Marek Kalnik marekkalnik

😁
View GitHub Profile
@marekkalnik
marekkalnik / gist:1391507
Created November 24, 2011 14:52
jQuery chaining and code scoping
var formHandling = function(formSelector) {
var onFormSubmit, onButtonClick, onQuantityChange;
jQuery(formSelector).bind('submit', onFormSubmit).end()
.find('button').bind('click', onButtonClick).end()
.find('input[name="quantity"]').bind('change', onQuantityChange);
onFormSubmit = function() { ... };
onButtonClick = function() { ... };
onQuantityChange = function() { ... };
@marekkalnik
marekkalnik / gist:1400418
Created November 28, 2011 13:36
Array diff withe one level recursiveness
<?php
$array1 = array(
array('code' => 1, 'quantity' => 2)
);
$array2 = array(
array('code' => 1, 'quantity' => 2)
);
@marekkalnik
marekkalnik / gist:1401438
Created November 28, 2011 18:33
Display item in multiple rows in twig
{% for address in customer.getAddresses %}
{% if loop.index % 3 == 1 %}
<div class="row">
{% endif %}
{# display item #}
{% if loop.index % 3 == 0 or loop.last %}
</div>
{% endif %}
{% endfor %}
<?php
/**
* Converts elements divided by newline characters to list items
* @param String $text
* @param Array $htmlAttrs
*/
function nl2li($text, array $htmlAttrs = null) {
if (!empty($htmlAttrs)) {
$attributes = array_walk($htmlAttrs, function($key, $value) {
@marekkalnik
marekkalnik / gist:1859797
Created February 18, 2012 15:30
Javascript private methods
Soldier = function() {
var shot, reload, runAway,
ammo = 10,
clips = 2;
shot = function() {
if(ammo == 0 && reload() === false) {
runAway();
} else {
ammmo -= 1;
@marekkalnik
marekkalnik / split-xml.awk
Created March 9, 2012 11:08
AWK - split XML subnodes into separate files
# This awk script splits an XML file and exports each node into separate file
# It doesn't work if the main node has an attribute
# It doesn't work if the main node contains a subnode with the same tagname, etc.
# Use at your own risk, think before using.
# Usage - replace "node" by your tag and run: $ awk -f split-xml.awk my.xml
/<node>/ {
rfile="node" count ".xml"
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > rfile
print $0 > rfile
getline
@marekkalnik
marekkalnik / cleanup_uft8.py
Created March 27, 2012 08:53
Replace utf caracters in file by ASCII equivalents
import os, codecs, unicodedata
def cleanup(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.ktr') or file.endswith('.kjb'):
filename = os.path.join(root, file)
f = codecs.open(filename, 'r', 'utf-8')
text = f.read()
print f
@marekkalnik
marekkalnik / kind.js
Created July 30, 2012 21:31
Kind window.unload popup
/**
* This won't work in newer FF versions as it doesn't handle onbeforeunload properly.
* Need another browser oriented hack...
*/
window.onbeforeunload = function (e) {
var newwindow;
e = e || window.event;
newwindow=window.open('http://www.google.com','cool-popup','height=800,width=800');
@marekkalnik
marekkalnik / benchmark.php
Created August 6, 2012 00:51
Check if string startsWith
<?php
function rand_str($length, $switch_chars)
{
$valid_chars = 'ABCDEFGHIJKLMNOPQRSabcdefghijklmnopqrs0123456789';
if ($switch_chars) {
$valid_chars = 'TUVWXYZtuvwxyz';
}
$random_string = "";
$num_valid_chars = strlen($valid_chars);
@marekkalnik
marekkalnik / dataformatters.php
Created September 12, 2012 23:27
It's just a view - code sample
<?php
abstract class TableDataFormatter
{
protected $columnConfiguration;
public function __construct(array $columnConfiguration)
{
$this->columnConfiguration = $columnConfiguration;
}