Friday, August 21, 2015

Install GNOME on Ubuntu 14.04

Unity slows down PC and it's very difficult to adjust to new environment. That's why I install GNOME desktop:

sudo apt-get install gnome-session-fallback

Restart PC and on login screen choose GNOME metacity session You should see desktop like this:



Move close, minimize, maximize buttons to the right

gsettings set org.gnome.desktop.wm.preferences button-layout 
':minimize,maximize,close'

Good old and familiar interface is back.

Wednesday, August 19, 2015

Git Octopus Merge

alfa and beta branches are checked out from master branch. In branch beta a file named beta.txt was created and committed. The listing shows creating and committing alfa.txt and octopus merging.

$ git checkout alfa 
Switched to branch 'alfa' 

josh@SMARTBOX ~/Documents/git/developer (alfa) 
$ vim alfa.txt 

josh@SMARTBOX ~/Documents/git/developer (alfa) 
$ git add . 

josh@SMARTBOX ~/Documents/git/developer (alfa) 
$ git commit -m 'added alfa.txt' 
[alfa d30de0e] added alfa.txt 
1 file changed, 1 insertion(+) 
create mode 100644 alfa.txt 

josh@SMARTBOX ~/Documents/git/developer (alfa) 
$ git checkout master 
Switched to branch 'master' 
Your branch is up-to-date with 'origin/master'. 

josh@SMARTBOX ~/Documents/git/developer (master) 
$ git merge alfa beta 
Fast-forwarding to: alfa 
Trying simple merge with beta 
Merge made by the 'octopus' strategy. 
alfa.txt | 1 + 
beta.txt | 1 + 
2 files changed, 2 insertions(+) 
create mode 100644 alfa.txt 
create mode 100644 beta.txt

Files alfa.txt and beta.txt that were created in their branches are now in master branch.

Sunday, July 5, 2015

PHP Dependancy Injection

This two scripts show Motor class as dependency that is injected in Car class.

<?php
class Motor {
    public function startEngine(){
        echo 'engine has started';
    }
}

class Car {
    public function __construct(Motor $m){
        $m -> startEngine();
    }
}

$a = new Car(new Motor());
?>

and second example

<?php
class Motor {
    public function __construct(){
        echo 'engine has started';
    }
}

class Car {
    public function __construct(){
        $m = new Motor();
    }
}

$a = new Car();
?>

The point is that instancing of dependent class is not done in client but in consumer class. 

Sunday, June 28, 2015

Expand Partition, Split VMDK to Multiple Files on VirtualBox

Expand Partition

C:\Windows\system32>cd C:/Program Files/Oracle

C:\Program Files\Oracle>cd VirtualBox

C:\Program Files\Oracle\VirtualBox>VBoxManage modifyhd "c:\Users\user_name\VirtualBox VMs\Windows7\Windows7-disk1.vmdk" --resize 20000
0%...
Progress state: VBOX_E_NOT_SUPPORTED
VBoxManage.exe: error: Resize hard disk operation for this format is not impleme
nted yet!

C:\Program Files\Oracle\VirtualBox>VBoxManage clonehd "c:\Users\user_name\VirtualBox VMs\Windows7\Windows7-disk1.vmdk" "c:\Users\user_name\VirtualBox VMs\Windows7\Windows7-disk1.vdi" --format VDI --variant standard
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: fa9fd259-fe7b-4a84-90c2-e7e0a516c
5ad

C:\Program Files\Oracle\VirtualBox>VBoxManage modifyhd "c:\Users\user_name\VirtualBox VMs\Windows7\Windows7-disk1.vdi" --resize 20000
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

C:\Program Files\Oracle\VirtualBox>


Now you can expand partition in gparted. Right click on partition -> resize

Split VMDK Volume to multiple parts


C:\Program Files\Oracle\VirtualBox>VBoxManage.exe 
clonehd "c:\Users\UserName\VirtualBox VMs\Ubuntu\Ubuntu-disk1.vmdk" 
"c:\Users\UserName\VirtualBox VMs\Ubuntu\Ubuntu-frags.vmdk" 
--format VMDK 
--variant Split2G
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'VMDK'. UUID: bc391000

Saturday, February 21, 2015

PDO - Full Snippet

This is fully working snippet for PDO Select with parameters

<?php
$username = "user_name";
$password = "password";
$hostname = "localhost";
$dataBase = "data_base_name";
$character_set = "utf8";
$id = 8;
$slug = "string";
try {
 $conn = new PDO("mysql:host=$hostname;dbname=$dataBase;charset=$character_set", $username, $password);
 $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $stmt = $conn -> prepare("SELECT * FROM table_name WHERE id = :id AND slug=:slug");
 $stmt->bindParam(":id", $id, PDO::PARAM_INT);
 $stmt->bindParam(":slug", $slug, PDO::PARAM_STR); 
 $stmt->execute();
 echo "Query has: ".$stmt -> rowCount() . " rows <hr>";
 while ($row = $stmt -> fetch()) {
  echo $row['id'] . " - " . $row['slug'] . "\n<br>";
 }
} catch(PDOException $e) {
 echo 'ERROR: ' . $e -> getMessage();
}
?>

Monday, September 29, 2014

Change WordPress Language in Theme

If using _e("Hello","textdomain"); to translate theme you need to create an es_es.po file in text editor:

msgid ""
msgstr ""
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
"Project-Id-Version: Theme Name\n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: Mike <w@w.com>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: es_ES\n"
"X-Generator: Poedit 1.6.9\n"
"X-Poedit-SourceCharset: UTF-8\n"

#: ../header.php:58
msgid "Hello"
msgstr "Hola"

Compile it with POEeditor you will get file es_es.mo. Upload both files
/wp-content/themes/{yourTheme}/languages

in functions.php add

add_action('after_setup_theme', 'my_theme_setup');
function my_theme_setup(){
    load_theme_textdomain('textdomain', get_template_directory() . '/languages');
}

In WP backend change language to Spanish. And both your theme and WP backend will be in Spanish. You can use plugin to leave backend in English.

Saturday, August 9, 2014

WordPress Pagination and Search in Backend

This an advanced WordPress tutorial about plugin that creates and searches a table in database. All that is happening in WordPress backend. This is useful when you need to see what your visitor have submitted to you in contact form.


After plugin activation populate table tablePrefix_messages with some