Nasrul Hazim Bin Mohamad

HTML: Editable Elements

Apr
24

I’m using the following:

  1. X-editable
  2. Bootstrap 2
  3. Cakephp / Any PHP Framework

Include the jQuery, Bootstrap 2 in your HTML head

<head>
    <!-- jQuery -->
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

    <!-- Bootstrap -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
    <script src="js/bootstrap.min.js"></script>

    <!-- X-Editable -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet"/>
    <script src="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap-editable/js/bootstrap-editable.min.js"></script>
</head>

The View

<a href="#" class="x-editable" data-type="text" data-pk="1">Some value 1</a>
<a href="#" class="x-editable" data-type="text" data-pk="2">Some value 2</a>

The JavaScript

$(document).ready(function() {
    $.fn.editable.defaults.mode = 'inline';

    $('.x-editable').editable({
        url: 'something/update'
    });
});

The Controller

<?php 
App::uses('AppController', 'Controller');

class Something extends AppController {

    public function update()
    {
        /* Data structure we received from x-editable
            Array
            (
                [name] => 
                [value] => Some value 1
                [pk] => 1
            )
        */
        $data = $this->request->data;
        $this->Some->read(null, $data['pk']);
        $this->Some->set('value', $data['value']);
        $this->Some->save();
        // return JSON if u need it or render a HTML (need to handle on success event)
    }
}
?>

References

  1. X-editable
  2. http://jquery.com/download/
  3. http://getbootstrap.com/2.3.2/getting-started.html

Angular X-editable – http://vitalets.github.io/angular-xeditable

SVN: Editor & Ignore List

Apr
20

Set SVN Text Editor

$ export SVN_EDITOR=joe

Add Directory to Ignore List

$ svn propset svn:ignore /path/to/ignore .

CakePHP: Upload File

Apr
18

The Model

<?php

App::uses('AppModel', 'Model');

class Picture extends AppModel {
	public $useTable = 'picture';
	public $primaryKey = 'id';
	public $name = 'Picture';
	public $order = 'Picture.id ASC';

	protected $_displayFields = array(
		'id',
		'name',
		'link',
	);
}
?>

The Database

CREATE TABLE `picture` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `link` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

The View

<?php
	echo $this->Form->create('Upload',array(
		'enctype' => 'multipart/form-data',
		'url' => array(
			'controller' => 'upload', 
			'action' => 'index'
		)
	));
	echo $this->Form->file('file');
	echo $this->Form->end(array(
		'label' => 'Submit',
		'class' => 'btn btn-success'
	));
?>

The Controller

<?php

App::uses('AppController', 'Controller');

class UploadController extends AppController { 
	public function index() {
                // in this example, we're trying to accept gif,jpeg and png

                $image = $this->request->data['Upload']['file'];

                $imageTypes = array("image/gif", "image/jpeg", "image/png");

                $uploadFolder = "img" . DS . "photos" . DS;

                $uploadPath = WWW_ROOT . $uploadFolder;

                $id = null;

                if(!empty($image['name']))
                {
                        $ext = substr(strtolower(strrchr($image['name'], '.')), 1); //get the extension

                        if(in_array($image['type'], $imageTypes))
                        {
                                $date = date_create();
                                $rand = rand(100000,999999);
                                $link = 'pic_' . date_format($date, 'U') . '_' . $rand . '.' . $ext;

                                move_uploaded_file($image['tmp_name'], $uploadPath . $link);

                                $this->Picture->create();
                                $this->Picture->set(array('name' => $image['name'], 'link' => $link));
                                $this->Picture->save();
                                $id = $this->Picture->id;
                                $this->Session->setFlash('Picture uploaded!');
                        }
                        else
                        {
                                $this->Session->setFlash('Only files with extension .gif, .png and .jpeg are allowed to be upload.');
                        }
                }

	}
}
?>

Amazon Web Services ( AWS )

Apr
17
Amazon Web Services

Amazon Web Services

CakePHP: PHPMailer Component – Using GMail SMTP

Apr
12

Requirements:

  1. CakePHP 2.x
  2. PHPMailer

Download PHPMailer & extract it to APP/vendors.

Add following lines in APP/vendors/phpmailer/class.phpmail.php line 542, in ELSE statement. This to ensure we have a secured domain through SSL / TLS.

// add a variable named SMTPSecure - SSL / TLS
var $SMTPSecure

// line 542 
if(!empty($this->SMTPSecure))
{
    $secure = $this->SMTPSecure . '://';
}

Create a component called EmailComponent in APP/app/Controller/Component

<?php

App::uses('Component', 'Controller');
App::import('Vendor', 'phpmailer', array('file' => 'phpmailer'.DS.'class.phpmailer.php'));

class EmailComponent extends Component {

  public function send($to, $subject, $message) {
    $sender = "from_you@you.com"; // this will be overwritten by GMail

    $header = "X-Mailer: PHP/".phpversion() . "Return-Path: $sender";

    $mail = new PHPMailer();

    $mail->IsSMTP();
    $mail->Host = "smtp.gmail.com"; 
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "ssl";
    $mail->Port = 465;
    $mail->SMTPDebug  = 2; // turn it off in production
    $mail->Username   = "[yourGMailAccount]@gmail.com";  
    $mail->Password   = "password";
    
    $mail->From = $sender;
    $mail->FromName = "From Me";

    $mail->AddAddress($to);

    $mail->IsHTML(true);
    $mail->CreateHeader($header);

    $mail->Subject = $subject;
    $mail->Body    = nl2br($message);
    $mail->AltBody = nl2br($message);

    // return an array with two keys: error & message
    if(!$mail->Send()) {
      return array('error' => true, 'message' => 'Mailer Error: ' . $mail->ErrorInfo);
    } else {
      return array('error' => false, 'message' =>  "Message sent!");
    }
  }
}

?>

Create a controller, ExampleController.php to test out our EmailComponent

<?php

App::uses('AppController', 'Controller');

class ExampleController extends AppController {
	public $components = array('Email');
	public function index() {
		// just to test out the sending email using SMTP is OK, create a method that will be able to access from public
		$to = '[someone@you.com]';
		$subject = 'Hi buddy, i got a message for you.';
		$message = 'Nothing much. Just test out my Email Component using PHPMailer.'
		$mail = $this->Email->send($to, $subject, $message);
		$this->set('mail',$mail);
		$this->render(false);
	}
}

When you navigate to http://localhost/APP/Example, the email should send to [someone@you.com]. Please do check the mailbox. 🙂

References:

  1. http://phpmailer.worxware.com/?pg=examplebgmail
  2. http://blog.teamtreehouse.com/sending-email-with-phpmailer-and-smtp
  3. http://book.cakephp.org/2.0/en/controllers/components.html#creating-a-component

RDBMS Data Limit

Apr
01

== Microsoft SQL Server ==
Max DB Size: 524,272 TB (32 767 files * 16 TB max file size)
Max Table Size: 524,272 TB
Max Row Size: 8,060 bytes (Unlimited)
Max Columns Per Row: 30,000
Max Blob / Clob Size: 2 GB
Max Char Size: 2 GB
Max Number Size: 126 bits
Min Date Value: 0001
Max Date Value: 9999
Max Column Name Size: 128

== MySQL ==
Max DB Size: Unlimited
Max Table Size: MyISAM storage limits: 256 TB; Innodb storage limits: 64 TB
Max Row Size: 64 KB
Max Columns Per Row: 4,096
Max Blob / Clob Size: 4 GB (longtext, longblob)
Max Char Size: 64 KB (text)
Max Number Size: 64 bits
Min Date Value: 1000
Max Date Value: 9999
Max Column Name Size: 64

== PostgreSQL ==
Max DB Size: Unlimited
Max Table Size: 32TB
Max Row Size: 1.6TB
Max Columns Per Row: 250 – 1600 ( depending on type)
Max Blob / Clob Size:
– 1 GB (text, bytea)[http://grokbase.com/t/postgresql/pgsql-general/12bsww982c/large-insert-leads-to-invalid-memory-alloc] – stored inline or
– 4 TB (stored in pg_largeobject)[http://www.postgresql.org/docs/9.3/static/lo-intro.html]
Max Char Size: 1GB
Max Number Size: Unlimited
Min Date Value: −4,713
Max Date Value: 5,874,897
Max Column Name Size: 63

== Oracle ==
Max DB Size: Unlimited (4 GB * block size per tablespace)
Max Table Size: 4 GB * block size (with BIGFILE tablespace)
Max Row Size: 8 KB
Max Columns Per Row: 1,000
Max Blob / Clob Size: 128 TB
Max Char Size: 32,767 B
Max Number Size: 126 bits
Min Date Value: −4712
Max Date Value: 9999
Max Column Name Size: 30

== SQLite ==
Max DB Size: 128 TB (231 pages * 64 KB max page size)
Max Table Size: Limited by file size
Max Row Size: Limited by file size
Max Columns Per Row: 32,767
Max Blob / Clob Size: 2 GB
Max Char Size: 2 GB
Max Number Size: 64 bits
Min Date Value: No DATE type
Max Date Value: No DATE type
Max Column Name Size: Unlimited

Reference: http://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems