Model saving vs. model validation when uploading files

Newbies to Yii and ActiveRecord design pattern, are often confused, what is difference between $model->save() and $model->validate(). though difference should be obvious, there are many code examples in the forum and Yii wiki, that proves, that many people still doesn’t understand it. Especially, when it comes to performing additional operations around saving model, like file upload.

Read More “Model saving vs. model validation when uploading files”

Using Twitter Bootstrap’s popups like real menu items

Twitter Bootstrap framework is fabulous and thanks to many Yii extensions supporting it (Yii-Boostrap, Yii-Booster, Yiistrap, YiiWheels and more) using it in own application is like snapping with fingers.

One of the best features of Bootstrap is a very easy way of creating menus and adding popup menus to elements (like buttons). However, in default implementation popups are oriented toward working just like a fancy links — i.e. redirecting browser to destination URL. In this article, you’ll find how force them to work as real menu items — i.e. capturing their onclick event and doing something with content.

Read More “Using Twitter Bootstrap’s popups like real menu items”

Self-documenting code is a myth

How many times did I heard that stupid talk? “Comments aren’t needed! Code should be self-documenting”. If you think this way, then… believe me or not, you’re wrong. Good developer does not need comments to describe, what he or she is doing. But there’s always a purpose for doing something, which must be documented for future reference. If you think, you can achieve the same with self-documenting code, you’re going strictly into violating fourth of Five major software development rules. Which says: “Premature optimization is the beginning of all hell!“.

Read More “Self-documenting code is a myth”

CSS and JS minifier/beautifier that rocks!

I definitely love and adore Avivo’s CSS/JS Minifier/Beautifier. Mostly for options that similar tools lack. Like: braces on own line, preserve empty lines and ability to select intend character.

It has a small bug in CSS Beautifier. Every : is beautified to : (with space), which breaks pseudo-classes like :hover and and pseudo-objects like ::before. But, this can be easily fixed (manually; simple find-and-replace in text editor).

So, even with this, Avivo’s tool is just fabulous!

CGridView and CDetailView column or attribute value by expression

CDetailView does not use “deferred” evaluation like CGridView does on rendering rows. CDetailView gives you direct access to your model, while CGridView only to a per-row prepared data. So, to render CGridView‘s column or CDetailView‘s attribute row, you must use slightly different approaches. Here is how.

Read More “CGridView and CDetailView column or attribute value by expression”

Upgrading MySQL on-board XAMPP

XAMPP is a great package of Apache, MySQL, phpmyadmin and some others (actually the best I’ve ever used). The only disadvantage of it, is that guys behind this project haven’t got much spare time and update it fairly slow. Newest XAMPP version (1.8.1) is over a year old now and still has MySQL 5.1.41 on-board, while version 5.6.10 is available for download, as of writing this. Here are some tips on manually upgrading MySQL on-board your XAMPP package.

Simple copy & folder switch does the job and is enough to upgrade MySQL on-board XAMPP from 5.1.41 to 5.6.10. However there are slight side effects, you must consider.

Read More “Upgrading MySQL on-board XAMPP”

HTML-to-Markdown on-line tool, that actually works

If you search the web for “convert HTML to Markdown online“, Google will return you a quite big bunch of useless links. Mostly pointing to websites that only claims that can do HTML-to-Markdown conversion (while actually doing opposite one) or that are doing conversion in a really poor quality (failing on simple HTML).

After digging through many of them, I finally found “html2text” — a simple, yet powerful Python script that just do the job. It was the only online tool, that I found, that actually was able to convert my simple (three unordered lists with some links inside) HTML text into pure Markdown.

Using tcpdump for capturing data sent by TK102 GPS tracker

Xexun TK102 GPS Tracker and most of its clones can send localization data either via SMS messages (some models offers just a pure data, that you have to analyze yourself, some sends it out along with Google Maps link) or via GPRS protocol, directly to specified server. Which then can analyze and visualize that data. SMS sending works just great in most cases, while getting GPS data via GPRS can be a real pain in the ass. This post is written with hope to help you a little bit, if you’re facing the same problem.

Read More “Using tcpdump for capturing data sent by TK102 GPS tracker”

TK102 connector / plug / cable type (and schematic diagram)

The Xexun TK102 GPS Tracker and most of its clones is using something, what English Wikipedia calls UC-E6 proprietary (non-USB) plug. So, this is neither microUSB nor miniUSB as I was assuming in the first place.

Here you’ll find some links to information about this plug and some simple schematic diagram for tracker.

Here is mentioned schematic diagram:

TK102-connector-plug

If you’d like to purchase a cable for Xexun TK102 GPS Tracker, then eBay should be your friend. If, on contrary you’d like to craft your own cable, then this great article will help you or can be at least a good starting point.

node.js based TK102 listener

There is a fairly good looking listener for TK-102 localizers, based on node.js. You can find it in this GitHub repository. I wasn’t able to check it out yet, but from the first sight it looks promising.

The only thing, that concerns me, is that many TK-102 clones requires properly formated reply from the server, to continue connection and send actual GPS data. If localizer does not receive such reply, it will break connection. I don’t see nothing about sending reply back to localizer, so I’m worried, if this will work with these clones requiring reply-back. But… we’ll check it, we’ll see it! :]

Xexun TK102 GPS tracker master password reset

Many sellers will tell you, that:

  • if you forget your master password (if you change it ever from default 123456, as you should do)
  • you’re deep in an ass
  • the only option for you, is to send tracker to them or to the producer, to reinstall original firmware
  • and that (of course!) you will have to pay for this.

This is lie in most cases and probably it is aimed to fool people paying extra money for reseting tracker.

In most scenarios you can easily reset it yourself. Here is how.

Read More “Xexun TK102 GPS tracker master password reset”

Simple element blocker with and without jQuery

If you’re using jQuery the best (the only reasonable?), in my opinion, way to block an element or entire page (for example for updating it contents) is to use blockUI plugin. But, if you only have to block one or two small elements then you may consider writing your own, very simple blocker instad of using blockUI. And if you’re using jQuery only for that purpose (really small project or task), the you may try to do the same without jQuery at all.

Read More “Simple element blocker with and without jQuery”

Test page for Markdown on Save Improved plugin

This is a dummy example post body, that can be used for testing Markdown rendering in any WordPress post or in any Markdown supporting page at all. I wrote it months ago and started using it for testing Markdown on Save Improved plugin. But then in turned out, that this quick sheet can be used virtually everwhere, where Markdown can be used. Just paste everything or selected part to test and verify Markdown in your blog, website, service etc.

Read More “Test page for Markdown on Save Improved plugin”

Print correct date in languages other than English

Unlike in English, full date written in many other languages (i.e. in Polish) has month written in genitive case. Neither WordPress (the_time()) nor PHP itself (date() on which the_time() is built on) supports writing dates in such format, so you’ve got to help yourself. This isn’t a hard task and there are many approaches to solve this problem across Internet. Here, I present on of the solution, ready to be used both in one, single theme or across entire WordPress Network.

Read More “Print correct date in languages other than English”

Hiding blog from guests or setting up ‘Under Construction’ site

WPRecipes has a quick and clean example of using shortcodes in WordPress in general and a particular example of restricting part of post (text, media or anything) to be visible only to registered users. This is quite nice example of doing so, without need of extra plugin for this purpose. You could also learn from here an overall knowledge of using shortcodes in WordPress, if you’re not familiar with that.

Keep in mind that this tips is for hiding part of post from guests. If you want to hide entire post, simply publish it as Private (all posts and pages are published as Public by default) — look in post or page settings.

Read More “Hiding blog from guests or setting up ‘Under Construction’ site”

My favorite AI Factory’s Chess Free games

I’m playing Chess Free by AI Factory Limited on my mobile devices. Games has an option to export gameplays in PGN format. Though I’m pretty weak player, I wanted to write down myself most interesting games I played.

If you’re anything more than beginner in chess, you probably won’t find anything interesting in this post. Sorry! :]

Read More “My favorite AI Factory’s Chess Free games”

Fixing small but annoying issues in Dashboard in WordPress Network

There are three small, but annoying things in Admin Dashboard that I always encounter, when dealing with WordPress Network installation:

  • My Sites menu items order,
  • slow loading of that menu,
  • WordPress sites’ Dashboards language.

All three issues can be easily fixed. But, you’ll need some deeper knowledge of WordPress and deal with some plugins (both hand-written and downloadable).

Alphabetic order of My Sites menu

First thing is how to sort My Sites menu items alphabetically?

The question, what order is used to sort My Sites menu in WordPress Network Dashboard remains unanswered. Items order in that menu changes as you’re making changes to your sites, but probably even WordPress authors can’t answer what sort order is used there. We only know for sure, that it isn’t alphabetical — the most wise and most obvious.

Here is a simple solution (network plugin) to solve this problem. Just copy this:

/*
 * Plugin Name: Sort My-Sites
 * Description: Sorts the My Sites listing alphabetically.
 * Author: Otto
 */

add_filter('get_blogs_of_user','sort_my_sites');

function sort_my_sites($blogs)
{
    $f = create_function('$a,$b', 'return strcasecmp($a->blogname, $b->blogname);');

    uasort($blogs, $f);

    return $blogs;
}

and paste it into text editor.

Save it under any name (sort-my-sites.php for example), put to a subdirectory (sort-my-sites) and ZIP it (sort-my-sites.zip). Then install and network enable such plugin. And voila!

This solution is a slight modification of this WordPress StackExchange answer.

My Sites menu items count

If you have many sites in your network and number of My Sites menu grows, you may even face the problem that your entire Dashboard loads visually slower only because Network Admin bar consumes much time and resources to build up My Sites menu.

In this case you may consider Joshua Lynch’s solution.

It utilizes similar approach as above and introduces some new filters, that removes all sites from My Sites menu and places commonly used items to it instead.

Dashboards’ Language

Very irritating (at least to me) is that when you (or your user) sets blogs language to different than English, not only blog is translated, but also is admin’s dashboard. Translations authors are paying much less attention to quality and update of dashboard’s translations, so translated dashboard may not only be confusing, but also improperly translated or with missing strings.

All these problems can be avoided, by forcing each dashboard to English, no matter, what language blogs are using. This can be done with a small and simple plugin called Admin in English.

Note: If you search the web for this plugin name, you may find some sites, outside WordPress.org, that shows some nice examples on how to force all dashboards to use the same, non-default (not English) language. I remember, that I run into article showing how to have all dashboards in Russian, while network blogs were all in Russian, English and German.

Free Javascript chess engines and notation board presenters

I had to find a free Javascript based chess engine for one of my projects. I must admit, that I’m a low-level player and much, much lower level chess developer, so I assumed, that there is no chance, you can write such advanced thing like complete chess engine in such simple language like Javascript.

Turned out, I was wrong. I was actually devatated (again and again) with the power and possibilities behind Javascript.

Read More “Free Javascript chess engines and notation board presenters”