Rewrite Engine

Because I own the codingfriends.co.uk, but would like to point that to the www.codingfriends.com

Here is a good way to make it so that any requests to the domain name codingfriends.co.uk (rewrite condition of the host record) will redirect to www.codingfriends.com with a redirection of HTTP 301 (moved permanently).

RewriteEngine On
RewriteCond ${HTTP_HOST} !^(www)\.codingfriends\.co\.uk 
RewriteRule (.*) http://www.codingfriends.com/$1 [L,R=301]

Save that to the .htaccess root directory file. Hope that helps.

SugarCRM add a new menu item

SugarCRM is a very nice and open source CRM (Customer relationship management) system. Being open source means that you are more than welcome to add/alter your own modules to it. I am going to do some modules which add in some basic information and how-to’s. This how to is how to add to a left menu item and in this case a account main menu left menu item.

Also going to be doing a module for it, so that you can upload to different SugarCRM’s that you may have, e.g. development version and live version.

The module consists of the main manifest.php file which holds all of the main details, module name author, description etc and also the install definitions.

Here is a basic manifest.php file that has the main details and install definitions for adding a menu item to the Accounts module.

<?php
$manifest = array(
  'acceptable_sugar_flavors' => array(
          0 => 'CE',
          1 => 'PRO',
          2 => 'ENT',
          3 => 'DEV'
        ),
    'acceptable_sugar_versions' => array (
        'regex_matches' => array (
            0 => "5\.*\.*.*"
        ),
    ),
 
    'name'              => 'Accounts insert left menu addition',
    'description'       => 'This module inserts a left menu addition',
    'author'            => 'Ian Porter',
    'published_date'    => '2009/06/01',
    'version'           => '0.1',
    'type'              => 'module',
    'icon'              => '',
    'is_uninstallable'   => 1,
    'silent' => true,
);
 
$installdefs = array (
  'id' => 'AccountsLeftMenu',
    'vardefs'=> array( ),
  'custom_fields' =>  array (  ),
  'copy' =>
  array ( ),
'menu'=> array(
array('from'=> '<basepath>/Menu.php',
'to_module'=> 'Accounts', ),
),
       'beans'=> array (
                 ),
  'language' => 
  array (
  ),
);
?>

As you can see from the above code, the acceptable sugar flavors means any of the version types of sugar, development, pro etc.. and the acceptable sugar versions means which version of sugar e.g. version 4.1.2 or 5.etc. the name etc speaks for itself really.

The installdefs are what happens with the files and such when the module is installed, id is the name of the module, the one that we are focusing in on is menu, this will insert the code below into the a set menu module (this case the Accounts menu structure).

Here is the Menu.php file

<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); 
 
/* licence and you stuff */
 
global $mod_strings, $app_strings, $sugar_config, $current_user;
 
/* you need to create the DetailViewPersonal.php file to communicate with */
 
if(ACLController::checkAccess('Accounts', 'edit', true)) {
    $module_menu[]=Array("index.php?module=Accounts&action=DetailViewPersonal&return_module=Accounts&return_action=DetailView&record=".$current_user->id, "Personal View",  'Accounts');
}
?>

The ACLController will check the access level of the user for editable, ACL(Access Control Level), and if so place a new menu below called “Personal View”, the action in the module_menu array is what is called and thus you will need to have a DetialViewPersonal.php in the modules/Accounts directory, but this was just a how to, of how to insert a menu item and not the underlying code.

There is more to come!.

Method – Add Two numbers

This tutorial is using the same base code as Add two numbers, but with using the function addtwo, this takes two parameters $a and $b which are both set to 0 as the default value and returns the two values added together. The answer part of the web page uses the function to add the two numbers together.

The source code

<?php
       function addtwo($a = 0, $b = 0)
       {
              return ($a + $b);
       }
       $value1 = $_POST['value1'];
       $value2 = $_POST['value2'];
?>
<html>
       <title>PHP - Add two numbers</title>
       <body>
              <form action="addtwonumbers.php" method="post">
                     <input type="text" name="value1" value="0" />
                     <input type="text" name="value2" value="0" />
                     <input type="submit" value="Calculate values"/>
              </form>
              Answer : <?php echo addtwo($value1+$value2); ?>
       </body>
</html>

save as addtwonumbers_function.php, this program will function the same as the previous tutorial apart from the inner working will call the method. The method programming allows for one method to be called multiple times, e.g. Within the example above there could be a method that was 5 lines in length and instead of writing the same 5 lines each time, you just write a method that is called.

Add two numbers

This tutorial will add up two values that have been entered within the web page, I am going to use the web page functional aspects rather than the command line interface because php is mainly used within websites.

The web page itself is using a form tag, this form tag allows for the web page to post data back to the server, where the php will use the posts ($_POST) array ( []).

The source code is

<?php
       $value1 = $_POST['value1'];
       $value2 = $_POST['value2'];
?>
<html>
       <title>PHP - Add two numbers</title>
       <body>
              <form action="addtwonumbers.php" method="post">
                     <input type="text" name="value1" value="0" />
                     <input type="text" name="value2" value="0" />
                     <input type="submit" value="Calculate values"/>
              </form>
              Answer : <?php echo ($value1 + $value2); ?>
       </body>
</html>

if you save that as addtwonumbers.php within the php website configured directory and then open up that page via a web browser (e.g. http://localhost/addtwonumbers.php, localhost means the local pc, just like 127.0.0.1 is the local ring e.g the pc itself to talk to itself via an IP).

Just for completeness, this is the command line interface code.

<?php
 echo "Please enter value 1 : ";
 fscanf(STDIN, "%d\n", $value1); // reads number from STDIN standard input
 echo "Please enter value 2 : ";
 fscanf(STDIN, "%d\n", $value2);
 echo "Answer : " .($value1 + $value2) . "\n";
?>

Read/Write Files

This is a tutorial on how to open and write files with PHP, of course there is always different ways to accomplish the same task within programming languages.

Fopen will open up a file, the secod parameter is who to open the file, r = read, w = write.

$readin = fopen("countrys.txt", "r");

The while loop will set the variable $county equal to a line from the input file, the fscanf will scan a file with the second parameters requirements, the ‘[^\n]’ means anything that is not(^) a return character (\n).

while ($county = fscanf($readin, "%[^\n]")) {

Fwrite will output to the output file handler (the $output created with fopen), with the text in the second parameters

fwrite($output, "insert into country(place) values (\"$county[0]\");\n");

NOTE: the { } are the begin and end of a code structure, e.g. While begin; do something; end;

This is the code

<?php
       $readin = fopen("countrys.txt", "r");
       $output = fopen("sqlcountrys.txt", "w");
 
       while ($county = fscanf($readin, "%[^\n]")) {
              echo $county[0]. "\n";
              fwrite($output, "insert into country(place) values (\"$county[0]\");\n");
       }
       fclose($readin);
       fclose($output);
?>

if you save that as phpreadfile.php. and also create a countrys.txt file with what ever text you like, e.g. United Kingdom

France
Germany
United States etc.

The output of the program (php phpreadfile.php) will display each line that is read from the input file and the output file will have some other text in it, for demonstrating purposes, have done some sql code.

Hello world

This is the hello world for the php tutorial section. PHP is another Object Orientation language, to get php, if you download it from PHP.NET and you are able to use with Apache or with MS Tnternet Information Services IIS.

Once you have php installed, if you save this

<?php
       echo "Hello World!";
?>

as phphelloworld.php within the correct directory for the php engine to use the file.

Once you point your browser to the file e.g.
http://localhost/phphelloworld.php

and the output will be “Hello World!”;

Hope that helps 🙂