Skip to content

Commit

Permalink
Moved responsive images rewrite to sl_images
Browse files Browse the repository at this point in the history
Added responsive images support to sl_articles/sla_articles
Fixed attachments display in sl_articles
  • Loading branch information
lowki committed May 12, 2013
1 parent cefad92 commit 834d032
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .htaccess
Expand Up @@ -25,4 +25,4 @@ RewriteRule article-([0-9]+).* index.php?mod=sl_articles&id=$1 [L]
#php_value error_reporting 30719

#Rewrite responsive images only to show_image script
RewriteRule responsive-(.*\.(?:jpe?g|gif|png|JPE?G|GIF|PNG))(?:/(\d*\w*))?(?:/(\d*\w*))? core/common/class/functions/includes/show_image.php?url=$1&width=$2&height=$3
RewriteRule ((?:medias/images|attachments/).*\.(?:jpe?g|gif|png|JPE?G|GIF|PNG))(?:/(\d*\w*))?(?:/(\d*\w*))? core/common/class/functions/includes/show_image.php?url=$1&width=$2&height=$3
1 change: 1 addition & 0 deletions admin/modules/sla_articles/languages/fr.php
Expand Up @@ -41,4 +41,5 @@
define("ARTICLES_UNPUBLISH_DATE", "Date de dépublication");
define("ARTICLES_UNPUBLISH_TIME", "Heure de dépublication");
define("ARTICLES_PERMANENT", "Article permanent");
define("ARTICLES_RESPONSIVE_IMAGES", "Adapter les images à l'écran.");
?>
6 changes: 4 additions & 2 deletions admin/modules/sla_articles/models/articles.php
Expand Up @@ -159,6 +159,7 @@ public function save_item($id,$values){
id_user='".$_SESSION["id_user"]."',
title='".$values["title"]."',
content='".$values["content"]."',
responsive_images='".$values["responsive_images"]."',
enabled='".$values["enabled"]."'
WHERE id='".$values["id"]."'");

Expand All @@ -174,8 +175,8 @@ public function save_item($id,$values){
$values=$this->slash->database->escapeArray($values);
$this->slash->database->setQuery("
INSERT INTO ".$this->slash->db_prefix."articles
(id,id_user,title,content,created_date,enabled) value
('','".$_SESSION["id_user"]."','".$values["title"]."','".$values["content"]."','".date ("Y-m-d H:i:s", time())."','".$values["enabled"]."')");
(id,id_user,title,content,responsive_images,created_date,enabled) value
('','".$_SESSION["id_user"]."','".$values["title"]."','".$values["content"]."','".$values["responsive_images"]."','".date ("Y-m-d H:i:s", time())."','".$values["enabled"]."')");
if (!$this->slash->database->execute()) {
$this->slash->show_fatal_error("QUERY_ERROR",$this->slash->database->getError());
}
Expand Down Expand Up @@ -223,6 +224,7 @@ public function recovery_fields() {
$obj["categories"] = $this->slash->sl_param($this->controller->module_name."_obj2","POST");
$obj["content"] = $this->slash->sl_param($this->controller->module_name."_obj3","POST");
$obj["enabled"] = $this->slash->sl_param($this->controller->module_name."_obj5","POST");
$obj["responsive_images"] = $this->slash->sl_param($this->controller->module_name."_obj6","POST");
if (!is_array($obj["categories"])) {$obj["categories"] = null;}

return $obj;
Expand Down
5 changes: 5 additions & 0 deletions admin/modules/sla_articles/views/default/view.php
Expand Up @@ -190,6 +190,11 @@ public function show_form ($id=0, $values=null, $errors=null) {
sl_form::br(2);
sl_form::ckeditor($mn,3,array("value"=>$values["content"]));
sl_form::br(2);

sl_form::title($this->slash->trad_word("ARTICLES_RESPONSIVE_IMAGES")." : ");
sl_form::checkbox($mn,6,array("value" => $values["responsive_images"]));
sl_form::br(2);


sl_form::title($this->slash->trad_word("FILES_ATTACHMENTS")." : ");
sl_form::br(2);
Expand Down
24 changes: 23 additions & 1 deletion core/common/class/functions/includes/sl_images.php
Expand Up @@ -166,7 +166,29 @@ public static function show_image($url,$max_width=9999,$max_height=9999){
}

}

/**
* Rewrite img tags in html code, to serve responsive markup
* @param $content:String Html content
* @param $widths:Array Widths to serve with associated breaking points default: ((180,0),(400,375),(800,480),(1000,768))
*/
public static function set_responsive($content,$widths = null){
require_once "core/plugins/simple_html_dom/simple_html_dom.php";
$dom = new simple_html_dom();
$dom->load($content);
if($widths === null) $widths = array(array(180,0),
array(400,375),
array(800,480),
array(1000,768));
foreach ($dom->find("img") as $img) {
$img->outertext = "<div data-picture data-alt=\"".$img->alt."\"".(isset($img->attr['style']) ? " data-style=\"".$img->attr['style']."\"":"").(isset($img->attr['class']) ? " data-class=\"".$img->attr['class']."\"":"").">\n";
foreach ($widths as $width) {
$img->outertext .= "<div data-src=\"".$img->src."/".$width[0]."\" data-media=\"(min-width: ".$width[1]."px)\"></div>\n";
}
$img->outertext .= "<noscript>".$img->outertext."</noscript>\n
</div>\n";
}
return $dom->outertext;
}

}

Expand Down
14 changes: 7 additions & 7 deletions modules/sl_articles/models/articles.php
Expand Up @@ -41,7 +41,9 @@ public function load_article($id){
$this->slash->show_fatal_error("QUERY_ERROR",$this->slash->database->getError());
}
$row = $this->slash->database->fetch("ASSOC");
if($row['responsive_images']) $row['content'] = sl_images::set_responsive($row['content']);
$row["attachments"] = $this->load_attachments($id);
var_dump($row);
return $row;
} else {
return NULL;
Expand All @@ -59,13 +61,11 @@ public function load_attachments($id,$where=""){
$attachments = array();

$i=0;
$result = mysql_query("select * from ".$this->slash->database_prefix."attachments where id_module = '".$id_module."' and id_element = '".$id."' and state = 1 ".$where." order by position asc ");
while($row = mysql_fetch_array($result))
{
$attachments[$i]["filename"] = $row["filename"];
$attachments[$i]["id_field"] = $row["id_field"];
$i++;
}
$this->slash->database->setQuery("select * from ".$this->slash->database_prefix."attachments where id_module = '".$id_module."' and id_element = '".$id."' and state = 1 ".$where." order by position asc ");
if (!$this->slash->database->execute()) {
$this->slash->show_fatal_error("QUERY_ERROR",$this->slash->database->getError());
}
$attachments = $this->slash->database->fetchAll("ASSOC");

return $attachments;
}
Expand Down
6 changes: 2 additions & 4 deletions modules/sl_articles/views/default/view.php
Expand Up @@ -56,12 +56,10 @@ public function show_article($row_article) {

echo "<div class='tp_page_top'></div><div class='tp_page'>".$row_article["content"]."</div><div class='tp_page_bottom'></div>";

if (count($row_article["attachments"]) > 0) {
$url = "medias/attachments/sl_articles/".$row_article["id"]."/".$row_article["attachments"][0]["filename"];
foreach ($row_article["attachments"] as $attachment) {
$url = "medias/attachments/sl_articles/".$row_article["id"]."/".$attachment["filename"];
echo sl_images::show_image($url);
}


}


Expand Down
17 changes: 1 addition & 16 deletions modules/sl_pages/models/pages.php
Expand Up @@ -25,7 +25,6 @@
* @{
*/
require "core/plugins/simple_html_dom/simple_html_dom.php";

class pages extends slModel implements iModel{

Expand All @@ -41,28 +40,14 @@ public function load_page($id){
$this->slash->show_fatal_error("QUERY_ERROR",$this->slash->database->getError());
}
$row = $this->slash->database->fetch("ASSOC");
if($row['responsive_images']) $row['content'] = $this->rewrite_img($row['content']);
if($row['responsive_images']) $row['content'] = sl_images::set_responsive($row['content']);
return $row;
} else {
return NULL;
}

}

public function rewrite_img($content){
$dom = new simple_html_dom();
$dom->load($content);
foreach ($dom->find("img") as $img) {
$img->outertext = "<div data-picture data-alt=\"".$img->alt."\"".(isset($img->attr['style']) ? " data-style=\"".$img->attr['style']:"").(isset($img->attr['class']) ? " data-class=\"".$img->attr['class']:"").">\n
<div data-src=\"responsive-".$img->src."/180\"></div>\n
<div data-src=\"responsive-".$img->src."/375\" data-media=\"(min-width: 400px)\"></div>\n
<div data-src=\"responsive-".$img->src."/480\" data-media=\"(min-width: 800px)\"></div>\n
<div data-src=\"responsive-".$img->src."/768\" data-media=\"(min-width: 1000px)\"></div>\n
<noscript>".$img->outertext."</noscript>\n
</div>";
}
return $dom->outertext;
}

/*
public function load_attachments($id,$id_module,$where){
Expand Down
1 change: 1 addition & 0 deletions setup/sql/slashcms.sql
Expand Up @@ -76,6 +76,7 @@ CREATE TABLE IF NOT EXISTS `bdd_prefix_articles` (
`id_user` int(11) unsigned NOT NULL,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
`responsive_images` tinyint(1) unsigned NOT NULL,
`alias` varchar(255) NOT NULL,
`meta_description` varchar(255) NOT NULL,
`meta_keywords` varchar(255) NOT NULL,
Expand Down

0 comments on commit 834d032

Please sign in to comment.