Skip to content

Instantly share code, notes, and snippets.

View ternavsky's full-sized avatar

Eugene Ternavsky ternavsky

  • https://simplecodesoftware.com/
  • Russia, Novocherkassk
View GitHub Profile
@ternavsky
ternavsky / csv_download.php
Last active August 29, 2015 14:22
Force download CSV file
function forceCSVFileDownload($filename, $csv) {
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Type: csv/plain'); # Don't use application/force-download - it's not a real MIME type, and the Content-Disposition header is sufficient
header('Content-Length: ' . strlen($csv));
header('Connection: close');
print($csv);
}
@ternavsky
ternavsky / str_putcsv.php
Last active August 29, 2015 14:22
Converts array to csv string
public static function str_putcsv($input, $delimiter = ',', $enclosure = '"') {
// Open a memory "file" for read/write...
$fp = fopen('php://temp', 'r+');
// ... write the $input array to the "file" using fputcsv()...
foreach ($input as $fields) {
fputcsv($fp, $fields, $delimiter, $enclosure);
}
// ... rewind the "file" so we can read what we just wrote...
@ternavsky
ternavsky / SerializableModel.php
Last active March 8, 2020 20:12
OctoberCMS Model behavior that allows to store some model's attributes in json format in grouping fields in db.
<?php namespace Path\To\Behaviors;
use System\Classes\ModelBehavior;
/**
* Model behavior that allows to store some model's attributes
* in json format in grouping fields in db.
*
* USAGE: In your model add
*
@ternavsky
ternavsky / Inflect.php
Last active August 28, 2015 12:53 — forked from tbrianjones/Inflect.php
A PHP Class for converting English words between Singular and Plural.
<?php
// original source: http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/
/*
The MIT License (MIT)
Copyright (c) 2015
Permission is hereby granted, free of charge, to any person obtaining a copy
@ternavsky
ternavsky / queue-ensure.php
Last active August 26, 2016 12:08 — forked from mauris/queue-ensure.php
Laravel Artisan Queue Ensurer - Set a cron job to run this file periodically to ensure that Laravel's queue is processing all the time. If the queue listener was down, it would restart it!
<?php
function runCommand ()
{
$command = 'php artisan queue:listen > /dev/null & echo $!';
$number = exec($command);
file_put_contents(__DIR__ . '/queue.pid', $number);
}
if (file_exists(__DIR__ . '/queue.pid')) {
@ternavsky
ternavsky / CountryCodes.php
Created December 8, 2015 09:27
Class allows to get country name by 2 letters code (getNameByCode) and code by name (getCodeByName). You can add several names for country separated by "|" (ex. United States|USA) in country array. getNameByCode will return first variant. getCodeByName will check all name variants in case-insensetive manner and return code if found.
<?php
class CountryCodes
{
private static $codes = array (
'AF' => 'Afghanistan',
'AX' => 'Åland Islands',
'AL' => 'Albania',
'DZ' => 'Algeria',
'AS' => 'American Samoa',
@ternavsky
ternavsky / atom-config
Last active September 16, 2016 05:51
My Atom Configuration by http://atom.io/packages/sync-settings
test
@ternavsky
ternavsky / gist:36894cb0babd29c07e30590430c3120a
Created January 15, 2018 07:46
OctoberCMS file permissions production setup
#!/bin/bash
sudo chown -R www-data:www-data *
sudo find . -type d -exec chmod 755 {} \;
sudo find . -type f -exec chmod 664 {} \;
sudo chmod -R g+s storage themes
sudo chmod -R ug+rwx storage themes
@ternavsky
ternavsky / ApiController.php
Last active June 25, 2018 14:40
Base class for Laravel Api controller
<?php
use Backend\Classes\Controller;
use Illuminate\Http\Response;
class ApiController extends Controller {
protected $statusCode = Response::HTTP_OK;
public function setStatusCode($code)
@ternavsky
ternavsky / .php_cs
Created June 27, 2018 07:11
Visual Studio php-cs-fixer config. Put it somewhere and set path in php-cs-fixer extension settings.
<?php
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'combine_consecutive_unsets' => true,
'method_separation' => true,
'no_multiline_whitespace_before_semicolons' => true,
'single_quote' => true,