Archive for the "Technology" Category

How to call external site url using jQuery / AJAX

Recently I worked on a project where I had to pull data from an external site and populate a select on client’s website. Initially I thought it would be quite easy and straight forward. I used jQuery ajax request to fetch the data. But to my surprise I was not getting request call or any response. After struggling for atleast an hour I remembered Same origin policy

Then I figured out you can call the external url provided it gives the response in JSONP format. Below code worked for me.

$.ajax({
url: externalURL,
dataType: ‘jsonp’,
success: function(data){
// write your code here
}
});

If external site does not give JSONP response, you can call the url using iframe and then assign the response to field that you want. I have not tried this approach but it should work.

Hope this helps…

CiviCRM iphone app ( pre alpha release )

I have developed very basic iphone app for CiviCRM using Titanium framework.

Features

  • Allows users to “Search Contacts” from their remote CiviCRM database.
  • Add Individuals.
  • App uses CiviCRM REST interface so you can scale it according to your needs.

You can get the code from our public svn repository: http://svn.civicrm.org/tools/branches/v3.2/other/iphone_1.0/

Few screenshots:

Originally posted on http://civicrm.org/blogs/kurund/civicrm-iphone-app-pre-alpha-release

How to remove default title set by drupal

You can implement a module and call below function

function modulename_preprocess_page(&$variables) {
unset($variables['title']);
}

This will unset default title, but there are 2 problems:
1. This hook is not called by all the themes. So you cannot entirely rely on this.
2. Some themes check if ‘title’ exits before showing breadcrumb. So if you unset title then your breadcrumb will also disappear.

So finally I came up with this hack.

drupal_set_title( ‘ ‘ );

Note that there is space  between single quotes. This removes default title

Reverting a commit in svn

If you want to revert your commit use below command

svn merge -r [current_version]:[previous_version] [repository_url]

svn commit -m “Reverting bad commit. [previous_version]”

My notes: Upgrading CiviCRM v2.1 to CiviCRM v3.1 ( Drupal / Joomla )

Recently we successfully upgraded one of our client CiviCRM install from v2.1 to v3.1.2. Over all I was pretty happy with the CiviCRM upgrade process considering client’s db was in bad shape. This client is one of the early adopters of CiviCRM, ( since v1.8 ) that made things bit worse.

Few things you should remember before starting upgrade:

1. Backup your working CiviCRM database

If you have demo / test server try to upgrade it first rather than live. Always keep backup of your current working CiviCRM database so that you can revert back if needed.

2. Ensure Database Integrity

Never assume client’s database structure is correct, it might be upgraded and hence might be missing foreign key constraints etc, especially if it is been upgraded from CiviCRM v1.x. This might lead to more errors and will take more time to fix / result in more issues during upgrade process.

3. Understand upgrade errors

Try to figure out why you get errors during upgrade process rather than commenting sql or php code in CiviCRM.

4. Fix current database issues

If you get any sql errors during upgrade try to fix your current database. Most of the time database might have stale data.

So based on above, we followed these steps:

1. Backup CiviCRM database. I usually do it via command prompt.( you can also use phpmyadmin or any other database qui’s )

mysqldump -u DBUSER -p PASSWD DBNAME > civicrm_backup.mysql

2. We followed these steps to ensure database integrity before upgrade:

- Export only data from current CiviCRM database. Before exporting I would suggest you should empty cache tables in CiviCRM.

civicrm_cache, civicrm_group_contact_cache, civicrm_acl_cache, civicrm_acl_contact_cache

Then take database backup:

mysqldump5 -c -e -t -n -u DBUSER -p PASSWDS DBNAME > civicrm_only_data.mysql

- Drop current database

mysqladmin -f -u DBUSER -p $PASSWD drop DBNAME

- Create new database and import structure from sql/civicrm.mysql ( from your current install, so in this case creating structure for CiviCRM v2.1 )

mysqladmin -f -u DBUSER -p $PASSWD create DBNAME

mysql -u DBUSER -p PASSWD DBNAME < civicrm.mysql

- Now import data which you exported

mysql -u DBUSER -p PASSWD DBNAME < civicrm_only_data.mysql

After these steps make sure your CiviCRM is working without any errors.

Now you are ready for actual upgrade process :)

1. Upgrade CiviCRM v2.1 to latest 2.2.x release using following instructions:

2. Make sure everything is working fine. If yes than take database backup using instructions mentioned above. ( so that you have db to revert if next upgrade fails )

3. Upgrade CiviCRM v2.2.x to CiviCRM v3.1.x using these instructions:

During this upgrade we got few sql errors:
- We got foreign key contraint failure error when following SQL was fired by CiviCRM

ALTER TABLE `civicrm_activity`

ADD CONSTRAINT `FK_civicrm_activity_source_contact_id` FOREIGN KEY (`source_contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE SET NULL;

We got this error because civicrm_activity table in client’s db had wrong values. For few records “source_contact_id” value didn’t exist in civicrm_contact table. So we did source_contact_id = NULL for those activity records.

- Error

DB Error: constraint violation Database Error Code: Cannot add or update a child row: a foreign key constraint fails (`database_civicrm/civicrm_group_contact_cache`, CONSTRAINT `FK_civicrm_group_contact_cache_contact_id` FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE), 1452

This was because civicrm_group_contact table had entries for contacts that didn’t exits in civicrm_contact table. So we deleted those entries from civicrm_group_contact table.

Note that after each failed step you should rollback to previous working copy / fixed copy.

I agree this process looks bit complicated :) but trust me it might help you to save lot of time in debugging upgrade errors and ensure smooth future upgrade. It took me around 3-4 hours for upgrading this install from CiviCRM v2.1 to CiviCRM v3.1

I hope this helps someone :)

MacPorts: MySQL upgrade from 5.0.x to 5.1.x gives error during insert ( mysql_upgrade / ERROR 1558 (HY000) )

Yesterday I upgraded my mysql from 5.0.x to 5.1.x. After upgrade inserts started to fail with this error:

ERROR 1558 (HY000) at line 146: Column count of mysql.proc is wrong. Expected 20, found 16. Created with MySQL 50067, now running 50142. Please use mysql_upgrade to fix this error.

After little bit of google I found this solution. You need to run following command.

sudo  /opt/local/lib/mysql5/bin/mysql_upgrade -uroot -p –basedir=/opt/local/lib/mysql5/bin/mysql

This fixed my db issues. Hope this helps…

iGoogle style dashboard implementation using jQuery

Today I have finally finished my secret project for CiviCRM v3.1. It was to implement iGoogle style dashboard  with few features specific to our project. Main base for this feature was jquery ui’s sortable, droppable and draggable plugins.

It is always challenging and fun to work on such features. For complete  info check: http://civicrm.org/node/677

Configure tata indicom photon+ on Mac ( Snow Leopard )

Tata Indicom supply drivers along with photon+ device, but it didn’t work for me on my Mac (Snow Leopard). So I had to manually download it from web.

You need following packages:

1. HUAWEI_UMTS_Dashboard_MACB301D 11SP00C03(ISO).zip (Driver) – required

- Once you install above driver it will automatically add a device “HUAWEIMobile-Modem” in your Network Preferences. Next step is to configure the device.

Configuration: Default

Telephone Number: #777

Account Name: internet

Password:internet

“Apply” your changes and then click “Connect”.

2. MobileConnectDriver(4.15.00.00).zip (Software) – optional

- You can also use this software for connecting to internet. This software gives you more details like signal strength, data transfered/downloaded etc.

Leopard (Mac OS X 10.5) to Snow Leopard (Mac OS X 10.6) upgrade problems / fixes

This blog post cover problems that I faced after upgrading my MacBook Pro from Leopard to Snow Leopard.

1. Firefox v 3.5.3 crashes.

- Firefox started to crashed randomly. Especially when I tried to access sites with flash content.

Solution: Few google results told me to re-install firefox and then install latest flash plugin. But that didn’t fix my problem. So finally I rebooted by system in Safe Mode. Opened firefox and then again restart in normal mode. After that I didn’t have any crash issues.

2. MacPort fails to install / compile new packages

- I have Apache, MySQL, PHP etc installed using MacPorts ( http://www.macports.org )

- So after upgrade, my MySQL stopped working from terminal. Also I got few error when I tried to upgrade few packages.

checking for iconv_open in -liconv... no
configure: error: *** No iconv() implementation found in C library or libiconv
Error: Unable to upgrade port: 1
--->  Executing: /opt/local/bin/port -uR upgrade glib2

Solution: So I again googled and found out that you need to re-install your MacPorts once you do major OS upgrade. So I followed these instructions: https://trac.macports.org/wiki/Migration. This fixed my issues.

For my macbook I have installed following ports using this command:

sudo port install apache2 mysql5-server php52 +pear +mysql5 php5-xdebug subversion

I will update this post as and when I get more issues. Hopefully there won’t be any..

Detecting OS in PHP

Simply

echo PHP_OS;

It will print:

For Mac: Darwin

For Windows Visat: WinNT

For Ubuntu: Linux

Also if you want to know OS details about users accessing your site, you can use

$_SERVER['HTTP_USER_AGENT']