Skip to content

Instantly share code, notes, and snippets.

View kadimi's full-sized avatar
I love programming...

Nabil Kadimi kadimi

I love programming...
View GitHub Profile
@kadimi
kadimi / rules_notif_order_status_update_in_progress_or_completed.json
Last active August 29, 2015 13:57
Drupal Commerce: send and email with the Rules module when the order status is changed to "In progress" or "Completed"
{ "rules_notif_order_status_update_in_progress_or_completed" : {
"LABEL" : "Notification on order status update to \u0022in progress\u0022 or \u0022completed\u0022",
"PLUGIN" : "reaction rule",
"TAGS" : [ "Custom" ],
"REQUIRES" : [ "rules", "rules_i18n", "mimemail", "entity" ],
"ON" : [ "commerce_order_update" ],
"IF" : [
{ "data_is" : {
"data" : [ "commerce-order:status" ],
"op" : "IN",
@kadimi
kadimi / arabic_numerals.php
Last active December 26, 2021 08:13
Convert numbers in Arabic
<?php
/**
* Converts numbers in string from western to eastern Arabic numerals.
*
* @param string $str Arbitrary text
* @return string Text with western Arabic numerals converted into eastern Arabic numerals.
*/
function arabic_w2e($str) {
$arabic_eastern = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
@kadimi
kadimi / str_replace_last.js
Created April 2, 2014 22:16
Replace the last occurence of a string, useful if you want to replace the last comma with " and"...
/**
* Replace the last occurence of a string, useful if you want to replace the last comma with " and"...
*
* @param str needle The string to search
* @param str replacement The replacement string
* @param str str The subject
* @return str The new string
*/
function str_replace_last(needle, replacement, str) {
var needle_index = str.lastIndexOf(needle);
@kadimi
kadimi / array_to_real_text.js
Created April 2, 2014 22:25
Convert an array of words to real text.
function array_to_real_text (arr) {
txt = arr.join(", " );
return txt = str_replace_last(', ', ' and ', txt);
}
function str_replace_last(needle, replacement, str) {
var needle_index = str.lastIndexOf(needle);
if (needle_index < 0) {
return str;
} else {
@kadimi
kadimi / per_post_head_js.php
Created April 9, 2014 15:56
Add Javascript in header for a post, a page or a custom type post, add your code in a custom field with the name "js"
<?php
/**
* Add JavaScript code to the head section
*
* Add a custom field with the name "js" and this plugin will
* add it content to the head section of the page/post wrapped
* correctly inside <script> tags.
*/
add_action('wp_head', 'per_post_head_js');
function per_post_head_js() {
@kadimi
kadimi / toggle-attr.js
Last active August 7, 2023 14:33 — forked from mathiasbynens/toggleAttr() jQuery plugin
Toggle checked, selected, disabled, checked, readonly, multiple, etc…
/**
* toggleAttr() jQuery Plugin
*
* Toggle checked, selected, disabled, checked, readonly, multiple, etc…
*/
jQuery.fn.toggleAttr = function (attr) {
return this.each(function () {
var $this = $(this);
if ($this.attr(attr)) {
$this.removeAttr(attr);
@kadimi
kadimi / detect_remote_change.js
Last active August 7, 2023 14:33
Sometimes I need the "change" event to be triggered even when elements that are not changed directly by the user, this works good so far.
var t = 200; // The interval at which new change ares detected
jQuery( document ).ready( function() {
// Detect remote change
$( 'input, textarea' ).each( function() {
var $this = $( this );
$this.data( 'value', $this.val() );
setInterval( function() {
if( $this.val() !== $this.data( 'value' ) ) {
// Do not trigger if element does not have focus
@kadimi
kadimi / isURL.js
Last active August 29, 2015 14:07
Check if a string is a valid URL (very basic) -- see http://jsfiddle.net/nabil_kadimi/ftx9196o/
/**
* @author Nabil Kadimi (nabil * at * kadimi * com)
* @link http://jsfiddle.net/nabil_kadimi/ftx9196o/
*/
function isURL( url ) {
var pattern = new RegExp('^(https?:\\/\\/)?' // protocol
+ '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' // domain name
+ '((\\d{1,3}\\.){3}\\d{1,3}))' // OR ip (v4) address
+ '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' // port and path
+ '(\\?[;&a-z\\d%_.~+=-]*)?' // query string
@kadimi
kadimi / hslDiff.js
Last active August 29, 2015 14:08
HSL difference between two colors
/**
* Calculate the difference in hue, saturation and lightness between two colors
*
* The function requires tinycolor (https://github.com/bgrins/TinyColor)
* Example: http://jsfiddle.net/nabil_kadimi/a9cqqth0/2/
*
* @param {String} s Source color in hexadecimal
* @param {String} d Destination color in hexadecimal
* @return {Array} Array of differences [hue, saturtion, lightness]
*/
@kadimi
kadimi / is_prime_number.php
Last active November 6, 2015 16:56
is_prime_number.php
<?pphp
/**
* Check if a number is a prime number
* @param int $n The number.
* @retun boolean True if $n is a prime number, false otherwise.
*/
function is_prime_number($n) {
$is_prime = true;
$max_count = ceil($n/2);
for($i=2; $i<$max_count; $i++) {