How can create a plugin in elgg

I would like to share some basics about plugin development in elgg . 

In elgg all the plugins will be in mod directory. You can drop your plugin in this directory.

First you need a name for your plugin.The names of the plugins in an installation must be unique. 


you need a ‘start.php’ file for each plugin which to describe the common functions for a plugin.This file can be considered as the core controller of all plugins .The start.php must have an init function which initializes the plugin . 

You have to register this function to elgg system and have to make sure the initialisation function is called on initialisation. 

The following is a sample if the init functuion is yourplugin_init() 

register_elgg_event_handler('init','system',’yourplugin _init'); 


In common the elgg plugin will have following folders 

actions : Commonly all action(normally form actions) files are saved in this folder. 

And you can register all the actions involved in a plugin with in its start.php file 

The function is as follows .

register_action($action, $public = false, $filename = "", $admin_only = false) 

For example 

register_action('blog/add',false,$CONFIG->pluginspath . "blog/actions/add.php"); 

the parameters of the register_action indicates

action : The name of the action (here "blog/add")

second parameter is a Boolean parameter.which indicates whether the action is public or not that is whether this action be accessed by people not logged into the system or not.In above example false indicates that it is not a public action.

Next is filename that is where the action is located here the action is located in blog/actions/add.php , $CONFIG->pluginspath gives the path to plugin s that is the full path to mod directory of your elgg installation .

And if it is required you can register the action as admin action .you have to set the fourth parameter as true for this by default it in false.


Views : In common all html files will be in this folder .You can call these views in the plugin using the function

elgg_view(‘viewname’,$array_of params) 

You can override existing views also.The view lodes based on the plugin priority.

You can extend views using the function extend_view('original view ','your view'); 


The view folder contains a default folder which holds all the views of plugin .For the easiness .For the easiness you can use subfolders in the default folder.

For example if you have a plugin named blog and it have a view called create.php in the path views /defaults/blogs/create.php you can call this view like ‘blogs/create ‘ .That is you can call the view as follows elgg_view(‘blogs/create’) 

Language file : 

This file contains the language translations for the plugin.

Creating Friendly URLS:

You can create nice URLs for your plugin using page handler function.for doing this you have to define and register a page handler in `start.php`, 


function handlerfunction($page) {

switch ($page[0])

{

case 'index':

Your code for the index view

break;

case 'your_view':

include(‘yourview’);

break; 

}

} 

register_page_handler(handler, 'handler_function’);

in this function the first argument handler is your entity type, the next argument is the your plugin function handling the request which will get passed as an array in `$page` this carries each url segments. You can check for a location and can create the cases corresponding to that.



User settings: 

Plugins might have some settings , You can create the view in the file settings.phpand you can retrieve the settings by the following function

get_plugin_setting($name, $plugin_name );

and you can set values using 

set_plugin_setting($name, $value, $plugin_name);function 

You can create widgets on your plugins .The widget might be in the folder views/default/widgets

The widgets folder may have two files one is view.php that is view of widget and the other is eidt.php that is the page for setting the widget display options like number of items to be displayed etc. 

You can initialize the widget in the init function using the following function 

add_widget_type('widgettitle',$name_of_widget,$desc_of_widget) .

simple explode program in php

<?php
//explode string into array
$str=" Test Explode function in php ";
print_r (explode(" ",$str));

?>

it will be Output like:

Array ( [0] => [1] => Test [2] => Explode [3] => function [4] => in [5] => php [6] => )

simple example php function

<?php
//simple example php function
function welcome()
{
echo "Welcome to Function";
}
welcome();
?>

how can create notepad file in php

<?php
//how can create notepad file in php
$t= fopen("testfiles.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($t, $txt);
$txt = "Jane Doe\n";
fwrite($t, $txt);
fclose($t);

?>

simple uploading script in php

<?php
//simple uploading script in php
//echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "File Name ".$_FILES["file"]["name"] . "<br>";
echo "File Type ".$_FILES["file"]["type"] . "<br>";
echo "File Temp ".$_FILES["file"]["tmp_name"] . "<br>";
echo "File Size ".$_FILES["file"]["size"]."<br>";
move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
?>
<html>
<title>Simple file uploading in php</title>
<body>
<form method="post" action="fileuploading.php" enctype="multipart/form-data" >
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

While loop example in php

<?php
//While loop example in php(print 1 to 10)
$x=1;
while($x<=10)
{
echo $x;
$x++;
}
?>

switch case example in php

<?php
//switch case example in php
$day="Sunday";
switch($day)
{
  case "Monday":
   echo "Monday";
  break;
  case "Tuesday":
   echo "Tuesday";
  break;
  case "Wednesday":
   echo "Wednesday";
  break;
  case "Thurday":
   echo "Thurday";
  break;
  case "Friday":
   echo "Friday";
  break;
  case "Saturday":
   echo "Saturday";
  break;
  default:
  echo "Sunday";
}
?>

what is differences between PHP4 and PHP5

PHP4 and PHP5:
  • PHP5 removed register_globals, magic quotes, and safe mode. This was due to the fact that register_globals had opened security holes by intentionally allowing runtime data injection and the use of magic quotes had an unpredictable nature.
  • PHP4 was powered by Zend Engine 1.0, while PHP5 was powered by Zend Engine II.
  • PHP5 replaced magic quotes with the addslashes() function in order to escape characters.
  • PHP4 is more of a procedure language while PHP5 is object oriented.
  • In PHP5 one can declare a class as Abstract.
  • PHP5 incorporates static methods and properties.
  • PHP5 introduces a special function called __autoload()
  • PHP5 allows one to declare a class or method as Final
  • PHP5 introduces a number of magic methods, such as __call, __get, __set and __toString
  • In PHP5, there are 3 levels of visibilities: Public, private and protected.
  • PHP5 introduced exceptions.
  • In PHP4, everything was passed by value, including objects. Whereas in PHP5, all objects are passed by reference.
  • PHP5 introduces interfaces. All the methods defined in an interface must be public.
  • PHP5 introduces new error level defined as ‘E_STRICT’
  • PHP5 introduces new default extensions such as SimpleXML, DOM and XSL, PDO, and Hash.
  • PHP5 introduces new functions.
  • PHP5 introduces some new reserved keywords.
  • PHP5 includes additional OOP concepts than php4, like access specifiers , inheritance etc.
  • PHP5 includes improved support of current content management systems.
  • PHP5 includes reduced consumption of RAM.
  • PHP5 introduces increased security against exploitation of vulnerabilities in PHP scripts.
  • PHP5 introduces easier programming through new functions and extensions.
  • PHP5 introduces a new MySQL extension named MySQLi for developers using MySQL 4.1 and later.
  • In PHP5, SQLite has been bundled with PHP.
  • PHP5 introduces a brand new built-in SOAP extension for interoperability with Web Services.
  • PHP5 introduces a new SimpleXML extension for easily accessing and manipulating XML as PHP objects. It can also interface with the DOM extension and vice-versa.
  • In PHP5, streams have been greatly improved, including the ability to access low-level socket operations on streams.

What is the difference between echo and print statement in PHP

What is the difference between echo and print statement in PHP?

Multiple expressions can be given in echo statement, where as print cannot take multiple expressions. 
Echo does not have a return value, where as print returns a value indicating successful execution. 
Echo is faster when compared with print.

Create table in mysql

<?php
create table user(id int(10) NOT NULL AUTO_INCREMENT,username varchar(100) not null,password varchar(100),primary key(id));
?>

mysql db connection script php

<?php

//Mysql connection
$con=mysql_connect("localhost","root","");

if (mysqli_connect_errno()) {
  echo " Failed to connect to MySQL: " . mysqli_connect_error();
}

//Select particular db
mysql_select_db("test",$con);
?>

Ternary operator example in php

The Ternary operator is like if-else statement, Ternary operator is made for two different actions, one if the condition is true, the other one if it's false but with nested Ternary operators we can use it like an if-elseif.

Syntax

(condition) ? TRUE : FALSE

<?php
$i=0;
echo $output=($i==0)? "TRUE" : "FALSE";
?>

php heredoc simple example

<?php
$a=" Test Php Heredoc Syntax ";

print <<< EOD
{$a}
EOD;
?>