PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/wallfriends.php

https://github.com/alugo/Goteo
PHP | 270 lines | 155 code | 34 blank | 81 comment | 35 complexity | 1aefa58a2d30402964a61580db4c8fd7 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /*
  3. * Copyright (C) 2012 Platoniq y Fundación Fuentes Abiertas (see README for details)
  4. * This file is part of Goteo.
  5. *
  6. * Goteo is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Goteo is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with Goteo. If not, see <http://www.gnu.org/licenses/agpl.txt>.
  18. *
  19. */
  20. namespace Goteo\Library {
  21. use Goteo\Model\Invest,
  22. Goteo\Model\Project,
  23. Goteo\Core\Exception;
  24. class WallFriends {
  25. public $project = '';
  26. public $investors = array();
  27. public $avatars = array(); //listado de avatars válidos con su multiplicador de tamaño
  28. public $max_multiplier = 32; //màxim multiplicador de tamanys
  29. public $w_size = 32; //tamaño (width) de la imagen mínima en pixeles
  30. public $h_size = 32; //tamaño (height) de la imagen mínima en pixeles
  31. public $w_padding = 0;
  32. public $h_padding = 0;
  33. public $show_title = true; //enseña o no el titulo del widget (publi goteo)
  34. /**
  35. *
  36. * @param type mixed $id Identificador
  37. * @return type object Objeto
  38. */
  39. public function __construct ($id, $all_avatars=true, $with_title = true) {
  40. if($this->project = Project::get($id)) {
  41. $this->show_title = $with_title;
  42. $this->investors = $this->project->agregateInvestors();
  43. $avatars = array();
  44. foreach($this->investors as $i) {
  45. if($i->avatar->id != 1 || $all_avatars)
  46. $avatars[$i->user] = $i->amount;
  47. }
  48. $this->avatars = self::pondera($avatars,$this->max_multiplier);
  49. //arsort($this->avatars);
  50. $keys = array_keys( $this->avatars );
  51. shuffle( $keys );
  52. $this->avatars = array_merge( array_flip( $keys ) , $this->avatars );
  53. //print_r($this->project);die;
  54. }
  55. else {
  56. return false;
  57. }
  58. }
  59. /**
  60. * Pondera un array amb valor minim 1 i valor maxim ?
  61. * */
  62. public static function pondera($array = array(),$max_multiplier = 4) {
  63. $new = array();
  64. $min = min($array);
  65. $max = max($array);
  66. foreach($array as $i => $n) {
  67. //minim 1, màxim el que toqui
  68. $num = $n/$min;
  69. //apliquem alguna funcio que "comprimeixi" els resultats
  70. $num = round(sqrt($num));
  71. if($num > $max_multiplier) $num = $max_multiplier;
  72. $new[$i] = $num;
  73. }
  74. return $new;
  75. }
  76. /**
  77. * Retorna les imatges i contingut en html
  78. *
  79. * $num_icons: el numero de icones per fila del widget
  80. * */
  81. public function html_content($num_icons = 19) {
  82. $URL = (NODE_ID != GOTEO_NODE) ? NODE_URL : SITE_URL;
  83. $ret = array();
  84. foreach($this->avatars as $user => $mult) {
  85. $style = '';
  86. $w = $this->w_size;
  87. $h = $this->h_size;
  88. $src = $URL . '/image/2/'."$w/$h";
  89. if($this->investors[$user]->avatar instanceof \Goteo\Model\Image) {
  90. if ($this->investors[$user]->avatar->id == 1) {
  91. $noface = \Goteo\Model\Image::get(2);
  92. $src = $noface->getLink($w,$h, true);
  93. } else {
  94. $src = $this->investors[$user]->avatar->getLink($w,$h, true);
  95. }
  96. }
  97. $img = '<a href="'.$URL.'/user/profile/'.$user.'"><img'.$style.' src="' . $src . '" alt="'.$this->investors[$user]->name.'" title="'.$this->investors[$user]->name.'" /></a>';
  98. for($i = 0; $i<$mult+1; $i++) {
  99. $ret[] = $img;
  100. $total = count($ret);
  101. //cas que es posicioni a partir de la segona columna
  102. if($num_icons > 14) {
  103. //final de 1a fila, 2a columna
  104. if(in_array($total , array($num_icons + 1, $num_icons * 2 - 12, $num_icons * 3 - 25))) {
  105. $ret[] = '<div class="c"></div>';
  106. }
  107. if(in_array($total, array($num_icons * 5 - 38, $num_icons * 6 - 49, $num_icons * 7 - 60))) {
  108. $ret[] = '<div class="a"></div>';
  109. }
  110. if(in_array($total, array($num_icons * 5 - 36, $num_icons * 6 - 47, $num_icons * 7 - 58))) {
  111. $ret[] = '<div class="b"></div>';
  112. }
  113. if(in_array($total , array($num_icons * 9 - 71,$num_icons * 10 - 84))) {
  114. $ret[] = '<div class="d"></div>';
  115. }
  116. }
  117. //es posiciona a partir de la primera columna (minim tamany possible)
  118. else {
  119. if($total == $num_icons) {
  120. $ret[] = '<div class="c"></div><div class="c"></div><div class="c"></div>';
  121. }
  122. if($total == $num_icons * 2 + 1) {
  123. $ret[] = '<div class="a"></div>';
  124. }
  125. if(in_array($total, array($num_icons * 2 + 3, $num_icons * 2 + 5))) {
  126. $ret[] = '<div class="b"></div><div class="a"></div>';
  127. }
  128. if($total == $num_icons * 2 + 7) {
  129. $ret[] = '<div class="b"></div>';
  130. }
  131. if($total == $num_icons * 3 + 8) {
  132. $ret[] = '<div class="d"></div><div class="d"></div>';
  133. }
  134. }
  135. }
  136. }
  137. return $ret;
  138. /*
  139. //afegim el logo al final de tot
  140. $final = array();
  141. $total = count($ret);
  142. $cols = floor(($total + 3*14 + 3*13 + 2*14)/$num_icons);
  143. if($num_icons > 14) {
  144. foreach($ret as $i => $v) {
  145. if(in_array($i, array($num_icons*($cols-1) - 103,$num_icons*$cols - 107))) {
  146. $final[] = '<div class="e"></div>';
  147. }
  148. $final[] = $v;
  149. }
  150. }
  151. else {
  152. foreach($ret as $i => $v) {
  153. if(in_array($i, array($num_icons*($cols-2) - 94,$num_icons*($cols-1) - 98))) {
  154. //if(in_array($i, array($num_icons*($cols-2) - 94))) {
  155. $final[] = '<div class="e"></div>';
  156. }
  157. $final[] = $v;
  158. }
  159. }
  160. return $final;
  161. */
  162. }
  163. /**
  164. * Muestra un div con las imagenes en pantalla.
  165. * @param type int $width
  166. * @param type int $height
  167. *
  168. */
  169. public function html($width = 608, $extern = false) {
  170. $URL = (NODE_ID != GOTEO_NODE) ? NODE_URL : SITE_URL;
  171. // si es externo, abrimos en una ventana nueva
  172. $target = $extern ? ' target="_blank"' : '';
  173. //cal que siguin multiples del tamany
  174. $wsize = $this->w_size + $this->w_padding * 2;
  175. $hsize = $this->h_size + $this->h_padding * 2;
  176. //num icones per fila
  177. $num_icons = floor($width / $wsize);
  178. //tamany minim
  179. if($num_icons < 15) $num_icons = 14;
  180. //amplada efectiva
  181. $width = $wsize * $num_icons;
  182. // estilos estaticos
  183. $style = '<link rel="stylesheet" type="text/css" href="/view/css/wof.css" />';
  184. // estilos dinamicos
  185. $style .= '<style type="text/css">';
  186. $style .= "div.wof>div.ct>a>img {border:0;width:{$this->w_size}px;height:{$this->h_size}px;display:inline-block;padding:{$this->h_padding}px {$this->w_padding}px {$this->h_padding}px {$this->w_padding}px}";
  187. $style .= "div.wof>div.ct>div.a {display:inline-block;width:" . ($wsize * 5) . "px;height:" . $hsize . "px}";
  188. $style .= "div.wof>div.ct>div.b {display:inline-block;width:" . ($wsize * 8) . "px;height:" . $hsize . "px}";
  189. $style .= "div.wof>div.ct>div.c {display:inline-block;width:" . ($wsize * 14) . "px;height:" . $hsize . "px}";
  190. $style .= "div.wof>div.ct>div.d {display:inline-block;width:" . ($wsize * 14) . "px;height:" . $hsize . "px}";
  191. $style .= "div.wof>div.ct>div.e {display:inline-block;width:" . ($wsize * 4) . "px;height:" . $hsize . "px}";
  192. $style .= "div.wof>div.ct>div.i {overflow:hidden;padding:0;margin:0;position:absolute;height:" . ($hsize * 3) . "px;background:#fff;left:" . ($num_icons < 15 ? "0" : $wsize) . "px;top:" . ($hsize * 5) . "px}";
  193. $style .= "div.wof>div.ct>div.b.i {left:" . ($wsize * ($num_icons <15 ? 6 : 7)) . "px;top:" . ($hsize * 5) . "px}";
  194. $style .= "div.wof>div.ct>div.c.i {left:" . ($num_icons < 15 ? "0" : $wsize) . "px;top:" . $hsize . "px}";
  195. $style .= "div.wof>div.ct>div.d.i {left:" . ($num_icons < 15 ? "0" : $wsize) . "px;top:" . ($hsize * 9) . "px;height:" . ($hsize * 2) . "px;background:url(".$URL."/view/css/project/widget/wof_sup_bck.png) no-repeat}";
  196. $content = $this->html_content($num_icons);
  197. $cols = floor((count($content) + 3*13 + 3*11 + 2*13 +2*3) / $num_icons);
  198. $logotop = ($hsize * ($cols-2));
  199. if ($logotop < 385) $logotop = 385;
  200. $style .= "div.wof>div.ct>div.e.i {left:" . (($num_icons - 5) * $wsize) . "px;top:" . ($logotop) . "px;height:" . ($hsize * 2) . "px;background:#fff url(".$URL."/view/css/project/widget/wof_logo.png) center no-repeat}";
  201. $style .= "div.wof>div.ct>div.c>div.c1 {float:left;height:" . ($wsize * 3) . "px;width:" . ($wsize * 3) . "px}";
  202. $style .= "div.wof>div.ct>div.c>div.c2 {float:right;height:" . ($wsize * 3) . "px;width:" . ($wsize * 11) . "px}";
  203. $style .= "</style>";
  204. $title = '<h2><a href="'.$URL.'/project/'.$this->project->id.'"'.$target.'>'.Text::get('wof-title').'</a><a href="'.$URL.'" class="right"'.$target.'>goteo.org</a></h2>';
  205. $info = '';
  206. if ($this->project->status == 3) {
  207. $info .= '<a class="expand" href="'.$URL.'/project/'.$this->project->id.'/invest" title="'.Text::get('wof-here').'"'.$target.'></a>';
  208. }
  209. //num finançadors
  210. $info .= '<div class="a i"><h3><a href="'.$URL.'/project/'.$this->project->id.'/supporters"'.$target.'>' . $this->project->num_investors . '</a></h3><p><a href="'.$URL.'/project/'.$this->project->id.'/supporters"'.$target.'>'.Text::get('project-view-metter-investors').'</a></p></div>';
  211. //financiacio, data
  212. $info .= '<div class="b i"><h3><a href="'.$URL.'/project/'.$this->project->id.'/needs"'.$target.'>' . \amount_format($this->project->invested,0,'',','). '<img src="'.$URL.'/view/css/euro/violet/yl.png" alt="&euro;"></a></h3>';
  213. $info .= '<p><a href="'.$URL.'/project/'.$this->project->id.'/needs">' . Text::get('project-view-metter-days') . " {$this->project->days} " . Text::get('regular-days') .'</a></p></div>';
  214. //impulsores, nom, desc
  215. $info .= '<div class="c i">';
  216. $info .= '<div class="c1"><p><a href="'.$URL.'/user/'.$this->project->owner.'"'.$target.'><img src="'.$URL.'/image/'.$this->project->user->avatar->id.'/56/56/1" alt="'.$this->project->user->name.'" title="'.$this->project->user->name.'"><br />' . Text::get('regular-by') . ' ' . $this->project->user->name . '</a></p></div>';
  217. $info .= '<div class="c2"><h3><a href="'.$URL.'/project/'.$this->project->id.'"'.$target.'>' . $this->project->name . '</a></h3><p><a href="'.$URL.'/project/'.$this->project->id.'"'.$target.'>'.$this->project->subtitle.'</a></p></div>';
  218. $info .= '</div>';
  219. //apoyar el proyecto
  220. $info .= '<div class="d i">';
  221. if ($this->project->status == 3) {
  222. $info .= '<p>'.Text::get('wof-join-group').'</p>';
  223. $info .= '<a href="'.$URL.'/project/'.$this->project->id.'/invest"'.$target.'>'.Text::get('wof-support').'</a>';
  224. } else {
  225. $info .= '<p>'.Text::get('wof-join-comunity').'</p>';
  226. $info .= '<a href="'.$URL.'/project/'.$this->project->id.'/updates"'.$target.'>'.Text::get('wof-follow').'</a>';
  227. }
  228. $info .= '</div>';
  229. //logo
  230. $info .= '<div class="e i">';
  231. $info .= '</div>';
  232. return $style . '<div class="wof" style="width:'.$width.'px;">' . ($this->show_title ? $title : '') . '<div class="ct">' . $info . implode("",$content).'</div></div>';
  233. }
  234. }
  235. }