lqdn_latest_nodes.module 13.4 KB
Newer Older
Mindiell's avatar
Mindiell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
<?php
/**
 * Returns the content of last block.
 *
 * @param $_type
 *   A string containing the type.
 * @param $_dossier
 *   An object containing all informations the about a dossier.
 *   In particularly, name and tags ID separated by commas.
 * @param $_nb
 *   The number of elements to return. By default 8.
 * @param $_offset
 *   ...
 *
 * @return
 *   A string containing html, ready to be display.
 */
function lqdn_latest_nodes_list_items($_type, $_dossier = NULL, $_nb = 8, $_offset = 0, $_template = 'simple_item') {
  global $language,$base_url;
  switch ($_type) {
    case 'rp':
      $type = "'revue_de_presse'";
      break;
    case 'actu':
      $type = "'actualite', 'important'";
      break;
    default:
      $type = "'actualite', 'important', 'revue_de_presse'";
  }

  /**
   * @todo use template.
   */
  $items = array();

  /*
   * GIBUS : use DISTINCT here, because untranslated nodes (like for
   * HADOPI, only in fr) are selected twice.
   */
  $query = "
    SELECT DISTINCT(n.`nid`), n.`created`, n.`title`, n.`language`, v.`teaser`, v.`body`
    FROM {node} n
    LEFT JOIN {node_revisions} v ON n.`vid` = v.`vid`";
  if (!empty($_dossier)) $query .= "
    LEFT JOIN {term_node} t ON n.vid = t.vid";
  $query .= "
    WHERE (n.`status` <> 0)
      AND (n.`type` in (" . $type . "))";
  if (!empty($_dossier)) $query .= "
    AND (t.`tid` in (" . $_dossier->tags . "))";
  $query .= "
      AND (n.`language` in ('***NO_LANGUAGE***', '%s', ''))
    ORDER BY n.`created` DESC
    LIMIT %d, %d";

  $result = db_query($query, $language->language, $_offset, $_nb);
  $i = 0;
  while ($data = db_fetch_object($result)) {
    $options['attributes']['title'] = trim($data->title);
    $options['attributes']['class'] = 'node_title';

    $prev_date = $date;
    $date = format_date($data->created, 'small');
    $date_class = ($date != $prev_date) ? 'date first-date' : 'date';

    $tags = array();
    $node_o = node_load($data->nid);
    foreach($node_o->taxonomy as $taxo) {
      $tags[] = array('data'=>l( $taxo->name, 'taxonomy/term/' . $taxo->tid, array('rel'=>'tag')),'class'=>'tag');
    }
    //$items[] = '<span class="' . $date_class . '">' . $date . '</span> ' . l(trim($data->title), 'node/' . $data->nid, $options);
    if ($_template == 'complete_item') {
      $image = '';
      $matches = array();
      preg_match('`<img[^>]+src="([^"]+)"[^>]*>`i', $data->body, $matches);
      if (!empty($matches[1]))
      {
        $image = $matches[1];
	if (!preg_match('`^https?://`',$image))
	{
		$image = $base_url.$image;
	}
	$image_max_width = 140;
	$image_max_height = 110;
	$chtitimage_dirpath = file_directory_path().'/smallnodeimage';
	$chtitimage = $chtitimage_dirpath.'/'.md5($image).'-'.$image_max_width.'x'.$image_max_height.'.jpg';
	if (!file_exists($chtitimage))
	{
		$imagesizes = getimagesize($image);
		if ($imagesizes[0] > $image_max_width || $imagesizes[1] > $image_max_height)
		{
			if (!file_exists($chtitimage_dirpath))
				mkdir($chtitimage_dirpath);
			exec("convert '".$image."' -resize ".$image_max_width."x".$image_max_height." ".$chtitimage);
			$image = $chtitimage;
		}
	} else {
		$image = $chtitimage;
	}

      } else {
        $dossiers = _lqdn_similar_nodes_get_dossiers(_lqdn_similar_nodes_get_tags($data->nid));
        if (!empty($dossiers))
          $image = $dossiers[0]->image;
        else {
          $image = '/sites/all/themes/lqdn314/images/_logo.png';
        }
      }
    }
    $node = (object)array(
      'title' => $data->title,
      'link' => 'node/' . $data->nid,
      'link_options' => $options,
      'date' => $date,
      'date_class' => $date_class,
      'teaser' => check_markup($data->teaser),
      'image' => $image,
      'tags' => theme('item_list', $tags, NULL, 'ul', array('class' => 'tags'))
    );
    $items[] = array('data'=> theme($_template, $node), 'class'=> str_replace('_', '-',$_template));
    $i++;
  }
  return $items;
}

/**
 * Returns the content of last block.
 *
 * @param $_type
 *   A string containing the type.
 * @param $_dossier
 *   An object containing all informations the about a dossier.
 *   In particularly, name and tags ID separated by commas.
 * @param $_title
 *   By default set to NULL (no title). It can be set to TRUE (default title).
 *   It can be also a string.
 * @param $_nb
 *   The number of elements to return. By default 8.
 * @param $_offset
 *   ...
 * @param $_attributes
 *   The attributes applied to the list element.
 *
 * @return
 *   A string containing html, ready to be display.
 */
function lqdn_latest_nodes_list($_type, $_dossier = NULL, $_title = NULL, $_nb = 8, $_offset = 0, $_attributes = NULL) {
  switch ($_type) {
    case 'rp':
      $class = 'last-rp';
      $title = t('Latest press articles on %dossier_name', array('%dossier_name' => $_dossier->name));
      break;
    case 'actu':
      $class = 'last-actu';
      $title = t('Latest news on %dossier_name', array('%dossier_name' => $_dossier->name));
      break;
    default:
      $class = 'latest-nodes';
      $title = '';
  }
  if ($_title === NULL || is_string($_title)) {
    $title = $_title;
  }

  $items = lqdn_latest_nodes_list_items($_type, $_dossier, $_nb, $_offset);

  return (count($items) <= 0) ? '' : '<div class="' . $class . '">' . theme('item_list', $items, $title, 'ul', $_attributes) . '</div>';
}

/**
Mindiell's avatar
Mindiell committed
171
 * Implementation of hook_blocki_list_alter().
Mindiell's avatar
Mindiell committed
172
 */
Mindiell's avatar
Mindiell committed
173
function lqdn_latest_nodes_block_list_alter(&$blocks) {
Mindiell's avatar
Mindiell committed
174 175 176 177 178
    $blocks[0]['info'] = t('The latest news');
    $blocks[1]['info'] = t('Latest news');
    $blocks[2]['info'] = t('Latest press articles');
    $blocks[3]['info'] = t('Latest publications');
    $blocks[4]['info'] = t('Latest publications (news and press articles)');
Mindiell's avatar
Mindiell committed
179 180 181 182 183 184 185
}

/**
 * Implementation of hook_block_configure().
 */
function lqdn_latest_nodes_block_configure($delta = '') {
  if ($delta == '1') {
Mindiell's avatar
Mindiell committed
186 187 188 189 190 191 192
    $form['ignore_the_latest_news'] = array(
      '#type' => 'checkbox',
      '#title' => t('Ignore the last news'),
      '#default_value' => variable_get('ignore_the_latest_news', 1),
      );
    return $form;
  }
Mindiell's avatar
Mindiell committed
193 194 195 196 197 198 199
}

/**
 * Implementation of hook_block_save().
 */
function lqdn_latest_nodes_block_save($delta = '', edit=array()) {
  if ($delta == '1') {
Mindiell's avatar
Mindiell committed
200 201
    variable_set('ignore_the_latest_news', $edit['ignore_the_latest_news']);
  }
Mindiell's avatar
Mindiell committed
202 203 204 205 206 207 208 209
}

/**
 * Implementation of hook_block_view().
 */
function lqdn_latest_nodes_block_view($delta = '') {
  global $language;

Mindiell's avatar
Mindiell committed
210 211 212 213 214 215 216 217 218
    /**
     * @todo gestion multilingue
     */
    $more_pages = array(
      1 => array('en' => 'news',         'fr' => 'actualite'      ),
      2 => array('en' => 'press-review', 'fr' => 'revue-de-presse'),
    );

    switch($delta) {
Mindiell's avatar
Mindiell committed
219
      case '':
Mindiell's avatar
Mindiell committed
220 221 222 223 224
        $block = array(
          'subject' => '',
          'content' => _lqdn_latest_nodes_the_latest_news(),
          );
        break;
Mindiell's avatar
Mindiell committed
225
      case '1':
Mindiell's avatar
Mindiell committed
226 227 228 229
        $block = array();
        $block['subject'] = t("Quad'news");
        $block['content'] = _lqdn_latest_nodes_latest_news();
        break;
Mindiell's avatar
Mindiell committed
230
      case '2':
Mindiell's avatar
Mindiell committed
231 232 233 234
        $block = array();
        $block['subject'] = t('Press review');
        $block['content'] = _lqdn_latest_nodes_latest_press_articles();
        break;
Mindiell's avatar
Mindiell committed
235
      case '3':
Mindiell's avatar
Mindiell committed
236 237 238 239 240
        $block = array(
          'subject' => t('Latest content'),
          'content' => _lqdn_latest_nodes_latest_nodes() . '<div style="clear:both;"></div>',
          );
        break;
Mindiell's avatar
Mindiell committed
241
      case '4':
Mindiell's avatar
Mindiell committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
        $block = array(
          'subject' => '',
          'content' => _lqdn_latest_nodes_latest_news_and_press_articles(),
          );
    }
    if ($delta >= 1 && $delta <= 2) {
      $p = $more_pages[$delta][$language->language];
      $block['content'] .= '<p>»&nbsp;' . l(t('Read more'), $p) . '</p>' .
        '<div style="clear:both;"></div>';
    }

    return $block;
}

function _lqdn_latest_nodes_the_latest_news() {
  global $language;
  $query = "
SELECT n.`nid`, n.`created`
FROM `node` n
WHERE n.`status` <> 0
  AND (n.`type` in ('actualite'))
  AND (n.`language` in ('***NO_LANGUAGE***', '%s', ''))
ORDER BY n.`sticky` DESC, n.`created` DESC
LIMIT 1
  ";
  $data = db_fetch_object(db_query(db_rewrite_sql($query), $language->language));
  $node = node_load($data->nid);
  //var_dump($node);
  //return node_view($node, 1);
  /*$tags = '<ul class="links inline">';
  foreach($node->taxonomy as $taxo) {
    $tags .= '<li class="taxonomy_term_' . $taxo->tid . '"><a title="" rel="tag" href="' . url('taxonomy/term/' . $taxo->tid) . '">' . str_replace(' ', '&nbsp;', $taxo->name) . '</a></li>';
  }
  $tags .=   '</ul>';*/
  $nb_files = count($node->files);

  $content = '';
  $content .= '<div id="very-last-news">';
  $content .= '  <h2 class="tampon">' . t('Hot news!') . '</h2>';
  $content .= '  <h3 class="title">';
  $content .= '    ' . l($node->title, 'node/' . $node->nid);
  $content .= '  </h3>';
  $content .= '    ' . check_markup($node->teaser);

  if (!empty($nb_files)) {
    $text = format_plural($nb_files, '1 attachment', '@count attachments');
    $path = "node/$node->nid";
    $options = array('attributes' => array('title' => t('Read full article to view attachments.')),
                     'fragment' => 'attachments',
                     );
    $attachments = l($text, $path, $options);
  }

  $metas = '  <ul class="metas">';
  $metas .= '    <li class="submitted">' . format_date($node->created) . '</li>';
  $metas .= '    <li class="more">' . l('» ' . t('Read more'), 'node/' . $node->nid) . '</li>';
  if (!empty($attachments)) {
    $metas .= '    <li class="attachments">(' . $attachments . ')</li>';
  }
  /*if (module_exists('lqdn_share_links')) {
    $metas .= '  <li class="share-links">' . _lqdn_share_links($node) . '</li>';
    }*/
  //$metas .= '    <li class="tags">' . $tags . '</li>';
  $metas .= '  </ul>';
  $content .= $metas;
  $content .= '</div>';

  return $content;
}

function _lqdn_latest_nodes_latest_news() {
  $offset = variable_get('ignore_the_latest_news', 1);
  return _lqdn_latest_nodes_the_latest_news() .
    lqdn_latest_nodes_list('actu', NULL, NULL, 10, $offset, NULL) .
'
<p><a href="#" class="more-nodes" data-type="actu">More...</a></p>
<script type="text/javascript">
  $("a.more-nodes").click(function(){
      $elem = $(this).parent().parent().find("div.last-actu div.item-list ul");
      var from = $elem.find("li").size();
      var type = $(this).attr("data-type");
      var url = "/more-nodes/" + from + "/" + type + "/-";
      $.getJSON(url, function(json){$elem.append($(json.content).hide().fadeIn(2000));});
      return false;
    });
</script>
';

}

function _lqdn_latest_nodes_latest_press_articles() {
  return lqdn_latest_nodes_list('rp', NULL, NULL, 10, 0, NULL);
}
function _lqdn_latest_nodes_latest_nodes() {
  return lqdn_latest_nodes_list(NULL, NULL, NULL, 10, 0, NULL);
}
//function lqdn_latest_nodes_list_items($_type, $_dossier = NULL, $_nb = 8, $_offset = 0)

function _lqdn_latest_nodes_latest_news_and_press_articles($_dossier = NULL, $_nb = 8, $_complete = 3) {
  global $language;
  drupal_add_css(drupal_get_path('module', 'lqdn_latest_nodes') . '/lqdn_latest_nodes.css');
  drupal_add_js(drupal_get_path('module', 'lqdn_latest_nodes') . '/lqdn_latest_nodes.js');

  if (empty($_dossier->name)) {
    if ($language->language=="en") {
      $l = l("Read more Quad'News", 'news');
    }
    else {
      $l = l("La suite des Quadr'Actu", 'actualite');
    }
  }
  else {
    if ($language->language=="en") {
      $l = l(t("Read more Quad'News about @dossier", array('@dossier' => $_dossier->name)), 'quad-news/' . $_dossier->machine_name);
    }
    else {
      $l = l(t("La suite des Quadr'Actu sur @dossier", array('@dossier' => $_dossier->name)), 'quad-news/' . $_dossier->machine_name);
    }
  }
  $moreactu = '<div class="prmore">' . $l . '</div>';
  $quadnews_content = array_merge(
    lqdn_latest_nodes_list_items('actu', $_dossier, $_complete, 0, 'complete_item'),
    lqdn_latest_nodes_list_items('actu', $_dossier, ($_nb - $_complete), $_complete),
    array($moreactu)    
    );

  if (empty($_dossier)) {
    if ($language->language=="en") {
      $l = l("Read full press reviews", 'press-review');
    }
    else {
      $l = l("Toute la revue de presse", 'revue-de-presse');
    }
  }
  else {
    if ($language->language=="en") {
      $l = l(t("Read full press review about @dossier", array('@dossier' => $_dossier->name)), 'press-review/' . $_dossier->machine_name);
    }
    else {
      $l = l(t("Toute la revue de presse sur @dossier", array('@dossier' => $_dossier->name)), 'press-review/' . $_dossier->machine_name);

    }
  }
  $morepr = '<div class="prmore">' . $l . '</div>';
  $pressreview_content = array_merge(
    lqdn_latest_nodes_list_items('rp', $_dossier, $_complete, 0, 'complete_item'),
    lqdn_latest_nodes_list_items('rp', $_dossier, ($_nb - $_complete), $_complete),
    array($morepr)
    );
  $content = array(
    (object)array(
      'title' => t("Quad'news"),
      'content' => $quadnews_content,
      'id' => 'quadnews',
    ),
    (object)array(
      'title' => t("Press review"),
      'content' => $pressreview_content,
      'id' => 'pressreview',
    ),
  );
  return theme('tab_switcher', $content);
}

/**
 * Implementation of hook_theme().
 */
function lqdn_latest_nodes_theme() {
  return array(
    'tab_switcher' => array(
      'template' => 'tab-switcher',
      'arguments' => array(
        'content' => NULL,
      ),
    ),
    'simple_item' => array(
      'template' => 'simple-item',
      'arguments' => array(
        'node' => NULL,
      ),
    ),
    'complete_item' => array(
      'template' => 'complete-item',
      'arguments' => array(
        'node' => NULL,
      ),
    ),
  );
}