Friday 23 October 2015

Establish connection with an Amazon S3 client.using PHP

If PHP S3 client connection is not set correctly, you will get following error:
1) Error retrieving credentials from the instance profile metadata server
2) AWS HTTP error: cURL error 60: SSL certificate problem:

For S3 PHP SDK version 3, the correct connection for S3 client as follows:

 <?php
  require $_SERVER['DOCUMENT_ROOT'] . '/aws/aws-autoloader.php';
  use Aws\S3\S3Client;
  use Aws\S3\Exception\S3Exception;
  use Aws\Common\Credentials\Credentials;


//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'mykey');
if (!defined('awsSecretKey')) define('awsSecretKey', 'mysecret');


$bucket = 'defashion2u';

try {
$client = S3Client::factory(array(
    'version' => 'latest',
    'region'  => 'us-east-1',
    'credentials' => array(
    'key' => awsAccessKey,
    'secret'  => awsSecretKey
  ),
    'http'    => [
        'verify' => $_SERVER['DOCUMENT_ROOT'] . '/aws/cacert.pem'
    ]
));
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

?>


The parameter version, region, credentials and SSL cert must be set.
More info on the ca cert, here

Monday 12 October 2015

Saturday 10 October 2015

Reuse / Load a Model Anywhere in Joomla 2.5/3.x

Sometimes you may wish to reuse a certain function that resides in a model outside of your current scope. This helps you save time and reduce duplication. This can be achieved easily by adding the following codes.

Assuming you are trying to call a model "Categories" (/components/com_mycomponent/models/categories.php) belonging to com_mycomponent (this can be called from either within or outside of the com_mycomponent):

jimport('joomla.application.component.model');
JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_mycomponent/models');
$categoriesModel = JModelLegacy::getInstance( 'Categories', 'MyComponentModel' );

Now you can call its methods, example:

$categoriesModel->getCategories();

Friday 9 October 2015

PHP Essential Quick Reference

PHP essentials

25 Essential PHP Functions

PHP - Inserts HTML line breaks before all newlines in a string

(PHP 4, PHP 5)
nl2br — Inserts HTML line breaks before all newlines in a string

Description string nl2br ( string $string [, bool $is_xhtml = true ] )
Returns string with '<br />' or '<br>' inserted before all newlines (\r\n, \n\r, \n and \r).

Parameters
string
The input string.

is_xhtml
Whether to use XHTML compatible line breaks or not.

Return Values
Returns the altered string.

Examples
Example #1 Using nl2br()
<?phpecho nl2br("foo isn't\n bar");?>

The above example will output:
foo isn't<br />
 bar

Example #2 Generating valid HTML markup using the is_xhtml parameter
<?phpecho nl2br("Welcome\r\nThis is my HTML document"false);?>
The above example will output:

Welcome<br>
This is my HTML document


Multiple controllers in Joomla MVC component

Joomla basic tutorial and understand that the main controller is located under components/com_component/controller.php & other file is components/com_helloworld/helloworld.php which is pointing to controller.php file to execute the request. You create new controller folder and put all controllers in that folder


The main controller is typically used to do what you mention in your comment (render a view based on the view parameter, usually specified in the URL). In controller.php, the function that does this is called display(). So the following URL:

http://example.com/index.php?option=com_mycomponent&view=test

Will result in calling the display() function in controller.php and loads the view test in /views/test.

Subcontrollers, in the controllers folder, are generally used for CRUD tasks, but can be called easily by using a task URL parameter. For example:

http://example.com/index.php?option=com_mycomponent&task=test.process

Will result in calling the process() function in /controllers/test.php - notice the task parameter is <controllerName>.<functionName>

How to switch the ‘action’ field in an HTML form dynamically

It is often required that you need to send the data in the same form to different server-side scripts depending one certain criteria. JavaScript allows you to modify the action field of an HTML form dynamically.
 

Multiple submit buttons

It is possible to have more than one submit button in a form. The example below shows how to switch the action field based on the submit button pressed.
 
<script type="text/javascript">
function OnSubmitForm()
{
  if(document.pressed == 'Insert')
  {
   document.myform.action ="insert.html";
  }
  else
  if(document.pressed == 'Update')
  {
    document.myform.action ="update.html";
  }
  return true;
}
</script>

<form name="myform" onsubmit="return onsubmitform();">
 
<input type="submit" name="operation" onclick="document.pressed=this.value" value="insert" />
 
<input type="submit" name="operation" onclick="document.pressed=this.value" value="update" />
 
</form>
 
 

Based on a radio button

The example below shows a case where the action field is switched based on the selection in the radio buttons.


<script type="text/javascript">
function OnSubmitForm()
{
  if(document.myform.operation[0].checked == true)
  {
    document.myform.action ="insert.html";
  }
  else
  if(document.myform.operation[1].checked == true)
  {
    document.myform.action ="update.html";
  }
  return true;
}
</script>
<form name="myform" onsubmit="return OnSubmitForm();">
   name: <input type="text" name="name"><br>
   email: <input type="text" name="email"><br>
   <input type="radio" name="operation" value="1" checked>insert
   <input type="radio" name="operation" value="2">update
   <p>
   <input type="submit" name="submit" value="save">
   </p>
</form>

Joomla Useful reference

Joomla Constant
https://docs.joomla.org/Constants


Component
Developing Joomla 3.x component