Wednesday, April 20, 2011

Dealing with duplicate taxonomy terms

I haven't encountered this views bug until last Monday. I was working a view where I needed to show unique taxonomy terms in a grid. I was setting up something along the lines of "browse by category" view and what I got was a grid filled with multiple/duplicate terms.

Knowing the error, I started googling for answers but that exercise just made me run around in circles. Until I got to himerus' blog (he's also the guy working on the omega theme, you should check it out) and a drupal.org node.

With the solutions I started trying them out. I settled for the "custom" module solution. The first time I tried it, it didn't work right off the bat. Take a look at the code:

<?php
// $Id$

/**
* Implementation of hook_views_pre_render
* This function is in place to filter out duplicate taxonomy terms
* From listings. It will cycle each result, and store a new array of
* unique terms, and when a duplicate is found, will unset that result
*
* @param $view
*/
function your_module_views_pre_render($view){
// first we make sure we are dealing with the correct view
if ($view->name == "your_view_name") {
  // create our array for comparisons
  $unique_tids = array();
  // let's cycle through each default result, and do some dirty stuff
  foreach($view->result AS $k => $result){
   if(in_array($result->node_node_data_YOUR_FIELD__term_data_tid, $unique_tids) || !$result->node_node_data_YOUR_FIELD__term_data_tid) {
    /* we already have seen this TID in the results, so blow that crap away
     * also will blow away any that are empty for some odd reason
     */
    unset($view->result[$k]);
   }
   else {
    // this is a term we haven't seen, so let's not blow it away, but add
    // it to our array of unique id's to present as the "true" result of this view
    $unique_tids[$k] = $result->node_node_data_YOUR_FIELD__term_data_tid;
   }
  }
  /* now, we have an accurate unique list of terms in $unique_tids
   * next, we cycle those to reorder the crap random ordering
   * since these tids were pulled from the nodes in the order they were
   * set to sort from the node type view
   */
  $alpha_arr = array();
  // cycle each of our unique tids, referencing the original key of the view->result array ($k)
  foreach($unique_tids AS $k => $tid) {
   // we need to grab the term now, not the tid to sort alpha
   $alpha_arr[$k] = strtolower($view->result[$k]->node_node_data_YOUR_FIELD__term_data_name);
  }
  // sort the array, maintaining the $k key so that we may again reference back to the original data
  asort($alpha_arr);
  // create new array of results to overwrite the current one
  $new_results = array();
  /* cycle one last time now that we have unique terms, sorted alphabetically
   *    the point of this is to now take our $k reference, and grab the original $view->result data
   *    that references this item
   */
  foreach($alpha_arr AS $v => $term_name) {
   $new_results[] = $view->result[$v];
  }
  // get rid of the original result set
  unset($view->result);
  // replace it with our new, accurate result set
  $view->result = $new_results;
}
}

Take a look at the "your_view_name", node_node_data_YOUR_FIELD__term_data_name and node_node_data_YOUR_FIELD__term_data_tid items. It should be easy to figure out the "your_view_name" thing. Just replace it with name of your views.

The  node_node_data_YOUR_FIELD__term_data_name and node_node_data_YOUR_FIELD__term_data_tid items is slightly different and made me to install a devel module to figure it out. Take a look at the $result array, I relized that I needed NOT to use the node_node_data_YOUR_FIELD__term_data_name or the node_node_data_YOUR_FIELD__term_data_tid but instead used a much shorter array key. So instead of "node_node_data_term_id__term_data_tid" use "term_id".

What's left is to turn on the module and flush the cache.

You should be golden!

Tuesday, March 29, 2011

Fixing that buggy stock ROM on your CM MagnumHD

My phone's
home pane
For those who are lucky enough to get Cherry Mobile's MagnumHD, we all can agree that the stock ROM f***** sucks. I really mean it sucks - the random restarts, the FCs, context buttons stop responding and the the most annoying is the screen times out (i.e. blacks out) when you make a phone call. So, being a geek, I will have none of this and I started scheming (along with Aryan Limjap which BTW, provides us the lovely images attached to this post) to get a fix into my CM Magnum. Warranty be damn!

System Pane
And what do you know, the Indians Indonesians came up with a better ROM. As you can see I'm now running a Wellcom-A99 ROM on my CM MagnumHD. I can tell you it runs better, cooler and longer. Also I would like to point out the Android version 2.2.2. I will still flash this thing to 2.3 when it comes out. But trust me on this, MY PHONE IS RUNNING BETTER on this ROM.

You might wonder how is this possible, then you apparently didn't inherit enough "bright" genes from your parents. But to get my point moving, CM MagnumHD is OEM phone from Foxconn and is rebranded in a lot of other countries.
  • Philippines - CM MagnumHD
  • China - Huawei Ideos X6
  • US - Viewsonic ViewPad 4
  • India Indonesia - Wellcom A99
  • India - OliveSmart V-S300
With the Apps
And that is why children, I can stick an Wellcom A99 ROM on my CM MagnumHD. But still want to beat this into your head, if you are doing this you are voiding your phone's warranty. As for me, I already voided mine when I rooted my phone. Refer to the screenshot with the apps.

So if you understand what I am telling you and you still want to flash in the Welcomm ROM then click here. Just scroll down and look for the link. The download is about 200MB. 

And btw, did I mention to you all that there's still a slight chance that this will put your phone into a boot-loop or (worse) brick your phone?

Edit: Mecha50 corrected me that the A99 ROM is Indonesian. My bad.

Friday, March 25, 2011

Yeah, I run Ubuntu...Wanna make sumthin' out of it?


Yes, I know my desktop looks cool. It also very, very sweet! My 3 year old ACER laptop which has only 1gig of RAM is running quite smoothly and responsively. To quote yoyong ucab,
"Best performance kaayo."
Trust me on this - when you see this thing run Vista, it makes you wanna cry. Vista is the OS that was bundled with this thing when I bought it and it so f'in slow. And to add insult to injury its Vista Starter. Yes. It's the fork-over-more-bucks-so-you-can-run-more-than-3-apps Vista Starter OS.

You will also notice that I got Netbeans running on this thing. I am a developer after all and its gravy. I even got to sync my code from my kenai.com repo.

You should be looking at Ubuntu on you next laptop. I'm beginning to question the wisdom of paying for the "other" operating system when there's this.

Now, I wonder if I can set this one up as an Android dev/test machine...hmmm.