четверг, 11 декабря 2008 г.

Падежи месяцев

1. Закачать перевод fix-months.po

2. Пропатчить функцию


function format_date2($timestamp, $type = 'medium', $format = '', $timezone = NULL) {
if (!isset($timezone)) {
global $user;
if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
$timezone = $user->timezone;
}
else {
$timezone = variable_get('date_default_timezone', 0);
}
}

$timestamp += $timezone;

switch ($type) {
case 'small':
$format = variable_get('date_format_short', 'm/d/Y - H:i');
break;
case 'large':
$format = variable_get('date_format_long', 'l, F j, Y - H:i');
break;
case 'custom':
// No change to format
break;
case 'medium':
default:
$format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
}

$max = strlen($format);
$date = '';
for ($i = 0; $i < $max; $i++) {
$c = $format[$i];
if (strpos('AaDFlM', $c) !== FALSE) {

$date0 = gmdate($c, $timestamp);
if ( $c == 'M' and $date0 == 'May' )
$date0 = "May2";
$date .= t($date0);
}

else if (strpos('BdgGhHiIjLmnsStTUwWYyz', $c) !== FALSE) {
$date .= gmdate($c, $timestamp);
}
else if ($c == 'r') {
$date .= format_date($timestamp - $timezone, 'custom', 'D, d M Y H:i:s O', $timezone);
}
else if ($c == 'O') {
$date .= sprintf('%s%02d%02d', ($timezone < 0 ? '-' : '+'), abs($timezone / 3600), abs($timezone % 3600) / 60);
}
else if ($c == 'Z') {
$date .= $timezone;
}
else if ($c == '\\') {
$date .= $format[++$i];
}
else {
$date .= $c;
}
}

return $date;
}

вторник, 4 ноября 2008 г.

Функция разбивки массива


/*
Разбить массив arr на n частей

Возвращает массив res, содержащий части исходного массива
*/
function array_divide($arr, $n) {

//Проверка входных параметров и подготовка
$res = array();
$cnt = count($arr);
if ( $n < 1 || $cnt < 1) return $res;

$part = intval( $cnt / $n);
$rest = $cnt - $part*$n;

//Определение массива интервалов
$intervals = array(0 => 0);
for ( $i=1; $i<$n; $i++ )
$intervals[$i] = $part*$i;

while ( $rest > 0 ) {
for ( $i=1; $i < count($intervals); $i++ ) {
$intervals[$i]++; $rest--;
}
}
$intervals[] = $cnt;

//Заполнение результирующего массива
for ( $i=0; $i < count($intervals)-1; $i++ )
$res[$i] = array_slice( $arr, $intervals[$i], $intervals[$i+1]-$intervals[$i] );

return $res;
}


Пример использования:

$terms = taxonomy_get_tree(1);
$items = array_divide( $terms, 3);
foreach ( $items as $key => $arr ) {
print "
    ";
    foreach ( $arr as $key2 => $value )
    print "
  • " . $value . "
  • ";
    print "
";
}

среда, 3 сентября 2008 г.

Облако тегов

Произвольная сортировка

function tagadelic_sort_tags2($tags, $sort) {
// list($sort, $order) = explode(',', variable_get('tagadelic_sort_order', 'title,asc'));
switch ($sort) {
case 'title':
usort($tags, "_tagadelic_sort_by_title");
break;
case 'weight':
usort($tags, "_tagadelic_sort_by_weight");
break;
case 'random':
shuffle($tags);
break;
}
if ($order == 'desc') {
$tags = array_reverse($tags);
}
return $tags;
}


//---------------------------------------------------------

$vocs = array(1);
$tags = tagadelic_get_weighted_tags($vocs, 3, 50);
$tags = tagadelic_sort_tags2($tags,'weight,desc'); //Сортировка
$output = theme('tagadelic_weighted',$tags);
print $output;

Нумерация комментариев



$page = $_GET['page'];
if ( ! $page ) {
$page = 0;
}
$comments_per_page = _comment_get_display_setting('comments_per_page');

$index_start = ( $page * $comments_per_page ) + $id;

print l($index_start, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid);

четверг, 14 августа 2008 г.

Следующая/Предыдущая



//Предыдущая-следующая
$query = "SELECT DISTINCT n.nid, n.title, n.created
FROM {node} n
INNER JOIN {term_node} tn ON n.nid = tn.nid
WHERE tn.tid = $tid AND n.status = 1
ORDER BY n.sticky DESC, n.created DESC" ;

$result = db_query(db_rewrite_sql($query));

$prev = ''; $next = '';
$before = 0;
while ($anode = db_fetch_object($result)) {
if ( $anode->nid == $node->nid ) $before = 1;

if ( $before == 0 )
$prev = l( $anode->title, "node/$anode->nid" );
else if ( $anode->nid != $node->nid )
{ $next = l( $anode->title, "node/$anode->nid" ); break; }

};

среда, 13 августа 2008 г.

Три колонки



$terms = taxonomy_get_tree($vid);
$cnt = count($terms);

if ( $cnt )
{

$rest = $cnt % 3;
$part = intval( $cnt / 3);
$intervals = array( 0 => 0, 1 => $part, 2 => $rest + 2*$part );
if ( $rest ) $intervals[1]++;
$intervals[] = $cnt;

for ( $i=0; $i < count($intervals)-1; $i++ )
{

for ( $d = $intervals[$i]; $d < $intervals[$i+1]; $d++ ) {
$term = $terms[$d];
print l( $term->name , "taxonomy/term/$term->tid");
}


}


}


Вывод заголовка иерархии



/*
Вывод заголовка иерархии
*/
function head_term( $tid, $rootname )
{

function head_term( $tid )
{
if ( is_numeric($tid) )
{
$terms = taxonomy_get_parents_all($tid);
$termdata = taxonomy_get_term($tid);
$vid=$termdata->vid;
$dl = " » ";
$navigate = '';
foreach ( $terms as $term )
{
if ( $navigate != '' )
$link = l_nbsp ( $term->name, "taxonomy/term/$term->tid").$dl;
else
$link = $term->name;
$navigate = $link.$navigate ;
}
$out = voc_root(0);
$root2 = voc_root($vid);
if ( $root2 != '' ) $out .= $dl.$root2;
$out .= $dl.$navigate;
}
else
{
$out = voc_root(0);
$vid = -1;
}
echo "
$out
";
menu_voc($vid);
}