YII PHP Framework Hacks
Styling Radio Buttons
http://www.yiiframework.com/wiki/110/styling-radio-buttons/
Removing the Manage Buttons
array(
'type'=>'raw',
'value'=>'CHtml::link($data->id,array("update", "id"=>$data->id))', 'header'=>'Edit'
),
Handling related models in YII forms
Many AWBs can be carried in one Trip. One AWB can be carried across Many Trips. We will use MySQL for the database. Below is the schema. tbl_tripAWB is the junction table.
CREATE TABLE tbl_trip(
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
truckId TINYINT UNSIGNED NOT NULL,
departureDate DATETIME NOT NULL,
tripOrigin MEDIUMINT UNSIGNED NOT NULL,
tripDestination MEDIUMINT UNSIGNED NOT NULL,
arrivalDate DATETIME NOT NULL,
tripFor BOOLEAN NOT NULL DEFAULT 0,
tripStatus VARCHAR(125),
trip TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
INDEX (truckId),
INDEX (tripOrigin),
INDEX (tripDestination)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE tbl_awb(
id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
awbPrefix VARCHAR(3) NOT NULL,
awbNumber VARCHAR(8) NOT NULL,
awbAirport MEDIUMINT UNSIGNED NOT NULL,
awbCarrier TINYINT UNSIGNED NOT NULL,
awbPieces TINYINT UNSIGNED NOT NULL,
awbWeight MEDIUMINT UNSIGNED NOT NULL,
awb TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
INDEX (awbAirport),
INDEX (awbCarrier)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE tbl_tripAwb(
id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
trip INTEGER UNSIGNED NOT NULL,
awb INTEGER UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE tbl_tripAwb
ADD CONSTRAINT FOREIGN KEY (trip) REFERENCES tbl_trip (id) ON UPDATE CASCADE,
ADD CONSTRAINT FOREIGN KEY (awb) REFERENCES tbl_awb (id) ON UPDATE CASCADE;
Update Trip form to display the list box with multi select containing AWBs.
dropDownList($model, ‘tripAwbs’, CHtml::listData(Awb::model()->findAll(), ‘id’, ‘awbNumber’, ‘awbPrefix’),array(‘multiple’=>’multiple’, ‘size’=>5)
); ?>
error($model,’tripAwbs’); ?>
Yii automatically created the below relationship
'tripAwbs' => array(self::HAS_MANY, 'TripAwb', 'trip'),
In order for the creation of a new Trip to work, you’ll need to update the Controller to handle the tripAwbs. Within the actionCreate() method (of TripController), after the Trip has been saved, loop through each AWB and add that record to tbl_tripAwb. The beginning of actionCreate() would look like:
if(isset($_POST['Trip']))
{
$model->attributes=$_POST['Trip'];
if($model->save()) {
foreach ($_POST['Trip']['tripAwbs'] as $awb) {
$tripAwb = new TripAwb;
$tripAwb->trip = $model->id;
$tripAwb->awb = $awb;
if (!$tripAwb->save()) print_r($tripAwb->errors);
// $this->redirect(array('view','id'=>$model->id));
}}
}
The form has already been created and populated, but to get it to indicate existing selections, we need to pass along the tbl_tripAwb values. You might think that you can just fetch all the tbl_tripAwb records where id equals the Model’s id (in other words, perform a with(‘tripAwb’)-> retrieval), but you can’t. For the drop-down menu in the form to preselect the right values, the form needs to access an array of values, not an array of objects. To achieve that, add some code to the loadModel() method of the TripController. This method is called for the update, delete, and view actions and just returns a single Model. The Model is loaded using:
public function loadModel($id)
{
$model=Trip::model()->findByPk($id);
// added for update by karthick
$criteria=new CDbCriteria;
$criteria->condition=’trip=:trip’;
$criteria->select = ‘awb’;
$criteria->params=array(‘:trip’=>$_GET['id']);
$tripAwbs = TripAwb::model()->findAll($criteria);
$Awbs = array();
foreach ($tripAwbs as $awb) {
$Awbs[] = $awb->awb;
}
$model->tripAwbs = $Awbs;
if($model===null)
throw new CHttpException(404,’The requested page does not exist.’);
return $model;
}
Then update the tbl_tripAwb when the form is submitted (and the post is updated). The easiest way to handle all possibilities is to clear out the existing values (for this trip) in tbl_tripAWb, and then add them new. To do that, in actionUpdate() of the TripController, you would have:
if(isset($_POST['Trip']))
{
$model->attributes=$_POST['Trip'];
if($model->save()) {
$criteria=new CDbCriteria;
$criteria->condition='trip=:trip';
$criteria->params=array(':trip'=>$model->id);
tripAwb::model()->deleteAll($criteria);
// repeat of for each loop found in actionCreate()
foreach ($_POST['Trip']['tripAwbs'] as $awb) {
$tripAwb = new TripAwb;
$tripAwb->trip = $model->id;
$tripAwb->awb = $awb;
if (!$tripAwb->save()) print_r($tripAwb->errors);
}
$this->redirect(array(‘view’,'id’=>$model->id));
}
}
First Indicator and Trading System for Meta Trader Platform
After checking lot of Indicators from Forum at www.http://forexfactory.com. I wanted to create one nice Indicator as a pre-cursor for my own EA. It will also help me to do scalping on 1 Min chart, until such time.
Indicator helps me to pay less attention is price action and helps me to just look at the chart only when required. This reduces lot of strain.
//+——————————————————————+
//| Infinite.mq4 |
//| Copyright © 2011, pannet1 |
//| pannet1@yahoo.com |
//+——————————————————————+
#property copyright “Copyright © 2011, pannet1″
#property link “http://pannet1.wordpress.com”
//—-
#property indicator_separate_window
#property indicator_buffers 8
#property indicator_minimum 0
#property indicator_maximum 2
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_color3 PaleGreen
#property indicator_color4 LightSalmon
//—-
extern int MA = 20;
extern int MA_Type = 1;
extern int StDev = 2;
extern int Shift = 0;
//—- indicator buffers
double CrossUp[];
double CrossDown[];
double MAUp[];
double MADown[];
double TrendUp[];
double TrendDown[];
double Up[];
double Down[];
//+——————————————————————+
//| Custom indicator initialization function |
//+——————————————————————+
int init()
{
//—- indicator line
SetIndexStyle (0, DRAW_ARROW, 0,1, indicator_color2);
SetIndexArrow (0, 163);
SetIndexBuffer(0, CrossUp);
SetIndexStyle (1, DRAW_ARROW, 0,1, indicator_color1);
SetIndexArrow (1, 163);
SetIndexBuffer(1, CrossDown);
SetIndexStyle (2, DRAW_ARROW,0,1, indicator_color1);
SetIndexArrow (2, 241);
SetIndexBuffer(2, MAUp);
SetIndexStyle (3, DRAW_ARROW,0,1, indicator_color2);
SetIndexArrow (3, 242);
SetIndexBuffer(3, MADown);
SetIndexStyle (4, DRAW_ARROW, 0,1, indicator_color3);
SetIndexArrow (4, 110);
SetIndexBuffer(4, TrendUp);
SetIndexStyle (5, DRAW_ARROW, 0,1, indicator_color4);
SetIndexArrow (5, 110);
SetIndexBuffer(5, TrendDown);
SetIndexStyle (6, DRAW_ARROW, 0,1, indicator_color1);
SetIndexArrow (6, 236);
SetIndexBuffer(6, Up);
SetIndexStyle (7, DRAW_ARROW, 0,1, indicator_color2);
SetIndexArrow (7, 238);
SetIndexBuffer(7, Down);
//—- name for DataWindow and indicator subwindow label
IndicatorShortName(“Infinite”);
return(0);
}
//+——————————————————————+
//| |
//+——————————————————————+
int deinit()
{
//—-
return(0);
}
//+——————————————————————+
//| Infinite V1 |
//+——————————————————————+
int start()
{
int counted_bars = IndicatorCounted();
// int i;
int limit;
//—- check for possible errors
if(counted_bars < 0)
return(-1);
//—- last counted bar will be recounted
if(counted_bars > 0)
counted_bars–;
limit = Bars – counted_bars;
//—-
if(limit <= MA)
limit=1;
//—–
// Begin Main Loop Function
for(int i=0; i<limit; i++)
{
double MAnow = iMA(NULL, 0, MA, 0, MA_Type,
PRICE_CLOSE, i);
double MAprevious = iMA(NULL, 0, MA, 0, MA_Type,
PRICE_CLOSE, i + 1);
double MAafter = iMA(NULL, 0, MA, 0, MA_Type,
PRICE_CLOSE, i – 1);
double MAswap = 0;
//—-
double BB_Upper = iBands(NULL, 0, MA, StDev, Shift,
PRICE_CLOSE, MODE_UPPER, i);
double BB_Lower = iBands(NULL, 0, MA, StDev, Shift,
PRICE_CLOSE, MODE_LOWER, i);
//- Correct error in MAafter if bar (0) is latest
if (i==0) { // if we are in present bar
MAswap = MAnow;
if (MAswap > 0)
{
MAafter = MAnow;
MAnow = MAprevious;
MAprevious = MAswap;
}
}
//—- Determine Bar type
double LOB = Low[i];
double HOB = High[i];
double OOB = Open[i];
double COB = Close[i];
bool BULLISH = false;
bool BEARISH = false;
bool DOJI = false;
if ( OOB<COB )
BULLISH = true;
if ( OOB>COB )
BEARISH = true;
if ( (OOB-COB) == 0 )
DOJI = true;
//— end determine Bar type
//—– Determine dynamic Take Profit or Stop Loss
if(
(OOB < MAnow && COB > MAnow)
)
CrossDown[i] = 0.25;
if(
(OOB > MAnow && COB < MAnow)
)
CrossUp[i] = 1.25;
//——–
//– Signal Trend Change
if(
(MAnow < MAprevious)
&&(MAnow < MAafter)
)
MAUp[i] = 0.75;
{
Print(“inside function MAUp, at Bar “, i);
Print(“Previous: “, MAprevious, ” now: “, MAnow, ” after:”, MAafter);
}
if(
(MAnow > MAprevious)
&& (MAnow > MAafter)
)
MADown[i] = 0.75;
//– end Trend Change
//– Determine present trend
if(
(MAnow > MAprevious)
&&(MAnow < MAafter)
)
TrendUp[i] = 0.75;
if(
(MAnow < MAprevious)
&&(MAnow > MAafter)
)
TrendDown[i] = 0.75;
//– end Trend Change
//— Signal Overbought or Oversold with Bollinger Band
if ( (HOB > BB_Lower && LOB < BB_Lower && HOB < MAnow) &&
(BULLISH==true)
)
Up[i] = 0.25;
if ( (HOB > BB_Upper && LOB < BB_Upper && LOB > MAnow) &&
(BEARISH==true)
)
Down[i] = 1.25;
//———-
}
return(0);
}
//+——————————————————————+
How to Trade
Get Ready when the Slanting Arrows are up or down.
Buy (Enter from Bottom Row arrow pointing upwards)
When a Sell Stop is encountered or
When the trend is changed (Moving average is UP)
Exit when a Buy Stop is encountered.
Clue: After entering focus attention in the top row.
Sell (Enter from Top Row arrow pointing downwards)
When a Buy Stop is encountered or
When the trend is changed (Moving average is Down)
Exit when a Sell Stop is encountered
Clue: After entering focus attention in the Bottom row.
Edit: I just shifted the MaAfter value and every other MAs to the left by ONE (1) and everything works fine.
Also made few changes to have Stops and “Take Profit to one Row (Top row for Buy and Bottom row for Sell).
Setting up an Extended Desktop for Watching Charts and Indicators
My friend wanted to use more than one monitor for trading purpose (watching charts and indicators) from within Meta Trader. Actually the solution already exists if you have a laptop with Intel Chipset and an out for external Monitor.
There could be similar solution for other Chipset manufacturers too (Nvidea etc).
Select “extended desktop” from operating mode. That’s it. Now you can move the windows spanning from your laptop to the LCD Monitor.
I have a Samsung N150 Plus netbook and a Samsung LCD TV in which I personally use this setup.
Nokia N900
My Nokia N900 suddenly started losing battery charge fast. I have updated the latest beta repositories and installed as much as new (useful) applications I can. The situation became so bad, that I cannot use my phone for even few calls. I was charging my phone more rather than using it. So, I decided to do something about it.
I then flashed my phone from one of the US kernels, i downloaded before. It was successful but I was not getting any network at all and hence could not make any calls. After pulling my hair for several days and going through abundant resources from the net, I downloaded the latest US kernel and did the flashing once again. This time I was successful.
Guess, what was the problem. Never downgrade from the previous version of kernel or whatever on the N900. The same kernel worked for me before, now it didn’t because. I have updated to the latest kernel after installing it and hence applying the old kernel do not work
Customizing Gedit as Web Developer’s IDE
These tips I followed from Micah Carrick’s blog
1. get the tag for gedit
wget http://www.micahcarrick.com/files/gedit_webdev_tags-0.1.tar.gz
tar -xzf gedit_webdev_tags-0.1.tar.gz
sudo mkdir /usr/share/gedit-2/plugins/taglist
sudo cp *.tags /usr/share/gedit-2/plugins/taglist/
rm -rf *.gz *.tags
2. get current version of PHP_Beautifier and other useful stuffs
sudo apt-get install csstidy linkchecker
sudo pear install PHP_Beautifier-0.1.14
3. use external tools plugin
for beautifying PHP (add command)
php_beautifier -s4 -l “ArrayNested() IndentStyles(style=bsd) NewLines(before=if:switch:while:for:foreach:function:T_CLASS:return:break,after=T_COMMENT)”
check PHP code for parse error (add command)
php -l
PHP function look up in browser firefox (add command)
xargs -I '{}' firefox \
'http://www.php.net/search.php?lang=en&show=quickref&pattern={}'
“Not working for me in Jaunty Jackalope”
4. get ftp support
run gconf-editor add ftp to the list at
/apps/gedit-2/preferences/editor/save/writable_vfs_schemes/
“Already updated in Jaunty Jackalope”
5.get symbol browser plugin
cd ~/.gnome2/gedit/
wget http://sourceforge.net/projects/symbol-browser/files/symbol-browser-bin/gedit-symbol-browser-plugin-bin-ubuntu-i386-0.1/gedit-symbol-browser-plugin-bin-ubuntu-i386-0.1.tar.gz
tar -xzf gedit-symbol-browser-plugin-bin-ubuntu-i386-0.1.tar.gz
rm -rf *.gz
sudo apt-get install libgnomeprintui2.2-0 ctags
Now enable from menu Edit->Preferences-Plugins
6. get tidy plugin
cd plugins
wget http://www.eng.tau.ac.il/~atavory/gedit-plugins/html-tidy/html-tidy-gedit-plugin.tar.gz
tar -xzf html-tidy-gedit-plugin.tar.gz
sudo apt-get install tidy
7. get PHP autocomplete
wget http://downloads.sourceforge.net/project/gedit-autocomp/gedit-autocomp/gedit-autocomplete-0.9.6/gedit-autocomplete-0.9.6.tar.gz*
tar -xzf gedit-autocomplete-0.9.6.tar.gz
cd gedit-autocomplete-0.9.6
mv *.* ../.
cd ..
rm -rf gedit-autocomplete
8. get python plugin for posting code into snipplr.com remotely.
wget http://geditsnipplr.googlecode.com/files/snipplrpy-0.4.tar.gz
tar -xzf snipplrpy-0.4.tar.gz
cd snipplrpy-0.4/
sudo python setup.py install
cd ..
wget http://geditsnipplr.googlecode.com/files/gedit_snipplr_plugin-1.0.tar.gz
tar -xzf gedit_snipplr_plugin-1.0.tar.gz
cd plugin
mv snipplr* ../.
cd ..
rm -rf plugin
if you have not signed up already, sign up with snipplr.com and get the api key from the site.
9. cleanup
rm -rf *.gz

First Step before Creating a Webapp with YII
- Install LAMP Server
- Install PHPunit
- Install Selenium
- Running the Functional Tests
- Configure Gedit for PHP
- Install Netbeans
- SVN update the latest YII
- install the webapp in the webroot
- Testing the DB Connection
- Configuring YII to use MySQL
% sudo pear channel-discover pear.phpunit.de
% sudo pear install phpunit/PHPUnit
Download Selenium Remote Control (Selenium RC) zip file from http://seleniumhq.org/download/.
Run Server from shell
% java -jar selenium-server.jar
Before we can run this functional test, we need to make a couple of configuration changes to our application. First we need to alter protected/tests/WebTestCase. php to properly define our test URL that Selenium will attempt to open when it runs
the tests. Open up that file and make sure the TEST_BASE_URL definition matches the URL to your webapp
define(‘TEST_BASE_URL’,'http://localhost/webappp/index-test.php/’);
The file protected/tests/phpunit.xml houses some configuration settings for Selenium Server. It is configured to use IE as the primary browser. We can remove the following highlighted line of code to ensure only Firefox will be used when running Selenium:
name=”Internet Explorer” browser=”*iexplore”
Now, as long as you have installed PHPUnitand have ensured that Selenium Server is running, and then we can navigate to our tests folder at the command prompt and run this functional test:
% cd protected/tests/
% phpunit functional/SiteTest.php
What should happen is that you will see your browser being automatically invoked, as the Selenium Server platform is using the browser to access the end-user functionality of the site that we configured in the WebTestCase.php file. As it runs through the test methods, it actually automates the behavior of a real user of the site.
we know we add our unit tests under protected/tests/unit/. Let’s create a simple database connectivity test file under this folder called DbTest.php. Create this new file with the following contents:
class DbTest extends CTestCase
{
public function testConnection()
{
$this->assertTrue(true);
}
}
Here we have added a fairly trivial test. The assertTrue() method, which is part of phpUnit, is an assertion that will pass if the argument passed to it is true, and it will fail if it is false. So, in this case, it will pass if true is true. Of course it is, so this test will pass. We are doing this to make sure our new application is working as expected for testing. Navigate to the tests folder and execute this new test:
% cd /WebRoot/trackstar/protected/tests
% phpunit unit/DbTest.php
Change the trivial assertEquals(true) statement in the testConnection() test method to:
$this->assertNotEquals(NULL, Yii::app()->db);
And rerun the test:
To customize our application configuration, we normally provide a configuration file
to initialize its property values when the application instance is being created. The
main application configuration file is located in /protected/config/main.php.

Configuring GII
Before we can start using Gii, we have to configure it for use within our application.
At this point you probably know enough to guess we would do that in our main
application configuration file, protected/config/main.php. This is correct. To
configure Gii for use, open this file and add the following highlighted code to the
returned array:
return array(
‘basePath’=>dirname(__FILE__).DIRECTORY_SEPARATOR.’..’,
‘name’=>’My Web Application’,
// preloading ‘log’ component
‘preload’=>array(‘log’),
// autoloading model and component classes
‘import’=>array(
‘application.models.*’,
‘application.components.*’,
),
‘modules’=>array(
‘gii’=>array(
‘class’=>’system.gii.GiiModule’,
‘password’=>’[add_your_password_here]‘,
),
),

How to Install Apps from GetDeb
- Install the getdeb package.
- Or configure the repository manually:
- Go to System-Administration-Software Sources, Third-Party Software tab, Add
deb http://archive.getdeb.net/ubuntu lucid-getdeb apps
- Add the repository GPG key, open a terminal window and type:
wget -q -O- http://archive.getdeb.net/getdeb-archive.key | sudo apt-key add -
- Click the “Install this now” button below the screenshot of the desired application.

Voice and Video Calls with Empathy using GTalk
Install the following packages
sudo apt-get install telepathy-sofiasip telepathy-butterfly telepathy-idle libtelepathy-farsight0 python-tpfarsight gstreamer0.10-plugins-ugly-multiverse gstreamer0.10-ffmpeg

