четверг, 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);
}

четверг, 3 июля 2008 г.

Хозяйке на заметку

Если при экспорте в Новости первая строка в XML пустая - нужно удалить первые/последние пустые строки во всех шаблонах

пятница, 21 марта 2008 г.

Аргументы Views

Appendix: Exported Related Headlines View


  $view = new stdClass();
  $view->name = '1_related_headlines';
  $view->description = 'Related headlines block';
  $view->access = array (
);
  $view->view_args_php = 'if(arg(0) == \'node\' && is_numeric(arg(1))){
  $nid = arg(1); //node id
  $tids = db_fetch_array(db_query("SELECT tid FROM {term_node} WHERE nid = %d", $nid)); //simplest query for node tids
  if($tids){
    return array(implode(\'+\', $tids));
  }
}';
  $view->page = FALSE;
  $view->page_title = '';
  $view->page_header = '';
  $view->page_header_format = '1';
  $view->page_footer = '';
  $view->page_footer_format = '1';
  $view->page_empty = '';
  $view->page_empty_format = '1';
  $view->page_type = 'node';
  $view->url = '';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '10';
  $view->block = TRUE;
  $view->block_title = 'Related Headlines';
  $view->block_header = '';
  $view->block_header_format = '1';
  $view->block_footer = '';
  $view->block_footer_format = '1';
  $view->block_empty = 'There are currently no related links.';
  $view->block_empty_format = '1';
  $view->block_type = 'list';
  $view->nodes_per_block = '10';
  $view->block_more = FALSE;
  $view->block_use_page_header = FALSE;
  $view->block_use_page_footer = FALSE;
  $view->block_use_page_empty = FALSE;
  $view->sort = array (
    array (
      'tablename' => 'node',
      'field' => 'changed',
      'sortorder' => 'DESC',
      'options' => 'normal',
    ),
  );
  $view->argument = array (
    array (
      'type' => 'taxid',
      'argdefault' => '2',
      'title' => '',
      'options' => '0',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array (
    array (
      'tablename' => 'node',
      'field' => 'title',
      'label' => '',
      'handler' => 'views_handler_field_nodelink_with_mark',
      'options' => 'link',
    ),
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'story',
),
    ),
  );
  $view->exposed_filter = array (
  );
  $view->requires = array(node);
  $views[$view->name] = $view;


Hope that helps :-)