Skip to content

Instantly share code, notes, and snippets.

View Stoffo's full-sized avatar

Stefan Lange Stoffo

  • Hamburg, Germany
  • 17:24 (UTC +01:00)
View GitHub Profile
@Stoffo
Stoffo / check_mongodb_collection_exists.php
Last active September 21, 2015 14:23
Check MongoDB Collection Existance
<?php
/**
* Checks if a Collection already exists.
* Returns true if Collection exists, otherwise it returns false.
*
* @param $collection
*
* @return bool
*/
@Stoffo
Stoffo / JSModulePattern1.js
Last active August 29, 2015 14:08
This pattern is used to mimic classes in conventional software engineering and focuses on public and private access to methods & variables. The module pattern strives to improve the reduction of globally scoped variables, thus decreasing the chances of collision with other code throughout an application.
( function( window, undefined ) {
// normally variables & functions start with a lowercase letter but with modules, that is not the case.
// The general tradition is to start them with a capital letter instead.
function MyModule() {
// `this` refers to the instance of `MyModule` when created
this.myMethod = function myMethod() {
alert( 'my method' );
};
@Stoffo
Stoffo / JSModulePattern2.js
Created November 6, 2014 09:55
Another example of the module pattern that exposes the module a little differently and makes use of a shared private cache. This method encourages more of an object creation approach where we can optimize performance by being efficient with shared storage.
var MyModule = ( function( window, undefined ) {
// this object is used to store private variables and methods across multiple instantiations
var privates = {};
function MyModule() {
this.myMethod = function myMethod() {
alert( 'my method' );
};
@Stoffo
Stoffo / CSV_MySQL_import.php
Last active August 29, 2015 14:10
import a CSV file easily with this file
#!/usr/bin/php
<?php
error_reporting(0);
$server = "localhost";
$port = 3306;
$username = "csv_test";
$password = "test";
$db = "test_db";
@Stoffo
Stoffo / http_statuscodes.php
Last active October 26, 2017 11:24
PHP Array with alle HTTP Status Codes
<?
$http_codes = array(
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
$.urlParam = function (name) {
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results === null) {
return null;
}
return results[1] || 0;
};
var PID = $.urlParam("PID"); //123456
@Stoffo
Stoffo / has_focus.js
Created March 4, 2015 13:33
Check if the Window has the focus or not
var has_focus;
$(window).focus(function () {
has_focus = true;
document.title = "focus";
}).blur(function () {
has_focus = false;
document.title = "no focus"
});
@Stoffo
Stoffo / jsonp_example.php
Last active September 21, 2015 14:28
simple example for an implemention of jsonp in php
<?php
$callback = $_GET['callback'];
$json = json_encode(['foo' => 'bar'], JSON_FORCE_OBJECT);
if ($callback) {
header('Content-Type: text/javascript');
echo $callback.'(';
echo $json;
echo ');';
@Stoffo
Stoffo / twig_file_date_extension.php
Last active May 24, 2020 07:47
Twig Function to get the file mtime in template for HTTP Caching
<?php
$function_filedate = new Twig_SimpleFunction(
'fileDate',
/**
* @param $file_path
* This function generates a new file path with the last date of filechange
* to support better better client caching via Expires header:
* i.e:
@Stoffo
Stoffo / remove_documents_mongodb.js
Created October 29, 2015 12:29
Remove Documents older than x days in MongoDB
var date = new Date();
var daysToDeletion = 120;
var deletionDate = new Date(date.setDate(date.getDate() - daysToDeletion));
printjson(deletionDate);
var db = db.getSiblingDB('db')
db.getMongo().setSlaveOk();
printjson(db.messages.find({insertDate : {$lt : deletionDate}}).count());