Skip to content

Instantly share code, notes, and snippets.

View tunecino's full-sized avatar
🏠
Working from home

Salem Ouerdani tunecino

🏠
Working from home
View GitHub Profile
const minute = 60;
const hour = minute * 60;
const day = hour * 24;
const week = day * 7;
const month = day * 30;
const year = day * 365;
/**
* Convert a date to a relative time string, such as
* "a minute ago", "in 2 hours", "yesterday", "3 months ago", etc.
@tunecino
tunecino / _yii.js
Last active December 1, 2021 01:40
/**
* use case example :
*
* Lets say we want to ask API for 50 products, with date = today and having 'abc' in their names,
* we only want to retrieve 'name' and 'sold' fields from db (Yii uses SQL SELECT for this),
* but also joined with their related shop (having shop defined as relation (see extraFields method))
*
* Code to run inside Vue component:
*
* import { Collection } from "~/scripts/yii.js";
@tunecino
tunecino / Controller.php
Last active January 29, 2019 17:04
Yii2 Auto login user in RESTful API (Bearer/OAuth 2.0)
<?php
public function beforeAction($action)
{
if ($action->id !== 'options' && $action->id !== 'ping') {
/**
* Try to auto login user if auth headers are received within request.
*/
$authHeader = Yii::$app->request->getHeaders()->get('authorization');
if ($authHeader !== null) {
if (preg_match('/^Bearer\s+(.*?)$/', $authHeader, $matches)) {
<?php
class ImageController extends \yii\rest\ActiveController
{
public $modelClass = 'app\models\Image';
public $relationAttribute = 'imageIds';
public $relations = [
'tags' => 'tag_id',
'owner' => 'owner_id'
];
public function actionRelationships($id = '')
@tunecino
tunecino / relationInfo.php
Created November 17, 2015 14:01
A helper function using yii\db\BaseActiveRecord::getRelation() to get relation type + optional check if both models are related on DB if respective ids are provided
<?php
private function relationInfo($model, $name, $related_id = false)
{
$relation = $model->getRelation($name, false);
if ($relation === null) return null;
$info = [];
$info['class'] = $relation->modelClass;
@tunecino
tunecino / designer.html
Created September 12, 2014 20:47
designer
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../topeka-elements/avatars.html">
<link rel="import" href="../core-icon/core-icon.html">
<link rel="import" href="../topeka-elements/category-images.html">
<link rel="import" href="../topeka-elements/theme.html">
<link rel="import" href="../topeka-elements/topeka-resources.html">
<link rel="import" href="../topeka-elements/topeka-category.html">
<link rel="import" href="../core-pages/core-pages.html">
<polymer-element name="my-element">