Archive for Uncategorized

Why Should I Buy the Microsoft Action Pack?

I was a former Action Pack subscriber and recently signed up again. The main reason I hadn’t maintained my subscription to the action pack was it became more difficult to acquire.  Microsoft has since made it much easier, requiring you take an intuitive 10 question quiz about the benefits of the action pack in order to get valid action pack (among other business requirements which are easily met by companies where the action pack is applicable).

There are two versions of the action pack — the solution provider version and the development and design version.  Both provide exceptional value.  Here are some of the features of each:

Microsoft Action Pack Solution Provider

  • Three internal use software licenses for Windows 7, Windows Server 2008, SQL Server, Exchange, Sharepoint, SBS and more
  • TechNet subscription
  • Technical Support
Microsoft Action Pack Development and Design
  • For those organizations who do software design and development
  • Internal use software licenses for Windows 7, Windows Server 2008, SQL Server, Exchange, Sharepoint, SBS and more
  • TechNet subscription
  • Technical Support
  • One limited MSDN subscription
Overall, the action pack is absolutely worth it, if not for the MSDN subscription alone.  Our company is the appropriate size and was able to take immediate advantage benefits of the action pack well above the cost of the subscription.

Using day number for date manipulation in PHP

I needed to take a day number of the year and be able to manipulate that to get other dates associated to it. Let’s take today, October 19th. This is the 292nd day of the year. We can convert this into Unix seconds:

$seconds = date('U',strtotime(date('Y').'-01-01 +'.(292-1).' days'));

As you can see it isn’t a completely obvious solution but it works as needed. Now since we have the timestamp, we could get the Monday before the 180th day.

$seconds = date('U',strtotime(date('Y').'-01-01 +'.(180-1).' days'));
$week_begins = date('Y-m-d', strtotime( 'Last Monday', $seconds));

Channel Content Not Appearing in Expression Engine Template

This problem didn’t take a lot of time to figure out, but i did notice that I couldn’t find any blog post about this particular problem when in need of a quick answer.

Basically the problem was that when I made a template for an Expression Engine site, the channel I wanted was not showing up.
Turns out, when in the “channel management” page, the channel had an assigned status group other than the default “open/closed/pending”. Expression Engine templates are set to default only the posts with the status of “open”. < not an intuitive template setting if your semi-new to EE >

So, the fix was very simple. In the template file, have the channel set the status attribute to be the custom status that was created:

{exp:channel:entries status="customStatus"}
... content
{/exp:channel:entries}

PHP Get Real Current URL

I’m using this snippet to get the current url. It’s fairly thorough, and needed it for a WordPress plugin I’m working on for startup weekend.

function get_real_current_url() {
	$pageURL = 'http';
	if ($_SERVER["HTTPS"] == "on") {
		$pageURL .= "s";
	}
 
	$pageURL .= "://";
	$pageURL .= $_SERVER["SERVER_NAME"];
	if ($_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= ":".$_SERVER["SERVER_PORT"];
	}
	$pageURL .= $_SERVER["REQUEST_URI"];
	return $pageURL;
}

Getting Google Map markers from a jquery ajax call

The Google Maps API is heavily biased to xml but what if you’d rather get your markers in json from an ajax call? Since we will be using jquery for the call you will need to include the library into your page.

First set up the code that will serve the json. We will put this at /get_markers and will be using Kohana 3 but any code will work aslong as it returns json.

//get markers from database
$lastpoints=ORM::factory('markers')->where('timestamp','>',$this->request->post('date'))->order_by('timestamp','DESC')->find_all();
if($lastpoints->count() > 0)
{
	echo json_encode( array_map( create_function( '$obj', 'return $obj->as_array();'), $lastpoints->as_array() ) );
}

Now we can grab that data and use it.

$.ajax({
        type: "POST",
        url: '/get_markers',
	dataType: 'json',
	data: 'date=' + dateText,
        success: function(data){ 
	     locations.length = 0;
	     for (p = 0; p < data.length; p++) {
	           locations.push(Array(data[p].latitude,data[p].longitude,data[p].timestamp));
	     }
	     initialize();
	}
});

In the example above we would be reloading only the new markers onto the map. If you would like the markers to just be added to the markers that are already on the map, you will need to remove the loop and the locations length reset in the success function and just have the array push. The initialize function is the code that sets up the map and is found in most examples and tutorials on the API.

Of course you will need to set the ajax call to an event.

What is ClickOnce

The Microsoft technology Clickonce is technology that enables clicking a link on a website and installing software. It has been a component of the .NET framework since version 2.0. ClickOnce is similar to Java WebStart and GNU/Linux ZeroConfig.

ClickOnce is supported natively by Internet Explorer and Firefox via a Firefox plugin.

There is also a provision in the ClickOnce architecture to automatically update applications. Applications can be configured to check for updates automatically at startup or after startup. ClickOnce also provides API’s for further controlling the update process.

ClickOnce is enabled in the Visual Studio project settings area.

Ordering mysql results by certain field value first

I needed to order a mysql result but in a somewhat custom way.

Say there is a field “id” and rows of values of 8, 4, 2, and 19.

SELECT `id` FROM `table_name` ORDER BY `id`;

This would obviously return the order: 2,4,8,19.

But what if you needed the 4 to be first and then order by integers?

SELECT IF(`id` = '4','0','1') AS `custom_order` FROM `table_name` ORDER BY `custom_order`;

This would return 4,2,8,19.

Simple and can be really helpful.

How do I queue jQuery ajax requests

I was recently doing some javascript testing and needed to make some asynchronous (sequential) ajax requests for authentication, creating of records, etc. As with all good things jQuery, the solution to this problem is to install the plugin. The ajaxq plugin, that is.

Installation is as simple as including it in your code.

[script language="javascript" src="/js/jquery-1.6.2.min.js"][/script]
[script language="javascript" src="/js/jquery.ajaxq.js"][/script]

Then calling the $.ajaxq method, which takes the queue name (use different names to run multiple asynchronous queues synchronously) and the settings object with the same parameters as jQuery’s ajax function.

$(function() {
	//Login, then logout
	$.ajaxq("queue", {
		url: '/api/user/login',
		type: "POST",
		data: { 
			username: "username", 
			password: "password"
		},
		success: function(data) {
			document.write("Logged in...<br />");
		}
	});
 
	$.ajaxq("queue", {
		url: '/api/user/logout',
		type: "POST",
		success: function(data) {
			document.write("Logged out...<br />");
		}
	});
});

And finally, an example which runs two queues simultaneously:

$(function() {
	//Login, then logout
	$.ajaxq("one", {
		url: '/api/requestone',
		type: "POST",
		success: function(data) {
			document.write("Request one queue 'one'...<br />");
		}
	});
 
	$.ajaxq("two", {
		url: '/api/user/logout',
		type: "POST",
		success: function(data) {
			document.write("Request 1 queue 'two'...<br />");
		}
	});
 
	$.ajaxq("two", {
		url: '/api/user/logout',
		type: "POST",
		success: function(data) {
			document.write("Request 2 queue 'two'...<br />");
		}
	});
});

Output could either be

Request 1 queue 'two'...
Request 2 queue 'two'...
Request 1 queue 'one'...

or

Request 1 queue 'one'...
Request 1 queue 'two'...
Request 2 queue 'two'...

Pagination in Kohana 3.2

Unfortunately Kohana 3.2 dropped the pagination module. Usually updates just extend the current features so not sure why they removed the functionality. It may have had pure MVC concepts but it was a nice feature.

However, getting the results in orm for a pagination is still pretty simple. First add the route that will pass the page number from the friendly URL.

Route::set('pagi_page', 'welcome/index(/)')
	->defaults(array(
		'controller' => 'welcome',
		'action'     => 'index',
		'page' => '1'
	));

Next set up the limits in your controller action:

public function action_index()
{
     $limit = 30; //change this to the number of results you want.
     $page_number = $this->request->param('page');
     $offset = $limit * ($page_number-1);
}

And then just use those variables to manipulate the ORM to get the expected results.

public function action_index()
{
     $limit = 30; //change this to the number of results you want.
     $page_number = $this->request->param('page');
     $offset = $limit * ($page_number-1);
 
     $results = ORM::factory('items')->limit($limit)->offset($offset)->find_all();
}

Adding a dynamic robots.txt to Kohana 3

Adding a function that dynamically generates a robots.txt  to Kohana 3 is actually very easy.

First we need to add a route to bootstrap.php that will route the yourdomain.com/robots.txt url to the function.

Route::set('robots', 'robots.txt')
 ->defaults(array(
  'controller' => 'home',
  'action'     => 'robots',
 ));

Now we create the action “action_robots” in the home controller and output the exclusions we are looking for.

public function action_robots()
{
 
		header('Content-type: text/plain');
		echo "User-agent: *\r\n";
		echo "disallow: /";
		exit();
}

The content-type here is very important, as search engines will not read it as a php file.

NOTE: This will not work if your application if the index_file configuration in bootstrap.php is set to TRUE. This is because if it is set to TRUE then the url would turn out to be /index.php/robots.txt which is obviously not where the search engines will be looking for the restrictions.