JFIF # -$)%7&).0444#9?92>-240 5+#+4;224;652252222225222220222522225222522;2225222222"B!1AQa"q2B#R3br5CS/!12AQ"aRq#3 ??;}q7:bd%Ո>L8/$rsrQٷf=+e: Rb0Z6pN۰7b 1S`JAT K,-֥7(bNRb9CsD/s,9?}+KA]`,EΞ@@ 3ukq14""tD@D@D@D@D@D@D@D@D@D@D@ȓ|:^Yw-)G%AI/9pwVSнm@6=g7AA5tg18gj>F'J,{C3(q<*#AzX?[$va:Q4SԽ7Uԥ&,c}nF;3mO$DN}RySm\*I"}b%7GLj;gp{{FYs(p_xOJFtStǸMU蓰s95"#H'Uq>7F5[}>v%'Y,&CqMzn}m*Xo vl˳hrӦ V)))g`7$sz^%I-1leE]y%݉>?f}( *BNNñ𜤢S[i'T1 ӥԊ>NlHď~)pKw1.UsD LI/k]Sr\r=ߴMAZNKi+P}| qBS*G*z:Imk\_|l6A7߉H\z0賈'Zt_\u>4 {\#O[ERxzLvP wOLT C0ȴ]BAʷ7uNNINS,(DDDDDDDL8MY݂N$ dMK׭i2FesmNQ=?omKv]OVl^@&pɴ[t5+E`oy.E]Ϳ}$g(7y7&X+imcT\(cHɤ|=. C =yȗXʧpv=&cX*[X_i4 GtfFՓnbMjR@ thv4LO I0zlU-_*G!cH9`nԿ \k-~rS*c[}9]qbi~+%)(h($ s;dՒG_\ё[Q,plq!pEſA RZU0*\n]a~Md_3EZ { &8e:jR*dAkyۛs\B˞0Z5%6e`3;0slSx+Xȇ"*ozkE"vܬWاQ8r @ m5$ [/KNFycgrۑ@ {""""""|xd*@s7o~7BSG|܎vøGtЍL١ѬnK/, f~^~l/Ij+!JI'^;{˚*hӤJarʮ)ڱ[P^$;%.V FLJW̔?2ԭUpJe,~b%iW Yhz̻FAl|3ln"M4kM@$2wmͣp8JY)ݬ.]3vԩİ(P*Tb/1FXTg KŮ*C9jE[69d!GZȩMu!5`H\Cp"=wSAmJjCn&/*Q[kQ~b"zΕ~)aA(2EZ0(FÑp.66_φk}T5 YdRarK ɽLSj"SnR-N-Mz~F^Igb Jq(~X fH'Ӵp5_HN(ܰ,Ȍ䶛DK%a~?FuI}"p=U+j}'p&I_ɑ-x!IٮM:w|q;9M?.6x:ODѪ̬zTL`t^?8xJ$ Q cL4d/_xy ˔ SPGNgwSrrS/`5ӧKj ,hTpI=LѦ(,Pc4*4iESO?5sMz<`&_bsTO)fkX[ xqq::h9ifVۉ\_R }JVg~Jzm`(]:O &6IOghX6+HM 7X]RkUr{HL-"< >~28b{[><@6gF5&\1̹nVŕonZM7 (SF$l\sM];owE+IֹȫzɲDߌPcMQMG)b,N ;*!uo&rHT`s^7įĴz0?P&Ҫ3]@H:hڢFҢM~p{&0s?k}+Ι9׵mw >?"fs+Odٯ̌m(R9T:UpbkW=F*ZQh urk8C8@ҧeUԀyKS '.UP,NBcpFS6n=AJl*7 4<(XY_Cda/D=()b,{yHL>[jrǹ7#M7fO`o/w]GȈEU2f\?7a)#봙݂͠SEg>VRdPfF@PV"Ꮷ_(qCJG_0?1[% NKu$7&ۭ ߡ26U$`/ 3ES:/nek |\tmSg5 س}6/qDT "(*sP4SrX)%T(6y%_ Z9<%]B}oyyY"]76*U*vjijw i3D̍IS \Jnn9ۋ>%o;~)5u56槡'z* B5#5

5#a`,>1TW{Xɘ}G4"ҕ4z5F>e6*[\;%*U0LUUr2cpnݢkɜY͌3+bG0#el۴oe,,jO*M1X/3z)W^,p>s{ İQs:ޝd|w :fIe$~+ajXjnT80'S>KIUP&kNϒT=XlȞNڞ]Yz_K[Qׂގ\gq!nB@IoG *l;_뼳\RUeэkm)qh傢5KNz٘6ba:671k{  $N vfN]S7gxg=VjG;wBx t~l/"ʭl=ԝ6n[Dٛ]@"x)# E):\8Bvkcpv4O*;coJ?4ªMCA'.\zVð'w1USݻSlTyj/ gʕ,:S')ܴ]7!A^b%P׶ٮհU3 o\}XTp,e 597n}dk6UFrVǧ3qaR:BWn>Ѻ}oxKӦK)kܑKL tCs1#?升 v{r:u)?#ZxM=ڝYـ#e}JHBGTG>GsܞG2+~R̅Hש)$[*Hfx-ugx({ I7λwvYm~ |e'X#db@hW,0H8*J5AъA`;jȊY*&sh8Jn]"M>l3z%Րsy=Um'qF sX %,Uv|0W`Gzcy*V0'3R`5ޓ Hڙ>PWbw7;)[U(:krm>/ QU+)P>Hm!r -evY>wT7ԝe)^6_SN⚓ϫ('?2Sj5,[پd|+_Pv'[]t'mΝ2l}z/dz^E|"'J qED)R2ƂSg`9Ոu5~ d!G%>M6%pdcP-P L`ϼTQnA_,24G GneRn,XnߕSzV$ReBfZuE ,Z(yi?vO!clOYA [; c I|vCom+Hꡤ\eaӴ;XS|v4%FcϷAQ[yϢ_s+Ơ&pt}=%^Sb"#gĀ'[ oAUPzr;ȔZTy4t>f種ً>T؟GRgC^-WЖukS,G LV$ܱO餰%cp)[*X_v$@DDӢ3bE-V0֍?zySyadd\ j5": Bxi?;3a]1]ZFD澙rc|8uz/ CȎ3UTqb4'ҥX 6KʖYT2fPe$6 lGzSQTP} OL1q^*rxջQ_K?'?=V MR K IS HERE

MRKShell
Server IP : 104.21.73.244  /  Your IP : 104.23.243.234
Web Server : nginx/1.14.1
System : Linux comtuc2-s-2vcpu-8gb-160gb-intel-nyc3 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64
User : nginx ( 991)
PHP Version : 7.2.34
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /var/www/comunicacion/produccion/hitadmin/application/models/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME SHELL ]     

Current File : /var/www/comunicacion/produccion/hitadmin/application/models/Nota_modelo.php
<?php

class Nota_modelo extends CI_Model {

    public function listado($start = "", $limit = "", $filtro = "", $orden = "") {
        $consulta = "SELECT n.id, TRIM(n.titulo) as titulo, n.habilitada, n.edicion_id, n.norank, 
		 n.fechaAlta, n.permisocoment, DATE_FORMAT(n.fechamod, '%H:%i') as hora, n.um, n.seleccionada,
		 DATE_FORMAT(n.fechamod, '%Y-%m-%d') as fecha,
		 (select visitas from nota_visitas where nota_id = n.id limit 1) as visitas, n.mayor
		 FROM nota n 
		 WHERE (n.lector =0 or n.lector is null) and n.edicion_id = " . $filtro['edicion_id'];

        if ($filtro['n.titulo'] != "") {
            $consulta .= " and (n.titulo like '%" . $filtro['n.titulo'] . "%'";
            if ($filtro['n.id'] != "" and is_numeric($filtro['n.id']))
                $consulta .= " or n.id = " . $filtro['n.id'];
            $consulta .= ")";
        }

        if ($orden <> "") {
            $consulta .= " ORDER BY $orden";
        }

        if ($start <> "" && $limit <> "") {
            $consulta .= " LIMIT $start,$limit";
        }
        //echo $consulta; die();
        $query = $this->db->query($consulta);
        $num = $query->num_rows();
        $res = $query->result_array();
        $cant = $this->cant_notas($filtro);
        $paso = 0;
        $edicion_id_validar = $filtro['edicion_id'] - 5;
        if ($num > 0) {
            $i = 0;
            $nuevo = array();
            foreach ($res as $k => $v) {
                $res[$k]['seccion'] = $this->dame_secciones($v['id'], '', ',');

                if ($v['edicion_id'] <= $edicion_id_validar)
                    $res[$k]['comentarios'] = $this->dameCantCom($v['id']);
                else
                    $res[$k]['comentarios'] = $this->dameCantComMin($v['id']);

                /* if($filtro['seccion']!="" && $filtro['seccion']!="Todos"){
                  if ($res[$k]['seccion']==$filtro['seccion'])
                  {
                  $nuevo[$i]['id']=$v['id'];
                  $nuevo[$i]['titulo']=$v['titulo'];
                  $nuevo[$i]['habilitada']=$v['habilitada'];
                  $nuevo[$i]['edicion_id']=$v['edicion_id'];
                  $nuevo[$i]['norank']=$v['norank'];
                  $nuevo[$i]['fechaAlta']=$v['fechaAlta'];
                  $nuevo[$i]['permisocoment']=$v['permisocoment'];
                  $nuevo[$i]['hora']=$v['hora'];
                  $nuevo[$i]['um']=$v['um'];
                  $nuevo[$i]['seleccionada']=$v['seleccionada'];
                  $nuevo[$i]['fecha']=$v['fecha'];
                  $nuevo[$i]['visitas']=$v['visitas'];
                  $nuevo[$i]['mayor']=$v['mayor'];
                  $nuevo[$i]['seccion']=$nuevo[$k]['seccion'];
                  $nuevo[$i]['comentarios'] =$nuevo[$k]['comentarios'];
                  $i++;
                  }
                  }else{
                  $nuevo[$i]['id']=$v['id'];
                  $nuevo[$i]['titulo']=$v['titulo'];
                  $nuevo[$i]['habilitada']=$v['habilitada'];
                  $nuevo[$i]['edicion_id']=$v['edicion_id'];
                  $nuevo[$i]['norank']=$v['norank'];
                  $nuevo[$i]['fechaAlta']=$v['fechaAlta'];
                  $nuevo[$i]['permisocoment']=$v['permisocoment'];
                  $nuevo[$i]['hora']=$v['hora'];
                  $nuevo[$i]['um']=$v['um'];
                  $nuevo[$i]['seleccionada']=$v['seleccionada'];
                  $nuevo[$i]['fecha']=$v['fecha'];
                  $nuevo[$i]['visitas']=$v['visitas'];
                  $nuevo[$i]['mayor']=$v['mayor'];
                  $nuevo[$i]['seccion']=$nuevo[$k]['seccion'];
                  $nuevo[$i]['comentarios'] =$nuevo[$k]['comentarios'];
                  $i++;
                  } */
                /*
                  if ($res[$k]['seccion']=="Periodismo ciudadano")
                  {
                  unset($res[$k]);
                  $cant = $cant-1;
                  $paso = 1;
                  }
                  else
                 */
                //	if($v['edicion_id']<= $edicion_id_validar)
                //	else 
                //		$res[$k]['comentarios'] = $this->dameCantComMin($v['id']);	
                //$res[$k]['visitas'] = $this->dameCantVisitas($v['id']);
                //$res[$k]['tema']=$this->dame_temas($v['id'],'',',');
            }

            //echo print_r($nuevo);
            //$cant=count($res);
            return '({"total":"' . $cant . '","rows":' . json_encode($res) . '})';
        } else
            return '({"total":"0","rows":""})';
    }

    public function extraer_string($str, $left, $right) {
        $str = substr(stristr($str, $left), strlen($left));
        $leftLen = strlen(stristr($str, $right));
        $leftLen = $leftLen ? -($leftLen) : strlen($str);
        $str = substr($str, 0, $leftLen);
        return $str;
    }

    public function traer_datos_nota($nota_id, $front = 0) {
        $this->db->select("n.id,TRIM(n.titulo) AS titulo, TRIM(n.antetitulo) AS antetitulo,n.sumario as bajada,
		n.texto, n.habilitada, n.edicion_id, n.um, n.fechaAlta, n.permisocoment as perm_coment, n.url, n.origen, 
		n.agregado, n.vista, n.mayor, n.scroll_inactivo, n.video_en_nota, n.patrocinado, n.acceso_cultural, n.id_seccion as secciones, n.id_subseccion as subsecciones");
        $this->db->from("nota n");
        $this->db->where('n.id', $nota_id);
        $query = $this->db->get();
        $num = $query->num_rows();
        $res = $query->result_array();
        if ($num > 0) {
            foreach ($res as $k => $v) {
                $res[$k]['periodistas'] = $this->dame_id_periodistas($v['id'], '', ',');
                $verifica = explode("xto>", $res[$k]['texto']);
                if (count($verifica) > 1) {
                    $texto = $this->extraer_string($res[$k]['texto'], "<texto>", "</texto>");
                    $tituloagregado = $this->extraer_string($res[$k]['texto'], "<tituloagregado>", "</tituloagregado>");
                    $textoagregado = $this->extraer_string($res[$k]['texto'], "<textoagregado>", "</textoagregado>");

                    //agregado para ver notas sion simbolos html en caso que se carguen con panel movil
                    $texto = html_entity_decode($texto);
                    $tituloagregado = html_entity_decode($tituloagregado);
                    $textoagregado = html_entity_decode($textoagregado);
                    $anteTitulo = $res[$k]['antetitulo'];
                    $res[$k]['antetitulo'] = $anteTitulo;
                    $titulo = $res[$k]['titulo'];
                    $res[$k]['titulo'] = html_entity_decode($titulo);
                    //******************************************************

                    if ($tituloagregado)
                        $res[$k]['tituloagregado'] = $tituloagregado;
                    if ($textoagregado)
                        $res[$k]['textoagregado'] = $textoagregado;
                    $res[$k]['texto'] = $texto;
                }
                $verifica_sumario = explode("umario>", $res[$k]['bajada']);
                if (count($verifica_sumario) > 1) {
                    $sumario = $this->extraer_string($res[$k]['bajada'], "<sumario>", "</sumario>");
                    $res[$k]['bajada'] = $sumario;
                }
                //agregado para ver notas sion simbolos html en caso que se carguen con panel movil
                else {
                    $sumario = $res[$k]['bajada'];
                    $res[$k]['bajada'] = html_entity_decode($sumario);
                }
                //******************************************************
            }
            return $res;
        } else
            return '({"total":"0","rows":""})';
    }

    public function traer_datos_nota_minimos($nota_id) {

        $sql = "SELECT n.edicion_id,n.lector,n.usuario_id,n.id, n.titulo,
		(
		   select t.nombre from tag t inner join 
		   (select tag_id from relacion_tag 
		    where nota_id = " . $nota_id . " and tipo = 5
		   ) as temp on (temp.tag_id = t.id) 
		   limit 1
		) as seccion
		FROM nota n 
		WHERE n.id = " . $nota_id;

        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            $nota = $res[0];

            return $nota;
        } else
            return false;
    }

    public function traer_datos_nota_minimizado($nota_id) {
        $this->db->select("n.id,TRIM(n.titulo) AS titulo, 
		n.texto, n.habilitada, n.edicion_id, n.url");
        $this->db->from("nota n");
        $this->db->where('n.id', $nota_id);
        $query = $this->db->get();
        $num = $query->num_rows();
        $res = $query->result_array();
        if ($num > 0) {
            foreach ($res as $k => $v) {
                $res[$k]['secciones'] = $this->dame_id_secciones($v['id'], '', ',');
                $verifica = explode("xto>", $res[$k]['texto']);
                if (count($verifica) > 1) {
                    $texto = $this->extraer_string($res[$k]['texto'], "<texto>", "</texto>");
                    $tituloagregado = $this->extraer_string($res[$k]['texto'], "<tituloagregado>", "</tituloagregado>");
                    $textoagregado = $this->extraer_string($res[$k]['texto'], "<textoagregado>", "</textoagregado>");
                    if ($tituloagregado)
                        $res[$k]['tituloagregado'] = $tituloagregado;
                    if ($textoagregado)
                        $res[$k]['textoagregado'] = $textoagregado;
                    $res[$k]['texto'] = $texto;
                }
            }
            return $res;
        } else
            return '({"total":"0","rows":""})';
    }

    public function traer_datos_nota_para_solr($nota_id, $milenium = 0) {
        $sql = "select CONCAT(e.fecha, 'T09:00:00Z') AS fecha,n.sumario,n.texto,n.edicion_id,TRIM(n.titulo) AS titulo, 
		n.antetitulo, n.habilitada 
			from nota n
			inner join edicion e on e.id=n.edicion_id
			where n.id = " . $nota_id;

        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            foreach ($res as $k => $v) {
                //agrego secciones
                $arr = $this->dame_array_secciones($nota_id, '', '');
                if (is_array($arr)) {
                    foreach ($arr as $k2 => $v2) {
                        $res[$k]['tag_id'][] = $v2['id'];
                        $res[$k]['tag'][] = $v2['nombre'];
                        $res[$k]['tag_tipo'][] = 5;
                    }
                }

                if (!$milenium) {
                    //agrego temas
                    $arr = $this->dame_array_temas($nota_id, '', '');
                    if (is_array($arr)) {
                        foreach ($arr as $k2 => $v2) {
                            $res[$k]['tag_id'][] = $v2['id'];
                            $res[$k]['tag'][] = $v2['nombre'];
                            $res[$k]['tag_tipo'][] = 3;
                        }
                    }
                    //agrego temas
                    $arr = $this->dame_array_tags($nota_id, '', '');
                    if (is_array($arr)) {
                        foreach ($arr as $k2 => $v2) {
                            $res[$k]['tag_id'][] = $v2['id'];
                            $res[$k]['tag'][] = $v2['nombre'];
                            $res[$k]['tag_tipo'][] = $v2['tipo_id'];
                        }
                    }
                    //agrego firmas
                    $arr = $this->dame_array_periodistas($nota_id);
                    if (is_array($arr)) {
                        foreach ($arr as $k2 => $v2) {
                            $res[$k]['id_firma'][] = $v2['id'];
                            $res[$k]['firma'][] = $v2['nombre'];
                        }
                    }
                }

                $verifica = explode("xto>", $res[$k]['texto']);
                if (count($verifica) > 1) {
                    $texto = $this->extraer_string($res[$k]['texto'], "<texto>", "</texto>");
                    $tituloagregado = $this->extraer_string($res[$k]['texto'], "<tituloagregado>", "</tituloagregado>");
                    $textoagregado = $this->extraer_string($res[$k]['texto'], "<textoagregado>", "</textoagregado>");
                    $res[$k]['texto'] = $texto;
                }
            }
            return $res[0];
        } else
            return false;
    }

    public function traer_datos_nota_blog_para_solr($nota_id) {
        $sql = "select date(n.fechaAlta) AS fecha,n.sumario,n.texto,n.edicion_id,TRIM(n.titulo) AS titulo, 
		n.antetitulo, n.habilitada 
			from nota n
			where n.id = " . $nota_id;

        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            foreach ($res as $k => $v) {
                $res[$k]['fecha'] = $res[$k]['fecha'] . 'T09:00:00Z';
                //agrego secciones
                $arr = $this->dame_array_secciones($nota_id, '', '');
                if (is_array($arr)) {
                    foreach ($arr as $k2 => $v2) {
                        $res[$k]['tag_id'][] = $v2['id'];
                        $res[$k]['tag'][] = $v2['nombre'];
                        $res[$k]['tag_tipo'][] = 5;
                    }
                }

                //agrego temas
                $arr = $this->dame_array_temas($nota_id, '', '');
                if (is_array($arr)) {
                    foreach ($arr as $k2 => $v2) {
                        $res[$k]['tag_id'][] = $v2['id'];
                        $res[$k]['tag'][] = $v2['nombre'];
                        $res[$k]['tag_tipo'][] = 3;
                    }
                }
                //agrego temas
                $arr = $this->dame_array_tags($nota_id, '', '');
                if (is_array($arr)) {
                    foreach ($arr as $k2 => $v2) {
                        $res[$k]['tag_id'][] = $v2['id'];
                        $res[$k]['tag'][] = $v2['nombre'];
                        $res[$k]['tag_tipo'][] = $v2['tipo_id'];
                    }
                }
                //agrego firmas
                $arr = $this->dame_array_periodistas($nota_id);
                if (is_array($arr)) {
                    foreach ($arr as $k2 => $v2) {
                        $res[$k]['id_firma'][] = $v2['id'];
                        $res[$k]['firma'][] = $v2['nombre'];
                    }
                }
            }
            return $res[0];
        } else
            return false;
    }

    public function traer_tags_nota($nota_id, $nombre = "tagsrf") {
        $this->db->select("n.id");
        $this->db->from("nota n");
        $this->db->where('n.id', $nota_id);
        $query = $this->db->get();
        $num = $query->num_rows();
        $res = $query->result_array();
        if ($num > 0) {
            foreach ($res as $k => $v) {
                $res[$k][$nombre] = $this->dame_id_tags($v['id'], '', ',');
            }
            echo "{success: true, msg: 'Se cargaron los datos con &eacute;xito.', data:" . str_replace(']', '', str_replace('[', '', json_encode($res))) . "}";
            //die();
        } else
            return '({"total":"0","rows":""})';
    }

    public function cant_notas($filtro = "") {
        $consulta = "SELECT n.id FROM nota n WHERE (n.lector =0 or n.lector is null) and edicion_id = " . $filtro['edicion_id'];

        if ($filtro['n.titulo'] != "") {
            $consulta .= " and (n.titulo like '%" . $filtro['n.titulo'] . "%'";
            if ($filtro['n.id'] != "" and is_numeric($filtro['n.id']))
                $consulta .= " or n.id = " . $filtro['n.id'];
            $consulta .= ")";
        }

        $query = $this->db->query($consulta);
        $num = $query->num_rows();
        return $num;
    }

    public function dameCantCom($nota_id) {
        $sql = "select count(id) as cant from comentario where objeto_id = " . $nota_id . " and tipo = 32";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0]['cant'];
        } else
            return 0;
    }

    public function dameCantComMin($nota_id) {
        $sql = "select count(id) as cant from comentario_min where objeto_id = " . $nota_id . " and tipo = 32";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0]['cant'];
        } else
            return 0;
    }

    public function dameCantVisitas($nota_id) {
        $sql = "select visitas from nota_visitas where nota_id = " . $nota_id;
        $sql .= " limit 1";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0]['visitas'];
        } else
            return 0;
    }

    public function dame_secciones($id_nota, $pre, $post) {


        /* $this->db->select("s.nombre");
          $this->db->from("relacion r");
          $this->db->join("seccion s","s.id=r.objeto_id","inner");
          $this->db->where("r.nota_id",$id_nota);
          $this->db->where("r.tipo",8);
          $query = $this->db->get(); */
        $sql = "select t.nombre from tag t 
		inner join (select tag_id from relacion_tag 
		where tipo=5 and nota_id = " . $id_nota . ") as temp_sec on (temp_sec.tag_id = t.id)";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        $concat = "";
        if (count($res) > 0) {
            $i = 0;
            foreach ($res as $k => $v) {
                if ($i == 0) {
                    $i = 1;
                    $concat .= $pre . $v['nombre'];
                } else
                    $concat .= $post . $pre . $v['nombre'];
            }
        }
        return $concat;
    }

    public function dame_id_secciones($id_nota, $pre, $post, $padre_id = "") {

        /* $this->db->select("s.id as nombre");
          $this->db->from("relacion r");
          $this->db->join("seccion s","s.id=r.objeto_id","inner");
          $this->db->where("r.nota_id",$id_nota);
          $this->db->where("r.tipo",8);
          $query = $this->db->get(); */
        $sql = "select t.id as nombre 
		from tag t 
		inner join 
		(select tag_id from relacion_tag
		where tipo=5 and nota_id = " . $id_nota . ") as temp_sec on (temp_sec.tag_id = t.id)";

        if ($padre_id == 0)
            $sql .= " where padre_id=0";

        $query = $this->db->query($sql);
        $res = $query->result_array();
        $concat = "";
        if (count($res) > 0) {
            $i = 0;
            foreach ($res as $k => $v) {
                if ($i == 0) {
                    $i = 1;
                    $concat .= $pre . $v['nombre'];
                } else
                    $concat .= $post . $pre . $v['nombre'];
            }
        }
        return $concat;
    }

    public function dame_array_secciones($id_nota, $pre, $post) {

        /* $this->db->select("s.id, s.nombre, s.template");
          $this->db->from("relacion r");
          $this->db->join("seccion s","s.id=r.objeto_id","inner");
          $this->db->where("r.nota_id",$id_nota);
          $this->db->where("r.tipo",8);
          $query = $this->db->get(); */
        $sql = "select t.id, t.nombre, t.template 
		from tag t 
		inner join (select tag_id from relacion_tag 
		where tipo=5 and nota_id = " . $id_nota . ") as temp_sec on (temp_sec.tag_id = t.id)";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res;
    }

    public function dame_array_periodistas($id_nota) {
        $query = $this->db->query("
			SELECT r.objeto_id as id,CONCAT(u.nombre,' ',u.apellido) as nombre FROM relacion r
			INNER JOIN usuario u ON u.id = r.objeto_id
			WHERE r.nota_id = '" . $id_nota . "'
			AND r.tipo = 4");
        $res = $query->result_array();
        return $res;
    }

    public function dame_temas($id_nota, $pre, $post) {

        /* $this->db->select("s.tema as nombre");
          $this->db->from("relacion r");
          $this->db->join("tema s","s.id=r.objeto_id","inner");
          $this->db->where("r.nota_id",$id_nota);
          $this->db->where("r.tipo",25);
          $query = $this->db->get(); */
        $sql = "select t.nombre from tag t 
		inner join (select tag_id from relacion_tag 
		where tipo=3 and nota_id = " . $id_nota . ") as temp_tem on (temp_tem.tag_id = t.id)";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        $concat = "";
        if (count($res) > 0) {
            $i = 0;
            foreach ($res as $k => $v) {
                if ($i == 0) {
                    $i = 1;
                    $concat .= $pre . $v['nombre'];
                } else
                    $concat .= $post . $pre . $v['nombre'];
            }
        }
        return $concat;
    }

    public function dame_id_temas($id_nota, $pre, $post) {

        /* $this->db->select("s.id as nombre");
          $this->db->from("relacion r");
          $this->db->join("tema s","s.id=r.objeto_id","inner");
          $this->db->where("r.nota_id",$id_nota);
          $this->db->where("r.tipo",25);
          $query = $this->db->get(); */
        $sql = "select t.id as nombre 
		from tag t 
		inner join (select tag_id from relacion_tag 
		where tipo=3 and nota_id = " . $id_nota . ") as temp_tem on (temp_tem.tag_id = t.id)";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        $concat = "";
        if (count($res) > 0) {
            $i = 0;
            foreach ($res as $k => $v) {
                if ($i == 0) {
                    $i = 1;
                    $concat .= $pre . $v['nombre'];
                } else
                    $concat .= $post . $pre . $v['nombre'];
            }
        }
        return $concat;
    }

    public function dame_array_temas($id_nota, $pre, $post) {

        /* $this->db->select("s.id, s.tema as nombre");
          $this->db->from("relacion r");
          $this->db->join("tema s","s.id=r.objeto_id","inner");
          $this->db->where("r.nota_id",$id_nota);
          $this->db->where("r.tipo",25);
          $query = $this->db->get(); */
        $sql = "select t.id, t.nombre 
		from tag t 
		inner join (select tag_id from relacion_tag 
		where tipo=3 and nota_id = " . $id_nota . ") as temp_tem on (temp_tem.tag_id = t.id)";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res;
    }

    public function dame_id_tags($id_nota, $pre = "", $post = "") {

        /* $this->db->select("s.id as nombre");
          $this->db->from("relacion r");
          $this->db->join("tags s","s.id=r.objeto_id","inner");
          $this->db->where("r.nota_id",$id_nota);
          $this->db->where("r.tipo",33);
          $query = $this->db->get();
          $res = $query->result_array();

          $concat="";
          if(count($res)>0)
          {
          $i = 0;
          foreach($res as $k => $v)
          {
          if ($i == 0)
          {
          $i = 1;
          $concat .=$pre.$v['nombre'];
          }
          else
          $concat .=$post.$pre.$v['nombre'];
          }
          }
          return $concat; */

        $sql = "select t.nombre,t.classora_id,t.tipo,t.peso 
		from tag t 
		inner join relacion_tag r on (r.tag_id = t.id and r.nota_id = " . $id_nota . ") 
		where t.tipo_id in (1,2,4)";
        $query = $this->db->query($sql);
        $tags = array();
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            foreach ($result as $reg) {
                $return['nombre'] = $reg['nombre'];
                $return['peso'] = $reg['peso'];
                $return['info'] = $reg['classora_id'] . "|" . $reg['nombre'] . "|" . $reg['tipo'] . "|" . $reg['peso'];
                $tags[] = $return;
            }
        }
        return $tags;
    }

    public function dame_array_tags($id_nota, $pre, $post) {
        $sql = "select t.id, t.nombre, t.tipo_id 
		from tag t 
		inner join (select tag_id from relacion_tag 
		where tipo in (1,2,4) and nota_id = " . $id_nota . ") as temp_tem on (temp_tem.tag_id = t.id)";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res;
    }

    //public function insertar_nota($array,$secciones,$temas,$periodistas="")
    public function insertar_nota($array, $secciones, $temas, $periodistas = "", $subsecciones = "") {
        $this->db->trans_begin();

        if (is_array($array)) {
            foreach ($array as $k => $v) {
                $this->db->set($k, $v);
            }
        }
        $this->db->set('fechaalta', date("Y-m-d H:i:s"));
        $this->db->set('fechamod', date("Y-m-d H:i:s"));
        $this->db->insert('nota');
        $last = $this->db->insert_id();

        /* Inserto registro en visitas */
        $sql = "insert into nota_visitas (nota_id, visitas, visitas_reg, enviadas) values ($last,0,0,0)";
        $this->db->query($sql);
        /* Fin visitas */

        /* if(is_array($tags))
          {
          foreach($tags as $tag)
          {
          $this->db->set('nota_id',$last);
          $this->db->set('objeto_id',$tag);
          $this->db->set('tipo',33);
          $this->db->set('orden',1);
          $this->db->insert('relacion');
          }
          } */
        if (is_array($secciones)) {
            $orden = 1;
            foreach ($secciones as $seccion) {
                /* $this->db->set('nota_id',$last);
                  $this->db->set('objeto_id',$seccion);
                  $this->db->set('tipo',8);
                  $this->db->set('orden',1);
                  $this->db->insert('relacion'); */
                $sql = "insert into relacion_tag (nota_id, tag_id, tipo, orden, fechaaud) values (" . $last . "," . $seccion . ",5," . $orden . ",now())";
                $this->db->query($sql);
                $orden ++;
            }
        }
        //subsecciones
        if (is_array($subsecciones)) {
            $orden = 1;
            foreach ($subsecciones as $subseccion) {
                if ($subseccion != "") {
                    /* $this->db->set('nota_id',$last);
                      $this->db->set('objeto_id',$seccion);
                      $this->db->set('tipo',8);
                      $this->db->set('orden',1);
                      $this->db->insert('relacion'); */
                    $sql = "insert into relacion_tag (nota_id, tag_id, tipo, orden, fechaaud) values (" . $last . "," . $subseccion . ",5," . $orden . ",now())";
                    $this->db->query($sql);
                    $orden ++;
                }
            }
        }
        //
        if (is_array($temas)) {
            $orden = 1;
            foreach ($temas as $tema) {
                if ($tema <> 0) {
                    /* $this->db->set('nota_id',$last);
                      $this->db->set('objeto_id',$tema);
                      $this->db->set('tipo',25);
                      $this->db->set('orden',1);
                      $this->db->insert('relacion'); */
                    $sql = "insert into relacion_tag (nota_id, tag_id, tipo, orden, fechaaud) values (" . $last . "," . $tema . ",3," . $orden . ",now())";
                    $this->db->query($sql);
                    $orden ++;
                }
            }
        }


        if (is_array($periodistas)) {
            $orden = 1;
            foreach ($periodistas as $per_id) {
                if ($per_id <> 0) {
                    $insert = "insert into relacion (nota_id, objeto_id, tipo, orden) values (" . $last . "," . $per_id . ",4," . $orden . ")";
                    $this->db->query($insert);
                    $orden ++;

                    /* $this->db->set('nota_id',$last);//last es el id de la nota
                      $this->db->set('objeto_id',$per_id);
                      $this->db->set('tipo',4);
                      $this->db->set('orden',$orden);
                      $this->db->insert('relacion');
                      $orden ++; */
                }
            }
        }


        $this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
            return $last;
        }
    }

    public function insertarNotaNumOro($datos, $seccion_id) {
        if ($this->db->insert("nota", $datos)) {
            $nota_id = $this->db->insert_id();
            $sql = "insert into nota_visitas (nota_id, visitas, visitas_reg, enviadas) values ($nota_id,0,0,0)";
            $this->db->query($sql);
            if ($seccion_id) {
                /* $this->db->set('nota_id',$nota_id);
                  $this->db->set('objeto_id',$seccion_id);
                  $this->db->set('tipo',8);
                  $this->db->set('orden',1);
                  $this->db->insert('relacion'); */
                $sql = "insert into relacion_tag (nota_id, tag_id, tipo, orden, fechaaud) values (" . $nota_id . "," . $seccion_id . ",5,1,now())";
                $this->db->query($sql);
            }
            return $nota_id;
        } else
            return false;
    }

    //subsecciones
    //public function editar_nota($array,$secciones,$temas,$id,$periodista="")
    public function editar_nota($array, $secciones, $temas, $id, $periodista = "", $subsecciones = "") {
        $this->db->trans_begin();

        $this->db->where('id', $id);
        //$array['fechamod'] = date('Y-m-d H:i:s');
        $this->db->update('nota', $array);

        /* $this->borrar_relacionados($id,33);
          if(is_array($tags))
          {
          foreach($tags as $tag)
          {
          $this->db->set('nota_id',$id);
          $this->db->set('objeto_id',$tag);
          $this->db->set('tipo',33);
          $this->db->set('orden',1);
          $this->db->insert('relacion');
          }
          }
         */

        $this->borrar_relacionados($id, 5);
        if (is_array($secciones)) {
            $orden = 1;
            foreach ($secciones as $seccion) {
                /* $this->db->set('nota_id',$id);
                  $this->db->set('objeto_id',$seccion);
                  $this->db->set('tipo',8);
                  $this->db->set('orden',1);
                  $this->db->insert('relacion'); */
                $sql = "insert into relacion_tag (nota_id, tag_id, tipo, orden, fechaaud) values (" . $id . "," . $seccion . ",5," . $orden . ",now())";
                $this->db->query($sql);
                $orden ++;
            }
        }
        //subsecciones
        //$this->borrar_relacionados($id,5);
        if (is_array($subsecciones)) {
            $orden = 1;
            foreach ($subsecciones as $subseccion) {
                if ($subseccion != "") {
                    /* $this->db->set('nota_id',$id);
                      $this->db->set('objeto_id',$seccion);
                      $this->db->set('tipo',8);
                      $this->db->set('orden',1);
                      $this->db->insert('relacion'); */
                    $sql = "insert into relacion_tag (nota_id, tag_id, tipo, orden, fechaaud) values (" . $id . "," . $subseccion . ",5," . $orden . ",now())";
                    $this->db->query($sql);
                    $orden ++;
                }
            }
        }
        ////
        $this->borrar_relacionados($id, 3);
        if (is_array($temas)) {
            $orden = 1;
            foreach ($temas as $tema) {
                /* $this->db->set('nota_id',$id);
                  $this->db->set('objeto_id',$tema);
                  $this->db->set('tipo',25);
                  $this->db->set('orden',1);
                  $this->db->insert('relacion'); */
                $sql = "insert into relacion_tag (nota_id, tag_id, tipo, orden, fechaaud) values (" . $id . "," . $tema . ",3," . $orden . ",now())";
                $this->db->query($sql);
                $orden ++;
            }
        }

        $this->borrar_periodistas_relacionados($id, 4);
        if (is_array($periodista)) {
            $orden = 1;
            foreach ($periodista as $per_id) {
                $insert = "insert into relacion (nota_id,objeto_id,tipo,orden)
						values (" . $id . "," . $per_id . ",4," . $orden . ")";

                $this->db->query($insert);
                /* $this->db->set('nota_id',$id);
                  $this->db->set('objeto_id',$per_id);
                  $this->db->set('tipo',4);
                  $this->db->set('orden',$orden);
                  $this->db->insert('relacion'); */
                $orden ++;
            }
        }

        $this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
            return 0;
        }
    }

    public function update($nota_id, $datos) {
        $this->db->where("id", $nota_id);
        if ($this->db->update("nota", $datos))
            return true;
        else
            return false;
    }

    public function update_con_secciones($nota_id, $datos, $secciones) {
        $this->db->where("id", $nota_id);
        if ($this->db->update("nota", $datos)) {
            if (is_array($secciones)) {
                $this->borrar_relacionados($nota_id, 5);
                $$tipo = 1;
                $orden = 1;
                foreach ($secciones as $seccion) {
                    /* $this->db->set('nota_id',$nota_id);
                      $this->db->set('objeto_id',$seccion);
                      $this->db->set('tipo',8);
                      $this->db->set('orden',1);
                      $this->db->insert('relacion'); */
                    $sql = "insert into relacion_tag (nota_id,tag_id,tipo,orden,fechaaud) values (" . $nota_id . "," . $seccion . ",5," . $orden . ",now())";
                    $this->db->query($sql);
                    $orden ++;
                }
            }
            return true;
        } else
            return false;
    }

    public function delete($nota_id) {
        $this->db->trans_begin();

        $this->db->query('DELETE FROM relacion WHERE nota_id = ' . $nota_id);
        $this->db->query('DELETE FROM relacion WHERE objeto_id = ' . $nota_id . ' and tipo = 32');
        $this->db->query('DELETE FROM nota_visitas WHERE nota_id = ' . $nota_id);
        $this->db->query('DELETE FROM relacion_tag WHERE nota_id = ' . $nota_id);
        $this->db->query('DELETE FROM nota WHERE id = ' . $nota_id);

        $this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
            return true;
        }
    }

    public function borrar_relacionados($nota_id, $tipo) {
        $this->db->where('nota_id', $nota_id);
        $this->db->where('tipo', $tipo);
        $this->db->delete('relacion_tag');
    }

    public function modificar_nota_grid($id, $datos) {
        $this->db->where('id', $id);
        if ($this->db->update('nota', $datos))
            return true;
        else
            return false;
    }

    public function buscarParaRelacionNota($nota_id, $edicion_id, $filtro = "") {
        $ids_notas_relacionadas = $this->idsNotasRelacionadas($nota_id);
        $not_in = "";
        $edicion_ant = $edicion_id - 60;
        if (count($ids_notas_relacionadas) > 0) {
            foreach ($ids_notas_relacionadas as $ids) {
                $not_in .= $ids['id'] . ",";
            }
            $not_in = substr($not_in, 0, strlen($not_in) - 1);
        }
        if ($not_in != "")
            $not_in .= "," . $nota_id;
        else
            $not_in = $nota_id;

        if ($filtro != "") {
            $sql = "select id,titulo 
			from nota";

            //if (count($ids_notas_relacionadas) > 0)
            //{
            //	foreach ($ids_notas_relacionadas as $ids)
            //	{
            //		$not_in .= $ids['id'].",";
            //$ids_not_in[]=$ids['id'];
            //	}
            //	$not_in = substr($not_in,0,strlen($not_in)-1);
            //$this->db->where_not_in("n.id",$ids_not_in);
            //}
            //if ($not_in!="")
            //	$not_in .= ",".$nota_id;
            //else
            //	$not_in = $nota_id;
            $sql .= " where id not in ($not_in) and edicion_id between $edicion_ant and $edicion_id";
            $sql .= " and titulo like '%$filtro%' and habilitada = 1";

            $sql .= " order by id desc";
            $sql .= " limit 10";

            $query = $this->db->query($sql);
            $res = $query->result_array();
            if (count($res) > 0) {
                return '({"total":"10","rows":' . json_encode($res) . '})';
            } else
                return '({"total":"0","rows":""})';
        }
        else {
            $sql = "select id,titulo 
			from nota where edicion_id between $edicion_ant and $edicion_id and id not in ($not_in) and habilitada = 1 order by id desc limit 30";
            $query = $this->db->query($sql);
            $res = $query->result_array();
            if (count($res) > 0) {
                return '({"total":"30","rows":' . json_encode($res) . '})';
            } else
                return '({"total":"0","rows":""})';
        }
    }

    public function buscarParaRelacionVideo($video_id, $edicion_id, $filtro = "") {
        $ids_notas_relacionadas = $this->idsNotasRelacionadasV($video_id);
        $not_in = "";
        $edicion_ant = $edicion_id - 1;

        $sql = "select id,titulo 
		from nota";

        if (count($ids_notas_relacionadas) > 0) {
            foreach ($ids_notas_relacionadas as $ids) {
                $not_in .= $ids['id'] . ",";
                //$ids_not_in[]=$ids['id'];
            }
            $not_in = substr($not_in, 0, strlen($not_in) - 1);
            //$this->db->where_not_in("n.id",$ids_not_in);
        }
        if ($not_in != "")
            $not_in .= "," . $video_id;
        else
            $not_in = $video_id;
        $sql .= " where id not in ($not_in) and edicion_id in ($edicion_ant,$edicion_id)";

        if ($filtro != "") {
            $sql .= " and titulo like '%$filtro%'";
        }

        $sql .= " order by id desc";
        $sql .= " limit 50";

        $query = $this->db->query($sql);
        $res = $query->result_array();

        if (count($res) > 0) {
            return '({"total":"50","rows":' . json_encode($res) . '})';
        } else
            return '({"total":"0","rows":""})';
    }

    public function buscarParaRelacionGaleria($galeria_id, $edicion_id, $filtro = "") {
        $ids_notas_relacionadas = $this->idsNotasRelacionadasG($galeria_id);
        $not_in = "";
        $edicion_ant = $edicion_id - 1;

        $sql = "select id,titulo 
		from nota";

        if (count($ids_notas_relacionadas) > 0) {
            foreach ($ids_notas_relacionadas as $ids) {
                $not_in .= $ids['id'] . ",";
                //$ids_not_in[]=$ids['id'];
            }
            $not_in = substr($not_in, 0, strlen($not_in) - 1);
            //$this->db->where_not_in("n.id",$ids_not_in);
        }
        if ($not_in != "")
            $not_in .= "," . $galeria_id;
        else
            $not_in = $galeria_id;
        $sql .= " where id not in ($not_in) and edicion_id in ($edicion_ant,$edicion_id)";

        if ($filtro != "") {
            $sql .= " and titulo like '%$filtro%'";
        }

        $sql .= " order by id desc";
        $sql .= " limit 50";

        $query = $this->db->query($sql);
        $res = $query->result_array();

        if (count($res) > 0) {
            return '({"total":"50","rows":' . json_encode($res) . '})';
        } else
            return '({"total":"0","rows":""})';
    }

    public function totalParaRelacionNota($ids_notas_relacionadas, $filtro = "") {
        $this->db->select("count(id) as cantidad");
        $this->db->from("nota n");

        if (count($ids_notas_relacionadas) > 0) {
            foreach ($ids_notas_relacionadas as $ids) {
                $ids_not_in[] = $ids['id'];
            }
            $this->db->where_not_in("id", $ids_not_in);
        }

        if ($filtro != "") {
            $this->db->like("n.titulo", $filtro);
            $this->db->like("n.antetitulo", $filtro);
            $this->db->like("n.sumario", $filtro);
            $this->db->like("n.texto", $filtro);
        }
        $query = $this->db->get();
        $res = $query->result();
        return $res[0]->cantidad;
    }

    public function idsNotasRelacionadas($nota_id) {
        $sql = "select n.id from nota n 
		inner join relacion r on (r.objeto_id = n.id) 
		where r.nota_id = " . $nota_id . " and r.tipo = 32";
        //$this->db->select("n.id");
        //$this->db->from("nota n");
        //$this->db->join("relacion r","r.objeto_id = n.id","inner");
        //$this->db->where("r.nota_id",$nota_id);
        //$this->db->where("r.tipo",32);
        //if($front==1)
        //{
        //	$this->db->where("n.habilitada",1);
        //}
        //$this->db->order_by("orden");
        //$query = $this->db->get();
        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res;
    }

    public function idsNotasRelacionadasV($video_id) {
        $sql = "select n.id from nota n 
		inner join relacion r on (r.nota_id = n.id) 
		where r.objeto_id = " . $video_id . " and r.tipo = 2";

        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res;
    }

    public function idsNotasRelacionadasG($galeria_id) {
        $sql = "select n.id from nota n 
		inner join relacion r on (r.nota_id = n.id) 
		where r.objeto_id = " . $galeria_id . " and r.tipo = 5";

        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res;
    }

    public function agregarRelacionNota($notar_id, $nota_id) {
        $datos['orden'] = $this->dameOrdenRelacionNota($nota_id);
        if ($datos['orden'] == "")
            $datos['orden'] = 1;
        $datos['nota_id'] = $nota_id;
        $datos['objeto_id'] = $notar_id;
        $datos['tipo'] = 32;
        $datos['destacado'] = ($this->hayDestacadaEnNota($nota_id)) ? 0 : 1;
        $this->db->insert("relacion", $datos);
        $datos['orden'] = $this->dameOrdenRelacionNota($notar_id);
        if ($datos['orden'] == "")
            $datos['orden'] = 1;
        $datos['nota_id'] = $notar_id;
        $datos['objeto_id'] = $nota_id;
        $datos['tipo'] = 32;
        $datos['destacado'] = ($this->hayDestacadaEnNota($notar_id)) ? 0 : 1;
        if ($this->db->insert("relacion", $datos))
            return true;
        else
            return false;
    }

    public function dameOrdenRelacionNota($nota_id) {
        $this->db->select("max(orden) as orden");
        $this->db->from("relacion");
        $this->db->where("nota_id", $nota_id);
        $this->db->where("tipo", 32);
        $query = $this->db->get();
        $res = $query->result();
        if ($res[0]->orden != "") {
            $orden = $res[0]->orden;
            $orden = $orden + 1;
        } else
            $orden = 1;
        return $orden;
    }

    public function hayDestacadaEnNota($nota_id) {
        $this->db->select("id");
        $this->db->from("relacion");
        $this->db->where("nota_id", $nota_id);
        $this->db->where("tipo", 32);
        $this->db->where("destacado", 1);
        $query = $this->db->get();
        if ($query->num_rows() > 0)
            return true;
        else
            return false;
    }

    public function buscarNotasRelacionadas($nota_id) {
        $this->db->select("r.id as relacion_id,n.id,n.titulo,r.destacado,r.orden");
        $this->db->from("nota n");
        $this->db->join("relacion r", "r.objeto_id = n.id", "inner");
        $this->db->where("r.nota_id", $nota_id);
        $this->db->where("r.tipo", 32);
        $this->db->order_by("r.orden asc");
        $query = $this->db->get();
        $res = $query->result_array();
        $cant = count($res);
        if (count($res) > 0) {
            return '({"total":"' . $cant . '","rows":' . json_encode($res) . '})';
        } else
            return '({"total":"0","rows":""})';
    }

    public function eliminarRelacionNota($notar_id, $nota_id) {
        $this->db->where("objeto_id", $nota_id);
        $this->db->where("nota_id", $notar_id);
        $this->db->where("tipo", 32);
        $this->db->delete("relacion");

        $this->db->where("objeto_id", $notar_id);
        $this->db->where("nota_id", $nota_id);
        $this->db->where("tipo", 32);
        if ($this->db->delete("relacion")) {
            return true;
        } else
            return false;
    }

    public function updateRelacionNota($id, $datos, $notar_id, $nota_id) {
        $this->db->where("id", $id);
        if ($this->db->update("relacion", $datos)) {
            if (isset($datos['orden'])) {
                $this->db->query("UPDATE relacion SET orden = '" . $datos['orden'] . "' where nota_id = " . $nota_id . " and tipo = 32 and objeto_id = " . $notar_id);
            }
            return true;
        } else
            return false;
    }

    public function asociarPeriodista($nota_id, $periodista_id) {
        $datos['nota_id'] = $nota_id;
        $datos['objeto_id'] = $periodista_id;
        $datos['tipo'] = 4;
        $datos['orden'] = 1;

        if ($this->db->insert("relacion", $datos))
            return true;
        else
            return false;
    }

    public function dameRelacionPeriodista($nota_id) {
        $this->db->select("id");
        $this->db->from("relacion");
        $this->db->where("nota_id", $nota_id);
        $this->db->where("tipo", 4);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            $res = $query->result();
            return $res[0]->id;
        } else
            return false;
    }

    public function cambiarPeriodista($relacion_id, $periodista) {
        $datos['objeto_id'] = $periodista;

        $this->db->where("id", $relacion_id);
        if ($this->db->update("relacion", $datos))
            return true;
        else
            return false;
    }

    public function quitarPeriodista($nota_id) {
        $sql = "delete from relacion where nota_id = " . $nota_id . " and tipo = 4";
        if ($this->db->query($sql))
            return true;
        else
            return false;
        /* $this->db->where("id",$relacion_id);
          if ($this->db->delete("relacion"))
          return true;
          else
          return false; */
    }

    public function dameTexto($nota_id) {
        $this->db->select("texto");
        $this->db->from("nota");
        $this->db->where("id", $nota_id);
        $query = $this->db->get();
        $res = $query->result_array();
        /* $verifica = explode("xto>",$res[0]['texto']);
          if (count($verifica) > 1)
          $texto = $this->extraer_string($res[0]['texto'],"<texto>","</texto>");
          else */
        $texto = $res[0]['texto'];

        return $texto;
    }

    public function listadoPorSeccion($seccion_id, $edicion_id = 0, $limit = "") {
        $sql = "select n.id, n.antetitulo, n.titulo, n.sumario as bajada, n.fechaAlta as fecha, n.url, 
		(
			select CONCAT(f.ruta,'tmb2_',f.imagen) 
			from foto f 
			inner join relacion r2 on (f.id = r2.objeto_id)
			where r2.tipo = 30 and r2.nota_id = n.id order by r2.destacado desc limit 1
		) as destacada, n.seleccionada 
		from nota n 
		inner join relacion_tag r on (r.nota_id = n.id) 
		where r.tipo = 5 and r.tag_id = " . $seccion_id . " and n.habilitada = 1";

        if ($edicion_id > 0)
            $sql .= " and n.edicion_id = " . $edicion_id;
        else
            $sql .= " and n.fechaVencimiento >= '" . date('Y-m-d') . "' and n.fechaInicio <= '" . date('Y-m-d') . "'";

        $sql .= " order by r.orden asc, n.id desc";

        if ($limit != "")
            $sql .= " limit " . $limit;

        $query = $this->db->query($sql);
        $res = $query->result();
        return $res;
    }

    public function listadoEnSecciones($seccion_id, $edicion_id = 0, $limit = "") {
        $sql = "select n.id, n.antetitulo, n.titulo, n.sumario as bajada, n.fechaAlta as fecha, n.url, 
		(
			select CONCAT(f.ruta,'tmb2_',f.imagen) 
			from foto f 
			inner join relacion r2 on (f.id = r2.objeto_id)
			where r2.tipo = 30 and r2.nota_id = n.id order by r2.destacado desc limit 1
		) as destacada 
		from nota n 
		inner join relacion_tag r on (r.nota_id = n.id) 
		where r.tipo = 5 and r.tag_id in (" . $seccion_id . ") and n.habilitada = 1";

        if ($edicion_id > 0)
            $sql .= " and n.edicion_id = " . $edicion_id;
        else
            $sql .= " and n.fechaVencimiento >= '" . date('Y-m-d') . "' and n.fechaInicio <= '" . date('Y-m-d') . "'";

        $sql .= " order by r.orden desc";

        if ($limit != "")
            $sql .= " limit " . $limit;
        $query = $this->db->query($sql);
        $res = $query->result();
        return $res;
    }

    public function listadoNotaSeccion($seccion_id, $edicion_id) {
        $this->db->select("n.id, n.titulo, n.fechaAlta, r.orden");
        $this->db->from("nota n");
        $this->db->join("relacion_tag r", "r.nota_id = n.id", "inner");
        $this->db->where("r.tag_id", $seccion_id);
        $this->db->where("r.tipo", 5);
        $this->db->where("n.habilitada", 1);
        if ($edicion_id > 0) {
            $this->db->where("n.edicion_id", $edicion_id);
        } else {
            $this->db->where("n.fechaInicio <=", date('Y-m-d'));
            //$this->db->where("n.fechaInicio <=",'2011-05-15');
            $this->db->where("n.fechaVencimiento >=", date('Y-m-d'));
            //$this->db->where("n.fechaVencimiento >=",'2011-05-15');
        }
        $this->db->order_by("r.orden asc");
        $query = $this->db->get();
        $res = $query->result_array();
        return $res;
    }

    public function modificarOrdenSeccion($nota_id, $tag_id, $datos) {
        $this->db->where("nota_id", $nota_id);
        $this->db->where("tag_id", $tag_id);
        if ($this->db->update("relacion_tag", $datos))
            return true;
        else
            return false;
    }

    public function dameParaDiagramar($nota_id) {
        $this->db->select("texto as texto1, vista");
        $this->db->from("nota");
        $this->db->where("id", $nota_id);
        $query = $this->db->get();
        $res = $query->result_array();
        $verifica = explode("xto>", $res[0]['texto1']);
        if (count($verifica) > 1)
            $texto = $this->extraer_string($res[0]['texto1'], "<texto>", "</texto>");
        else
            $texto = $res[0]['texto1'];

        $texto1 = $this->extraer_string($res[0]['texto1'], "<texto1>", "</texto1>");
        $texto2 = $this->extraer_string($res[0]['texto1'], "<texto2>", "</texto2>");

        $pos_img = strpos($res[0]['texto1'], "<imagen>");
        $pos_video = strpos($res[0]['texto1'], "<multimedio>");
        $pos_map = strpos($res[0]['texto1'], "<mapa>");
        $b_img = false;
        $b_video = false;
        $b_map = false;

        if ($pos_img)
            $b_img = true;

        if ($pos_video)
            $b_video = true;

        if ($pos_map)
            $b_map = true;

        $res[0]['texto1'] = ($texto1) ? $texto1 : $texto;
        $res[0]['texto2'] = ($texto2) ? $texto2 : "";

        $res[0]['objeto1'] = "imagen";
        $res[0]['objeto2'] = "imagen";
        switch ($res[0]['vista']) {
            case 1:
                $res[0]['texto1'] .= ($res[0]['texto2'] != "") ? "<br/>" . $res[0]['texto2'] : "";
                $res[0]['texto2'] = "";
                break;
            case 2:
                $res[0]['texto1'] .= ($res[0]['texto2'] != "") ? "<br/>" . $res[0]['texto2'] : "";
                $res[0]['texto2'] = "";
                break;
            case 3:
                if ($b_img)
                    $res[0]['objeto1'] = "imagen";
                if ($b_video)
                    $res[0]['objeto1'] = "video";
                if ($b_map)
                    $res[0]['objeto1'] = "mapa";
                break;
            case 4:
                if ($b_img and $b_map) {
                    if ($b_img > $b_map) {
                        $res[0]['objeto1'] = "mapa";
                        $res[0]['objeto2'] = "imagen";
                    } else {
                        $res[0]['objeto2'] = "mapa";
                        $res[0]['objeto1'] = "imagen";
                    }
                } elseif ($b_img and $b_video) {
                    if ($b_img > $b_video) {
                        $res[0]['objeto1'] = "video";
                        $res[0]['objeto2'] = "imagen";
                    } else {
                        $res[0]['objeto2'] = "video";
                        $res[0]['objeto1'] = "imagen";
                    }
                } elseif ($b_map and $b_video) {
                    if ($b_map > $b_video) {
                        $res[0]['objeto1'] = "video";
                        $res[0]['objeto2'] = "mapa";
                    } else {
                        $res[0]['objeto2'] = "video";
                        $res[0]['objeto1'] = "mapa";
                    }
                } elseif ($b_map) {
                    $res[0]['objeto2'] = "mapa";
                    $res[0]['objeto1'] = "mapa";
                } elseif ($b_img) {
                    $res[0]['objeto2'] = "imagen";
                    $res[0]['objeto1'] = "imagen";
                } elseif ($b_video) {
                    $res[0]['objeto2'] = "video";
                    $res[0]['objeto1'] = "video";
                }
                break;
        }
        return $res;
    }

    public function dameVistaDiagramar($nota_id) {
        $this->db->select("vista");
        $this->db->from("nota");
        $this->db->where("id", $nota_id);
        $query = $this->db->get();
        $res = $query->result_array();
        return $res[0]['vista'];
    }

    public function dameTextoAgregado($nota_id) {
        $this->db->select("texto");
        $this->db->from("nota");
        $this->db->where("id", $nota_id);
        $query = $this->db->get();
        $res = $query->result_array();
        $verifica = explode("xto>", $res[0]['texto']);
        if (count($verifica) > 1) {
            $texto = $this->extraer_string($res[0]['texto'], "<texto>", "</texto>");
            $tituloagregado = $this->extraer_string($res[0]['texto'], "<tituloagregado>", "</tituloagregado>");
            $textoagregado = $this->extraer_string($res[0]['texto'], "<textoagregado>", "</textoagregado>");
        } else {
            $texto = $res[0]['texto'];
            $tituloagregado = "";
            $textoagregado = "";
        }
        $resultado['texto'] = $texto;
        $resultado['tituloagregado'] = $tituloagregado;
        $resultado['textoagregado'] = $textoagregado;

        return $resultado;
    }

    public function listarNotasDiagramarBlog($seccion_id, $padre_id, $titulo) {
        $this->db->select("n.id, n.titulo");
        $this->db->from("nota n");
        $this->db->join("relacion r", "r.nota_id = n.id", "inner");
        $where = "r.tipo = 8";

        if ($titulo != "") {
            $where .= " and n.titulo like '%" . $titulo . "%'";
        }

        if ($padre_id > 0) {
            $where .= " and (r.objeto_id = " . $seccion_id . " or r.objeto_id = " . $padre_id . ")";
        } else
            $where .= " and r.objeto_id = " . $seccion_id;

        $this->db->where($where);
        $this->db->order_by("r.objeto_id desc, n.fechamod desc");
        $this->db->limit(10);

        $query = $this->db->get();
        $res = $query->result_array();
        return $res;
    }

    public function agregarNotaBlog($nota_id, $seccion_id, $columna) {
        $datos['orden'] = $this->dameOrdernParaBlog($seccion_id, $columna);
        $datos['muestraimg'] = 1;
        $datos['columna'] = $columna;
        $datos['seccion_id'] = $seccion_id;
        $datos['nota_id'] = $nota_id;
        if ($this->db->insert("nota_seccion", $datos))
            return true;
        else
            return false;
    }

    public function dameOrdernParaBlog($seccion_id, $columna) {
        $this->db->select("max(orden) as orden");
        $this->db->from("nota_seccion");
        $this->db->where("seccion_id", $seccion_id);
        $this->db->where("columna", $columna);
        $query = $this->db->get();
        $res = $query->result();
        if ($res[0]->orden != "")
            return $res[0]->orden + 1;
        else
            return 1;
    }

    public function dameNotasBlog($seccion_id, $columna) {
        $this->db->select("n.id as nota_id, n.titulo, ns.id, ns.orden, ns.muestraimg, ns.columna");
        $this->db->from("nota n");
        $this->db->join("nota_seccion ns", "ns.nota_id = n.id", "inner");
        $this->db->where("ns.seccion_id", $seccion_id);
        $this->db->where("ns.columna", $columna);
        $this->db->order_by("ns.orden");
        $query = $this->db->get();
        $res = $query->result_array();
        return $res;
    }

    public function quitarNotaBlog($relacion_id) {
        $this->db->where("id", $relacion_id);
        if ($this->db->delete("nota_seccion"))
            return true;
        else
            return false;
    }

    public function modificarNotaBlog($relacion_id, $datos) {
        //$datos['columna'] = $columna;
        $this->db->where("id", $relacion_id);
        if ($this->db->update("nota_seccion", $datos))
            return true;
        else
            return false;
    }

    public function dameNotasBlogParaPublicar($seccion_id, $columna) {
        $sql = "select n.id, n.antetitulo, n.titulo, n.sumario as bajada, n.fechaAlta as fecha, n.url,
		(
			select CONCAT(f.ruta,'tmb2_',f.imagen) 
			from foto f 
			inner join relacion r2 on (f.id = r2.objeto_id)
			where r2.tipo = 30 and r2.destacado = 1 and r2.nota_id = n.id limit 1
		) as destacada, ns.muestraimg 
		from nota n 
		inner join nota_seccion ns on (ns.nota_id = n.id) 
		where n.habilitada = 1 and ns.seccion_id = " . $seccion_id . " and ns.columna = " . $columna;
        $sql .= " order by ns.orden";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result();
            return $res;
        } else
            return false;
    }

    public function buscarParaCargarCarrousel($carrousel_id, $edicion_id, $filtro = "") {
        $ids_notas_cargadas = $this->idsNotasCargadas($carrousel_id);
        $not_in = "";
        $edi_in = ($edicion_id - 5) . "," . ($edicion_id - 4) . "," . ($edicion_id - 3) . "," . ($edicion_id - 2) . "," . ($edicion_id - 1) . "," . $edicion_id;
        if ($filtro != "") {
            $sql = "select titulo, id from nota";
            $sql .= " where edicion_id >= 3716 and lector=1 and habilitada=1";
            $sql .= " and titulo like '%$filtro%'";

            if (count($ids_notas_cargadas) > 0) {
                foreach ($ids_notas_cargadas as $ids) {
                    $not_in .= $ids['id'] . ",";
                }
                $not_in = substr($not_in, 0, strlen($not_in) - 1);
                $sql .= " and id not in ($not_in)";
            }

            $sql .= " order by id desc";
            $sql .= " limit 20";

            $query = $this->db->query($sql);
            if ($query->num_rows() > 0) {
                $res = $query->result_array();
                return '({"total":"20","rows":' . json_encode($res) . '})';
            } else
                return '({"total":"0","rows":""})';
        }
        else {
            $sql = "select titulo, id from nota";
            $sql .= " where edicion_id >= 3716 and lector=1 and habilitada=1";

            if (count($ids_notas_cargadas) > 0) {
                foreach ($ids_notas_cargadas as $ids) {
                    $not_in .= $ids['id'] . ",";
                }
                $not_in = substr($not_in, 0, strlen($not_in) - 1);
                $sql .= " and id not in ($not_in)";
            }

            $sql .= " order by id desc";
            $sql .= " limit 20";

            $query = $this->db->query($sql);
            if ($query->num_rows() > 0) {
                $res = $query->result_array();
                return '({"total":"20","rows":' . json_encode($res) . '})';
            } else
                return '({"total":"0","rows":""})';
        }
    }

    public function idsNotasCargadas($carrousel_id) {
        $sql = "select n.id from nota n inner join carrusel_contenido c on (c.objeto_id = n.id) 
		where c.tipo = 1 and c.carrusel_id = " . $carrousel_id;

        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res;
    }

    public function totalParaCargarCarrousel($ids_notas_cargadas, $filtro = "") {
        $this->db->select("count(id) as cantidad");
        $this->db->from("nota");

        if (count($ids_notas_cargadas) > 0) {
            foreach ($ids_notas_cargadas as $ids) {
                $ids_not_in[] = $ids['id'];
            }
            $this->db->where_not_in("id", $ids_not_in);
        }

        if ($filtro != "")
            $this->db->like("titulo", $filtro);

        $query = $this->db->get();
        $res = $query->result();
        return $res[0]->cantidad;
    }

    public function buscarNotasCargadas($carrousel_id) {
        $sql = "select c.id as carga_id, c.orden, n.id, n.titulo, 
		(select CONCAT(f.ruta,f.imagen) as foto 
		  from foto f 
		  inner join relacion r on (f.id = r.objeto_id and r.tipo=30) 
		  where r.nota_id = n.id order by r.destacado desc limit 1
		 ) as imagen
		 from nota n 
		 inner join carrusel_contenido c on (c.objeto_id = n.id) 
		 where c.tipo = 1 and c.carrusel_id = " . $carrousel_id . " 
		 order by c.orden asc";

        $query = $this->db->query($sql);
        $res = $query->result_array();
        $cant = count($res);
        if (count($res) > 0) {
            $dim = $this->dameAnchoAlto($carrousel_id);
            if ($dim) {
                $temp_ancho = $dim['ancho'];
                $temp_alto = $dim['alto'];
            } else {
                $temp_ancho = 140;
                $temp_alto = 105;
            }

            for ($i = 0; $i < count($res); $i++) {
                $res[$i]['ancho'] = $temp_ancho;
                $res[$i]['alto'] = $temp_alto;
            }
            return '({"total":"' . $cant . '","rows":' . json_encode($res) . '})';
        } else
            return '({"total":"0","rows":""})';
    }

    public function dameAnchoAlto($carrousel_id) {
        $sql = "select cp.alto, cp.ancho from carrusel c inner join carrusel_plantilla cp on (c.plantilla = cp.id) 
		where c.id = " . $carrousel_id;

        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0];
        } else
            return false;
    }

    public function cargarEnCarrousel($nota_id, $carrousel_id) {
        $datos['orden'] = $this->dameOrdenNotaCarrousel($carrousel_id);
        $datos['carrusel_id'] = $carrousel_id;
        $datos['objeto_id'] = $nota_id;
        $datos['tipo'] = 1;
        if ($this->db->insert("carrusel_contenido", $datos))
            return true;
        else
            return false;
    }

    public function dameOrdenNotaCarrousel($carrousel_id) {
        $this->db->select("max(orden) as orden");
        $this->db->from("carrusel_contenido");
        $this->db->where("carrusel_id", $carrousel_id);
        $this->db->where("tipo", 1);
        $query = $this->db->get();
        $res = $query->result();
        if ($res[0]->orden != "") {
            $orden = $res[0]->orden;
            $orden = $orden + 1;
        } else
            $orden = 1;
        return $orden;
    }

    public function eliminarCargaCarrousel($carga_id, $carrousel_id) {
        $this->db->where("id", $carga_id);
        if ($this->db->delete("carrusel_contenido")) {
            return true;
        } else
            return false;
    }

    public function updateCargaCarrousel($carrousel_id, $datos) {
        $this->db->where("id", $carrousel_id);
        if ($this->db->update("carrusel_contenido", $datos)) {
            return true;
        } else
            return false;
    }

    public function listadoMilenium($fecha, $start, $limit, $buscar, $sort, $dir) {
        $sql = "select id, antetitulo, titulo, sumario, seccion, DATE_FORMAT(fecha, '%d-%m-%Y') as fecha, habilitada, pagina, aprobada, milenium_id, estado, version 
        from nota_milenium 
        where fecha = '$fecha'";

        if ($buscar != "") {
            $sql .= " and (antetitulo like '%$buscar%' or titulo like '%$buscar%' or sumario like '%$buscar%' 
			or seccion like '%$buscar%')";
        }

        if ($sort != "")
            $sql .= " order by $sort $dir";
        else
            $sql .= " order by id";

        $sql .= " limit $start,$limit";

        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $registros = $this->totalMilenium($fecha, $buscar);
            $res = $query->result_array();
            return '({"total":"' . $registros . '","rows":' . json_encode($res) . '})';
        } else
            return '({"total":"0","rows":""})';
    }

    public function totalMilenium($fecha, $buscar = "") {
        $sql = "select count(id) as cantidad from nota_milenium 
		where fecha = '$fecha'";

        if ($buscar != "") {
            $sql .= " and (antetitulo like '%$buscar%' or titulo like '%$buscar%' or sumario like '%$buscar%' 
			or seccion like '%$buscar%')";
        }

        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res[0]['cantidad'];
    }

    public function modificarMilenium($id, $datos) {
        $this->db->where("id", $id);
        if ($this->db->update("nota_milenium", $datos))
            return true;
        else
            return false;
    }

    public function datosMilenium($id) {
        $sql = "select id, antetitulo, titulo, sumario, texto, fecha, habilitada, seccion 
		from nota_milenium where id = " . $id;
        $query = $this->db->query($sql);
        $res = $query->result_array();
        $texto = $this->extraer_string($res[0]['texto'], "<texto>", "</texto>");
        $tituloagregado = $this->extraer_string($res[0]['texto'], "<tituloagregado>", "</tituloagregado>");
        $textoagregado = $this->extraer_string($res[0]['texto'], "<textoagregado>", "</textoagregado>");

        if ($texto)
            $res[0]['texto'] = $texto;
        else
            $res[0]['texto'] = "";

        if ($tituloagregado)
            $res[0]['tituloagregado'] = $tituloagregado;
        else
            $res[0]['tituloagregado'] = "";

        if ($textoagregado)
            $res[0]['textoagregado'] = $textoagregado;
        else
            $res[0]['textoagregado'] = "";
        return $res;
    }

    public function delteMilenium($id) {
        $sql = "delete from foto_milenium where nota_id = " . $id;
        $sql2 = "delete from nota_milenium where id = " . $id;

        if ($this->db->query($sql)) {
            if ($this->db->query($sql2))
                return true;
            else
                return false;
        } else
            return false;
    }

    public function buscarNotaMilenium($id) {
        $sql = "select id, antetitulo, titulo, sumario, texto, seccion, fecha, aprobada 
		from nota_milenium where id = " . $id;
        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res[0];
    }

    public function insertarDesdeMilenium($datos, $seccion) {
        $secciones = array("Politica" => "2", "Economia" => "3", "Economia y Finanzas" => "3", "Deportes" => "4", "LG-DEPORTIVA" => "4",
            "Opinion" => "6", "Policiales" => "11", "Ciudadanos" => "383", "Tucumanos" => "134", "Mundo" => "10", "Actualidad" => "108",
            "Argentina" => "9", "Atletico" => "4", "B Nacional" => "4", "Economico" => "3", "Espectaculos" => "377", "Informacion General" => "383",
            "La Brujula" => "377", "Rural" => "107", "San Martin" => "4", "Suplemento Eventual tab" => "4", "Tapa" => "16",
            "Hogar" => "106", "Mujer" => "134", "Sociedad" => "134");
        if ($this->db->insert("nota", $datos)) {
            $id = $this->db->insert_id();
            $array_sec = explode(",", $seccion);
            if (count($array_sec) > 0) {
                foreach ($array_sec as $sec) {
                    if ($sec != "" and isset($secciones[$sec])) {
                        $seccion_id = $secciones[$sec];
                        $this->insertaRelacionSeccionMilenium($id, $seccion_id);
                    } else
                        $this->insertaRelacionSeccionMilenium($id, 134);
                }
            } else
                $this->insertaRelacionSeccionMilenium($id, 134);

            return $id;
        } else
            return false;
    }

    public function insertarDesdeEscritorio($datos, $seccion) {
        $secciones = array("Política" => "2", "Economía" => "3", "Deportes" => "4",
            "Opinión" => "6", "Policiales" => "11", "Tucumanos" => "134", "Mundo" => "10");
        if ($this->db->insert("nota", $datos)) {
            $id = $this->db->insert_id();
            $array_sec = explode(",", $seccion);
            if (count($array_sec) > 0) {
                foreach ($array_sec as $sec) {
                    if ($sec != "" and isset($secciones[$sec])) {
                        $seccion_id = $secciones[$sec];
                        $this->insertaRelacionSeccionMilenium($id, $seccion_id);
                    } else
                        $this->insertaRelacionSeccionMilenium($id, 134);
                }
            } else
                $this->insertaRelacionSeccionMilenium($id, 134);

            return $id;
        } else
            return false;
    }

    public function insertaRelacionSeccionMilenium($nota_id, $seccion_id) {
        $sql = "insert into relacion_tag (nota_id, tag_id, tipo, orden, fechaaud) values ($nota_id, $seccion_id, 5, 1, now())";
        $this->db->query($sql);
    }

    public function dameXmlMilenium($id) {
        $sql = "select xml from nota_milenium where id = " . $id;
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0]['xml'];
        } else
            return false;
    }

    public function dameNotasMailing($edicion_id, $seccion_id) {
        $sql = "select n.id, n.titulo, n.url 
		from nota n  
		inner join relacion_tag r on (r.nota_id = n.id and r.tipo = 5) 
		where n.edicion_id = " . $edicion_id . " and r.tag_id = $seccion_id ";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    public function dameNotasEnvioUM($edicion_id, $ord) {
        $sql = "select n.id, n.titulo, n.antetitulo, n.sumario, n.url, (select t.nombre from tag t 
		inner join relacion_tag r on (r.tag_id=t.id and r.tipo=5) 
        where r.nota_id = n.id and t.habilitado=1
        limit 1) as seccion 
		from nota n  
		where n.edicion_id = $edicion_id and n.um = 1 and n.habilitada = 1 order by fechamod $ord limit 10";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    public function dameNotasUltimoMomento($edicion_id = NULL, $seccion_id = NULL, $limit = NULL) {
        $sql = "SELECT DISTINCT n.id, n.titulo, n.url, 
				(
					SELECT CONCAT(f.ruta,'tmb2_',f.imagen) 
					FROM foto f 
					INNER JOIN relacion r2 ON (f.id = r2.objeto_id)
					WHERE r2.tipo = 30 AND r2.nota_id = n.id ORDER BY r2.destacado DESC LIMIT 1
				) AS destacada 
				FROM nota n ";

        if ($seccion_id > 0)
            $sql .= " INNER JOIN relacion_tag r ON (r.nota_id = n.id AND r.tipo = 5) ";

        $sql .= " WHERE n.habilitada = 1 and r.tag_id IN (2,3,11,6,10 )";

        if ($edicion_id > 0)
            $sql .= " and n.edicion_id = " . $edicion_id;

        $sql .= " order by n.id desc";

        if ($limit != "")
            $sql .= " limit " . $limit;

        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res;
    }

    function cambiarEdicion($notas, $edicion_id) {
        if (is_array($notas)) {
            foreach ($notas as $nota_id) {
                $sql = "update nota set edicion_id = '" . $edicion_id . "' where id = '" . $nota_id . "'";
                $query = $this->db->query($sql);
            }
            return true;
        } else
            return false;
    }

    public function listadoNotasBlog($secciones, $start, $limit, $buscar, $sort, $dir, $seccion_id = 0) {
        if ($secciones) {
            $sec = explode(",", $secciones);
            $add = "";
            if ($seccion_id > 0) {
                $add = " r.tag_id = " . $seccion_id;
            } else {
                foreach ($sec as $seccion) {
                    $add .= "r.tag_id = " . $seccion . " or ";
                }
                $add = "(" . substr($add, 0, strlen($add) - 4) . ")";
            }
            $sql = "select n.id, n.edicion_id, n.antetitulo, n.titulo, n.habilitada, n.url, DATE_FORMAT(n.fechamod, '%d-%m-%Y %H:%i') as fecha, 
			(select CONCAT(nombre,' ',apellido) as nom_user from usuario where id = n.usuario_id) as usuario 
			, (select visitas from nota_visitas where nota_id = n.id limit 1) as visitas
			/*, (select count(id) as cant from comentario where objeto_id = n.id and tipo = 32) as comentarios*/ 
	        from nota n 
	        inner join relacion_tag r on (n.id = r.nota_id and r.tipo = 5)
	        where n.edicion_id = 0 and $add";

            if ($buscar != "") {
                $sql .= " and (n.antetitulo like '%$buscar%' or n.titulo like '%$buscar%')";
            }

            if ($sort != "")
                $sql .= " order by $sort $dir";
            else
                $sql .= " order by n.id desc";

            $sql .= " limit $start,$limit";
            //	echo $sql;die();
            $query = $this->db->query($sql);
            if ($query->num_rows() > 0) {
                $res = $query->result_array();

                foreach ($res as $k => $v) {
                    $res[$k]['seccion'] = $this->dame_secciones($v['id'], '', ',');
                    $res[$k]['seccion_id'] = $this->dame_id_secciones($v['id'], '', ',', 388);
                    $sec_ids = explode(",", $res[$k]['seccion_id']);
                    if (is_array($sec_ids)) {
                        if (count($sec_ids) > 1) {
                            if ($sec_ids[0] > $sec_ids[1])
                                $res[$k]['seccion_id'] = $sec_ids[1];
                            else
                                $res[$k]['seccion_id'] = $sec_ids[0];
                        } else
                            $res[$k]['seccion_id'] = $sec_ids[0];
                    } else
                        $res[$k]['seccion_id'] = 0;

                    $res[$k]['comentarios'] = $this->dameCantCom($v['id']); // se añadió en la consulta para poder ordenar por este campo
                }
                //echo "<pre>".print_r($res,true)."</pre>";die();
                $registros = $this->totalNotasBlog($secciones, $buscar, $seccion_id);
                return '({"total":"' . $registros . '","rows":' . json_encode($res) . '})';
            } else
                return '({"total":"0","rows":""})';
        } else
            return '({"total":"0","rows":""})';
    }

    public function totalNotasBlog($secciones, $buscar, $seccion_id = 0) {
        $sec = explode(",", $secciones);
        $add = "";
        if ($seccion_id > 0) {
            $add = " and r.tag_id = " . $seccion_id;
        } else {
            foreach ($sec as $seccion) {
                $add .= "r.tag_id = " . $seccion . " or ";
            }
            $add = "and (" . substr($add, 0, strlen($add) - 4) . ")";
        }

        $sql = "select count(n.id) as cantidad from nota n 
		inner join relacion_tag r on (r.nota_id = n.id and r.tipo=5)
		where n.edicion_id=0 $add";

        if ($buscar != "") {
            $sql .= " and (n.antetitulo like '%$buscar%' or n.titulo like '%$buscar%')";
        }

        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res[0]['cantidad'];
    }

    public function guardarEntradaBlog($datos, $seccion_id, $nota_id = 0, $categoria = "", $relacion_id2 = "", $periodista_id = "") {
        if ($nota_id > 0) {
            $this->db->where("id", $nota_id);
            if ($this->db->update("nota", $datos)) {
                $update_relacion = "update relacion_tag set tag_id = " . $seccion_id . " where nota_id = " . $nota_id . " and orden = 1";
                if ($this->db->query($update_relacion)) {
                    if ($relacion_id2 > 0) {
                        $categoria = ($categoria == "") ? 0 : $categoria;
                        $update_relacion2 = "update relacion_tag set tag_id = " . $categoria . " where nota_id = " . $nota_id . " and orden=2";
                        $this->db->query($update_relacion2);
                    } elseif ($categoria != "") {
                        /* $info['nota_id'] = $nota_id;
                          $info['tag_id'] = $categoria;
                          $info['tipo'] = 5;
                          $info['orden'] = 2;
                          $this->db->insert("relacion",$info); */
                        $sql_insert_cat = "insert into relacion_tag (nota_id,tag_id,tipo,orden,fechaaud) values (" . $nota_id . "," . $categoria . ",5,2,now())";
                        $this->db->query($sql_insert_cat);
                    }
                    $return = $nota_id;
                } else {
                    $sql = "delete from nota where id = " . $nota_id;
                    $this->db->query($sql);
                    $return = false;
                }

                //inserto relacion periodista/nota(blog)
                $this->borrar_periodistas_relacionados($nota_id, 4);
                if ($periodista_id and $periodista_id != "") {
                    $insert_per = "insert into relacion (nota_id, objeto_id, tipo, orden) values (" . $nota_id . "," . $periodista_id . ",4,1)";
                    $this->db->query($insert_per);
                }
            } else
                $return = false;
        }
        else {
            if ($this->db->insert("nota", $datos)) {
                $nota_id = $this->db->insert_id();
                /* $info['nota_id'] = $nota_id;
                  $info['objeto_id'] = $seccion_id;
                  $info['tipo'] = 8;
                  $info['orden'] = 1; */
                $sql_insert_sec = "insert into relacion_tag (nota_id,tag_id,tipo,orden,fechaaud) value (" . $nota_id . "," . $seccion_id . ",5,1,now())";
                if ($this->db->query($sql_insert_sec)) {
                    if ($categoria) {
                        /* $info['nota_id'] = $nota_id;
                          $info['objeto_id'] = $categoria;
                          $info['tipo'] = 8;
                          $info['orden'] = 2; */
                        $sql_insert_cat = "insert into relacion_tag (nota_id,tag_id,tipo,orden,fechaaud) values (" . $nota_id . "," . $categoria . ",5,2,now())";
                        $this->db->query($sql_insert_cat);
                    }
                    $anio = substr($datos['fechaAlta'], 0, 4);
                    $mes = substr($datos['fechaAlta'], 5, 2);
                    $dia = substr($datos['fechaAlta'], 8, 2);
                    //$this->contador_blog($anio,$mes,$dia,$seccion_id,1);
                    //$sql_audit = "select f_cantidad_blog($anio,$mes,$dia,$seccion_id,1)";
                    //$this->db->query($sql_audit);
                    //inserto relacion periodista/nota(blog)
                    //$this->borrar_periodistas_relacionados($nota_id,4);
                    if ($periodista_id and $periodista_id != "") {
                        $insert_per = "insert into relacion (nota_id, objeto_id, tipo, orden) values (" . $nota_id . "," . $periodista_id . ",4,1)";
                        $this->db->query($insert_per);
                    }

                    $return = $nota_id;
                } else {
                    $sql = "delete from nota where id = " . $nota_id;
                    $this->db->query($sql);
                    $return = false;
                }
            } else
                $return = false;
        }

        return $return;
    }

    public function contador_blog($anio, $mes, $dia, $seccion_id, $op) {
        $sql_audit = "select f_cantidad_blog($anio,$mes,$dia,$seccion_id,$op)";
        $this->db->query($sql_audit);
    }

    public function dameFechaNota($nota_id) {
        $sql = "select DATE_FORMAT(fechaAlta, '%Y-%m-%d') as fecha from nota where id = " . $nota_id;
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0]['fecha'];
        } else
            return false;
    }

    public function traer_datos_nota_blog($nota_id) {
        $sql = "select n.id,n.antetitulo, n.titulo, n.sumario, n.habilitada, n.texto, n.url, n.vista,
		r.orden as relacion_id, r.tag_id as seccion, re.orden as relacion_id2, re.tag_id as categoria, 
		n.usuario_id as periodista
		from nota n 
		inner join relacion_tag r on (r.nota_id = n.id and r.tipo = 5 and r.orden = 1) 
		left join relacion_tag re on (re.nota_id = n.id and re.tipo = 5 and re.orden=2)
		where n.id = " . $nota_id;
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            //para traer el id del periodista
            /* foreach ($res as $k=>$v)
              {
              $res[$k]['periodista']=$this->dame_id_periodistas($v['id'],'',',');
              } */

            return $res;
        } else
            return '({"total":"0","rows":""})';
    }

    public function updateNotaBlog($datos, $nota_id) {
        $this->db->where("id", $nota_id);
        if ($this->db->update("nota", $datos))
            return true;
        else
            return false;
    }

    public function dameIdBlog($nota_id) {
        $sql = "select tag_id from relacion_tag where nota_id = " . $nota_id . " and tipo = 5 and orden!=2 limit 1";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0]['tag_id'];
        } else
            return false;
    }

    public function listadoNotasLectores($start, $limit, $buscar, $sort, $dir, $habilitada = "") {
        $fil_habi = "";
        if ($habilitada != "") {
            $fil_habi = " n.habilitada=$habilitada and ";
        }
        if ($habilitada != "" and $habilitada == 3) {
            $fil_habi = " (n.habilitada=3 or n.habilitada=4) and ";
        }
        $sql = "SELECT n.id, n.antetitulo, n.titulo, n.habilitada,n.usuario_id, DATE_FORMAT(e.fecha, '%d-%m-%Y') AS fecha, 
		(SELECT CONCAT(nombre,' ',apellido) AS nom_user FROM usuario WHERE id = n.usuario_id) AS usuario 
        FROM nota n 
        inner join edicion e on (e.id = n.edicion_id)
        INNER JOIN relacion_tag r ON (n.id = r.nota_id AND r.tipo = 5) 
        WHERE $fil_habi r.tag_id=207 and n.lector=1 and n.edicion_id >= 3464"; //3716, local 3464

        if ($buscar != "") {
            $sql .= " and (n.antetitulo like '%$buscar%' or n.titulo like '%$buscar%')";
        }

        //if ($sort!="")
        //$sql .= " order by $sort $dir";
        //else
        $sql .= " order by n.edicion_id desc, n.id desc";

        $sql .= " limit $start,$limit";
        //echo "<pre>".print_r($sql,true)."</pre>";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            foreach ($res as $k => $v) {
                $res[$k]['seccion'] = $this->dame_secciones($v['id'], '', ',');
                $res[$k]['seccion_id'] = $this->dame_id_secciones($v['id'], '', ',');
            }
            $registros = $this->totalNotasLectores($buscar, $habilitada);
            return '({"total":"' . $registros . '","rows":' . json_encode($res) . '})';
        } else
            return '({"total":"0","rows":""})';
    }

    public function totalNotasLectores($buscar, $habilitada = "") {
        $fil_habi = "";
        if ($habilitada != "") {
            $fil_habi = " n.habilitada=$habilitada and ";
        }
        $sql = "select count(n.id) as cantidad from nota n 
		inner join relacion_tag r on (r.nota_id = n.id and r.tipo=5) 
		where $fil_habi r.tag_id=102 and n.lector=1 and n.edicion_id >= 3716"; //3716, local 2716

        if ($buscar != "") {
            $sql .= " and (n.antetitulo like '%$buscar%' or n.titulo like '%$buscar%')";
        }

        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res[0]['cantidad'];
    }

    /* Para preview de nota */

    public function traer_datos_nota_preview($nota_id) {
        $sql = "select n.id,TRIM(n.titulo) AS titulo, TRIM(n.antetitulo) AS antetitulo,n.sumario,
		n.texto, n.habilitada, n.edicion_id, n.um, n.fechaAlta, n.permisocoment as perm_coment, n.url, 
		n.agregado, n.fechaInicio as fechaInicioField, n.fechaVencimiento as fechaFinField, n.vista, 
		DATE_FORMAT(e.fecha, '%d-%m-%Y') as fecha_edicion 
		from nota n 
		inner join edicion e on (e.id = n.edicion_id) 
		where n.id = " . $nota_id;

        $query = $this->db->query($sql);
        $num = $query->num_rows();
        if ($num > 0) {
            $res = $query->result_array();
            $resultado = $res[0];
            $resultado['seccion'] = $this->dame_array_secciones($resultado['id'], '', ',');
            $resultado['temas'] = $this->dame_array_temas($resultado['id'], '', ',');
            $resultado['tags'] = $this->dame_array_tags($resultado['id'], '', ',');
            $verifica = explode("xto>", $resultado['texto']);
            if (count($verifica) > 1) {
                $texto = $this->extraer_string($resultado['texto'], "<texto>", "</texto>");
                $tituloagregado = $this->extraer_string($resultado['texto'], "<tituloagregado>", "</tituloagregado>");
                $textoagregado = $this->extraer_string($resultado['texto'], "<textoagregado>", "</textoagregado>");
                if ($tituloagregado)
                    $resultado['tituloagregado'] = $tituloagregado;
                if ($textoagregado)
                    $resultado['textoagregado'] = $textoagregado;
                $resultado['texto'] = $texto;
            }
            $verifica_sumario = explode("umario>", $resultado['sumario']);
            if (count($verifica_sumario) > 1) {
                $sumario = $this->extraer_string($resultado['sumario'], "<sumario>", "</sumario>");
                $resultado['sumario'] = $sumario;
            }

            return $resultado;
        } else
            return false;
    }

    public function dameNotasRelacionadas($nota_id) {
        $sql = "SELECT n.id, n.titulo, n.url, 
					(select t.nombre 
					  from tag t 
					  inner join relacion_tag r on (t.id = r.tag_id and r.tipo=5) 
					  where r.nota_id = n.id limit 1) as seccion
				FROM nota n 
				inner join relacion re on (n.id = re.objeto_id and re.tipo = 32) 
				WHERE re.nota_id = " . $nota_id . " and n.habilitada = 1";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    public function dameNotasRelacionadasConFoto($nota_id) {
        $sql = "SELECT n.id, n.titulo, n.url, 
					(select s.nombre 
					  from tag s 
					  inner join relacion_tag r on (s.id = r.tag_id and r.tipo=5) 
					  where r.nota_id = n.id and s.tipo_id = 5 limit 1) as seccion,
				(
					select CONCAT(f.ruta,f.imagen) 
					from foto f 
					inner join relacion r2 on (f.id = r2.objeto_id)
					where r2.tipo = 30 and r2.nota_id = n.id order by r2.destacado desc limit 1
		 		) as destacada
				FROM nota n 
				inner join relacion re on (n.id = re.objeto_id and re.tipo = 32) 
				WHERE re.nota_id = " . $nota_id . " and n.habilitada = 1 ORDER BY n.id DESC";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    /* Fin preview */

    public function dameNotasParaTaggingMasivo($edicion_id) {
        $sql = "select id, titulo, sumario, texto from nota where edicion_id = " . $edicion_id . " order by id desc";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    /* Para recargar ranking */

    public function dameUltimas($edicion_id, $limit) {
        $sql = "SELECT n.id, n.titulo, n.url,
		(select t.nombre 
		  from tag t 
		  inner join relacion_tag r on (r.tag_id = t.id and r.tipo = 5)
		  where r.nota_id = n.id 
		  limit 1) as seccion 
		  FROM nota n 
		  WHERE n.edicion_id = " . $edicion_id . " and n.norank = 0 and n.habilitada = 1
		  ORDER BY n.id desc 
		  LIMIT $limit";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    public function dameLeidas($edicion_id, $limit, $order) {
        $sql = "SELECT n.id, n.titulo, n.url,
		(select t.nombre 
		  from tag t 
		  inner join relacion_tag r on (r.tag_id = t.id and r.tipo = 5)
		  where r.nota_id = n.id 
		  limit 1) as seccion 
		  FROM nota n 
		  left JOIN nota_visitas nv on (n.id = nv.nota_id)
		  WHERE n.edicion_id = " . $edicion_id . " and n.norank = 0 and n.habilitada = 1 
		  ORDER BY nv.visitas $order 
		  LIMIT $limit";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    public function dameEnviadas($edicion_id, $limit, $order) {
        $sql = "SELECT n.id, n.titulo, n.url,
		(select t.nombre 
		  from tag t 
		  inner join relacion_tag r on (r.tag_id = t.id and r.tipo = 5)
		  where r.nota_id = n.id 
		  limit 1) as seccion 
		  FROM nota n 
		  left join nota_visitas nv on (n.id = nv.nota_id)
		  WHERE n.edicion_id = " . $edicion_id . " and n.norank = 0 and n.habilitada = 1 
		  ORDER BY nv.enviadas $order 
		  LIMIT $limit";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    public function dameMasComentadas($edicion_id, $limit) {
        $sql = "SELECT n.id, n.titulo, n.url,
		(select t.nombre 
		  from tag t 
		  inner join relacion_tag r on (r.tag_id = t.id and r.tipo = 5)
		  where r.nota_id = n.id 
		  limit 1) as seccion 
		  FROM nota n 
		  LEFT JOIN 
		  (
		  	select count(c.id) as comentarios, c.objeto_id 
		  	from comentario_min c 
		  	inner join nota ta on (ta.id = c.objeto_id and c.tipo = 32)
		  	where ta.edicion_id = " . $edicion_id . " 
		  	group by c.objeto_id 
		  	order by comentarios desc 
		  	limit 150
		  ) as com_nota on (com_nota.objeto_id = n.id)
		  WHERE n.edicion_id = " . $edicion_id . " and n.norank = 0 and n.habilitada = 1 
		  ORDER BY com_nota.comentarios desc 
		  LIMIT $limit";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    /* Fin recarga ranking */

    public function dameNotasEdicion($edicion_id) {
        $sql = "SELECT n.id, n.titulo, n.url,
			(SELECT t.nombre 
			  FROM tag t 
			  INNER JOIN relacion_tag r ON (r.tag_id = t.id AND r.tipo = 5)
			  WHERE r.nota_id = n.id 
			  LIMIT 1) AS seccion, 
			  (
				SELECT CONCAT(f.ruta,f.imagen) 
				FROM foto f 
				INNER JOIN relacion r2 ON (f.id = r2.objeto_id)
				WHERE r2.tipo = 30 AND r2.nota_id = n.id ORDER BY r2.destacado DESC LIMIT 1
			 ) AS img 
			  FROM nota n 
			  WHERE n.edicion_id = " . $edicion_id . " AND n.habilitada = 1 
			  ORDER BY n.id DESC";

        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    public function dameVisitasComentariosNotaSolr($edicion_id) {
        if ($edicion_id > 0) {
            $sql = "select n.id, nv.visitas, 
			(select count(id) as cant from comentario_min where objeto_id = n.id and tipo = 32) as comentarios 
			from nota n 
			inner join nota_visitas nv on (nv.nota_id = n.id) 
			where n.edicion_id = " . $edicion_id;
            $query = $this->db->query($sql);
            if ($query->num_rows() > 0) {
                $res = $query->result_array();
                return $res;
            } else
                return false;
        } else
            return false;
    }

    //public function dameVisitasPorTiempo($tiempo,$dir,$limit,$sort,$start){
    public function dameVisitasPorTiempo($id_edicion_viejo, $id_edicion_hoy, $dir, $limit, $sort, $start) {
        $sql = "select n.id, n.titulo, 
			(SELECT t.nombre FROM tag t 
	  		INNER JOIN relacion_tag rt ON (rt.tag_id = t.id AND rt.tipo=5) 
	  		WHERE rt.nota_id = n.id LIMIT 1) AS seccion, 
			DATE_FORMAT(e.fecha,'%d/%m/%Y') as fecha , nv.visitas , CONCAT(u.nombre,' ',u.apellido) AS usuario
	 		FROM nota n 
	 		INNER JOIN edicion e ON (e.id = n.edicion_id) 
	 		INNER JOIN nota_visitas nv ON (nv.nota_id = n.id) 
	 		INNER JOIN usuario_historial uh ON (uh.objeto_id = n.id AND uh.operacion_id=3) 
	 		INNER JOIN usuario u ON (u.id = uh.usuario_id) 
	 		WHERE n.habilitada = 1 AND n.um = 1 ";

        //NUEVO
        $sql .= " AND n.edicion_id BETWEEN " . $id_edicion_viejo . " AND " . $id_edicion_hoy . " ";

        /* 	//1: ULTIMO MES
          if($tiempo==1)
          $sql.=" AND e.fecha BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND CURDATE() ";

          //2: ULTIMOS 6 MESES
          if($tiempo==2)
          $sql.=" AND e.fecha BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 MONTH) AND CURDATE() ";

          //3: ULTIMO AÑO
          if($tiempo==3)
          $sql.=" AND e.fecha BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 YEAR) AND CURDATE() ";

          //4: ULTIMOS 5 AÑOS
          if($tiempo==4)
          $sql.=" AND e.fecha BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 YEAR) AND CURDATE() ";
         */

        $sql .= " ORDER BY nv.visitas " . $dir;
        $sql .= " LIMIT " . $start . ", " . $limit;

        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    public function dameObjetosRelacionados($nota_id) {

        $sql = "select r.id, r.nota_id,r.objeto_id, t.nombre from relacion r
			inner join tipos t on (t.id = r.tipo)
 			where nota_id = " . $nota_id;

        $sql .= " and (t.id=2 or t.id=30 or t.id=7 or t.id=6 or t.id=29)";

        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    public function borrar_periodistas_relacionados($nota_id, $tipo) {
        $this->db->where('nota_id', $nota_id);
        $this->db->where('tipo', $tipo);
        $this->db->delete('relacion');
    }

    public function dame_id_periodistas($id_nota, $pre, $post) {

        $sql = "select u.id as nombre 
		from usuario u 
		inner join 
		(select objeto_id from relacion
		where  nota_id = " . $id_nota . " and tipo=4) 
		as temp_us on (temp_us.objeto_id = u.id)";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        $concat = "";
        if (count($res) > 0) {
            $i = 0;
            foreach ($res as $k => $v) {
                if ($i == 0) {
                    $i = 1;
                    $concat .= $pre . $v['nombre'];
                } else
                    $concat .= $post . $pre . $v['nombre'];
            }
        }
        return $concat;
    }

    public function dame_id_subsecciones($id_nota, $pre, $post) {

        $sql = "select t.id as nombre 
		from tag t 
		inner join 
		(select tag_id from relacion_tag
		where tipo=5 and nota_id = " . $id_nota . ") as temp_sec on (temp_sec.tag_id = t.id)
		where padre_id <> 0";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        $concat = "";
        if (count($res) > 0) {
            $i = 0;
            foreach ($res as $k => $v) {
                if ($i == 0) {
                    $i = 1;
                    $concat .= $pre . $v['nombre'];
                } else
                    $concat .= $post . $pre . $v['nombre'];
            }
        }
        return $concat;
    }

    public function dameImagenesDestacadas($array_nota_id) {
        $array_nota_id = implode(',', $array_nota_id);
        $sql = "select r.nota_id,CONCAT( f.ruta , 'tmb2_' , f.imagen ) as imagen 
		from foto f 
		inner join relacion r on (f.id = r.objeto_id and r.tipo = 30) 
		where r.nota_id in (" . $array_nota_id . ") 
		order by r.destacado desc";

        $query = $this->db->query($sql);
        //echo 'query: '.$this->db->last_query();
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return "";
    }

    public function dameHoraUM($nota_id = 0) {
        if ($nota_id > 0) {
            $sql = "select DATE_FORMAT(fechamod,'%H:%I') as hora, um from nota where id = " . $nota_id;
            $query = $this->db->query($sql);
            if ($query->num_rows() > 0) {
                $res = $query->result_array();
                return $res[0];
            } else
                return false;
        } else
            return false;
    }

    public function dameImagenDestacada($nota_id) {
        //$array_nota_id = implode(',',$array_nota_id);
        $sql = "select CONCAT( f.ruta , 'tmb2_' , f.imagen ) as imagen 
		from foto f 
		inner join relacion r on (f.id = r.objeto_id and r.tipo = 30) 
		where r.nota_id = " . $nota_id . "
		order by r.destacado desc";

        $query = $this->db->query($sql);
        //echo 'query: '.$this->db->last_query();
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0]['imagen'];
        } else
            return "";
    }

    public function dameCantidadCompartidas($nota_id) {

        $sql = "select compartidas from nota_visitas where nota_id =" . $nota_id;
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0]['compartidas'];
        } else
            return "";
    }

    public function dameSeccionParaBlogPreview($id) {

        $sql = "select t.id as id_seccion_blog 
				from tag t 
				inner join 
				(select tag_id from relacion_tag
				where tipo=5 and nota_id = " . $id . ") as temp_sec 
				on (temp_sec.tag_id = t.id)";

        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0]['id_seccion_blog'];
        } else
            return "";
    }

    public function dameNotaMasOpinada($edicion_id, $fecha_edicion) {
        $sql = "SELECT n.id, n.titulo, n.url, n.sumario as bajada, 
				(SELECT t.nombre 
				  FROM tag t 
				  INNER JOIN relacion_tag r ON (r.tag_id = t.id AND r.tipo = 5)
				  WHERE r.nota_id = n.id 
				 LIMIT 1) AS seccion, 
				(
				 SELECT CONCAT(f.ruta,'tmb2_',f.imagen) 
				 FROM foto f 
				 INNER JOIN relacion r2 ON (f.id = r2.objeto_id)
				 WHERE r2.tipo = 30 AND r2.nota_id = n.id ORDER BY r2.destacado DESC LIMIT 1
				) AS img
				FROM nota n 
				INNER JOIN 
				(
				  SELECT COUNT(id) AS comentarios, objeto_id 
				  FROM comentario_min WHERE tipo=32 AND DATE(fecha) >= '" . $fecha_edicion . "' GROUP BY objeto_id	
				) AS com ON (com.objeto_id = n.id) 
				WHERE n.edicion_id = " . $edicion_id . " AND n.habilitada = 1 
				ORDER BY com.comentarios DESC, n.id DESC 
				LIMIT 1";

        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0];
        } else
            return false;
    }

    public function dameNotasMasVistasSabatino($edicion_id) {

        $sql = "SELECT n.id, n.titulo, n.antetitulo, n.sumario, n.url, DATE_FORMAT(fechaAlta,'%d/%m/%Y') AS fecha, 
			(SELECT t.nombre FROM tag t INNER JOIN relacion_tag r ON (r.tag_id=t.id AND r.tipo=5) WHERE r.nota_id = n.id AND t.habilitado=1 LIMIT 1) AS seccion 
		FROM nota n 
		INNER JOIN nota_visitas v ON (v.nota_id = n.id)
		WHERE edicion_id = '" . $edicion_id . "'
		AND n.habilitada = 1 AND n.norank = 0
		ORDER BY v.visitas DESC, n.id DESC 
		LIMIT 3";

        $query = $this->db->query($sql);

        //echo $this->db->last_query()."<br /><br />";

        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

    public function dameNotasMailingTarde($edicion_id, $edicion_fecha, $seccion_id) {
        $sql = "select n.id, n.titulo, n.url, date_format(fechaAlta, '%h:%i') as horaFechaAlta 
		from nota n  
		inner join relacion_tag r on (r.nota_id = n.id and r.tipo = 5) 
		where n.edicion_id = " . $edicion_id . " and r.tag_id = $seccion_id 
		and n.fechaAlta >= '" . $edicion_fecha . " 08:30' and n.um = 1";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res;
        } else
            return false;
    }

}

?>

Anon7 - 2022
AnonSec Team