PageRenderTime 96ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/gantry/admin/forms/fields/positions.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 386 lines | 283 code | 87 blank | 16 comment | 71 complexity | 34691c28750a17a0ce3a355cc20d511b MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * @version 3.2.22 August 3, 2012
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. defined('GANTRY_VERSION') or die();
  9. /**
  10. * @package gantry
  11. * @subpackage admin.elements
  12. */
  13. gantry_import('core.config.gantryformfield');
  14. class GantryFormFieldPositions extends GantryFormField {
  15. protected $type = 'positions';
  16. protected $basetype = 'hidden';
  17. protected $maxGrid;
  18. protected $layoutSchemas;
  19. protected $defaultMainbodySchemas;
  20. protected $template;
  21. protected $default;
  22. protected $mainbodySchemas;
  23. protected $defaultCount;
  24. var $schemas = array("1", "2", "3", "4", "5", "6"), $words = array("2", "3", "4", "5", "6", "7", "8", "9"), $combinations, $customCombinations, $settings, $keyName = "";
  25. public $position_info;
  26. public function getInput(){
  27. global $gantry;
  28. gantry_import('core.gantrypositions');
  29. $output = ''; $lis = ''; $currentScheme = '';
  30. $this->template = end(explode(DS, $gantry->templatePath));
  31. $this->default = explode(',', str_replace(' ', '', $this->element['default']));
  32. $this->defaultCount = count($this->default);
  33. // [0] => schemas | [1] => words | [2] => maxgrid | [3] => type
  34. //$opts = $node->children();
  35. $this->maxGrid = (int) $gantry->get('grid_system');
  36. if (!$this->maxGrid) $this->maxGrid = 12;
  37. if ($this->element->grid){
  38. $this->maxGrid = (int)$this->element->grid;
  39. } else {
  40. $this->maxGrid = (int) $gantry->get('grid_system');
  41. }
  42. $this->words = explode(",", $this->element->words);
  43. $this->schemas = explode(",", $this->element->schemas);
  44. $this->type = ($this->element->type) ? $this->element->type : 'regular';
  45. $this->layoutSchemas = $gantry->layoutSchemas[$this->maxGrid];
  46. $this->defaultMainbodySchemas = $gantry->mainbodySchemas;
  47. $this->mainbodySchemas = $gantry->mainbodySchemasCombos[$this->maxGrid];
  48. $this->keyName = '';
  49. if ($this->type == 'custom') {
  50. $tmpName = str_replace("Position", "Schemas", $this->element['name']);
  51. $tmpSchema = $this->$tmpName;
  52. $this->keyName = key($tmpSchema[1][0]);
  53. }
  54. if (!defined('GANTRY_CSS')) {
  55. $gantry->addStyle($gantry->gantryUrl.'/admin/widgets/gantry.css');
  56. define('GANTRY_CSS', 1);
  57. }
  58. if (!defined('POSITIONS')) {
  59. if (!defined('GANTRY_SLIDER')) {
  60. $gantry->addScript($gantry->gantryUrl.'/admin/widgets/slider/js/slider.js');
  61. define('GANTRY_SLIDER', 1);
  62. }
  63. $gantry->addScript($gantry->gantryUrl.'/admin/widgets/slider/js/unserialize.js');
  64. $gantry->addScript($gantry->gantryUrl.'/admin/widgets/positions/js/positions-utils.js');
  65. $this->settings = array("words" => $this->words, "schemas" => $this->schemas, "maxGrid" => $this->maxGrid);
  66. if ($this->type == 'custom') $this->customCombinations = $this->getCombinations();
  67. else $this->combinations = $this->getCombinations();
  68. define('POSITIONS', 1);
  69. }
  70. $posName = ($this->element['name'] == "mainbodyPosition") ? "sidebar" : $this->position_info->id;
  71. $realCount = $gantry->countModules($posName);
  72. if ($posName == 'sidebar') $realCount += 1;
  73. if ($realCount > 0) {
  74. if (!in_array($realCount, $this->schemas)) $realCount = $this->schemas[0];
  75. $this->default = $this->oneCharConversion($this->layoutSchemas[$realCount]);
  76. $this->defaultCount = $realCount;
  77. }
  78. // if the same type of combinations are requested, use the cached ones, otherwise get the new set
  79. if ($this->type != "custom" && ($this->words != $this->settings["words"] || $this->schemas != $this->settings["schemas"] || $this->maxGrid != $this->settings["maxGrid"])) {
  80. $this->combinations = $this->getCombinations();
  81. }
  82. if ($this->type == "custom") $this->customCombinations = $this->getCombinations();
  83. if (!in_array((string)$this->defaultCount, $this->schemas)) $this->defaultCount = (int)$this->schemas[0];
  84. $i = 0;
  85. $max_positions = isset($this->position_info)?$this->position_info->max_positions:false;
  86. if (!$max_positions) $max_positions = 6;
  87. foreach($this->schemas as $scheme) {
  88. $active = "";
  89. if ($i >= $max_positions) break;
  90. if ((int)$scheme == $this->defaultCount) {
  91. $active = ' class="active"';
  92. $currentLayout = $scheme;
  93. }
  94. $lis .= '<li'.$active.'><a href="#"><span>'.$scheme.'</span></a></li>';
  95. $i++;
  96. }
  97. $scriptinit = $this->sliderInit($this->id);
  98. $gantry->addDomReadyScript($scriptinit);
  99. $gantry->addDomReadyScript($this->showmax($this->id));
  100. $letters = array('a', 'b', 'c', 'd', 'e', 'f');
  101. $output = '
  102. <div class="wrapper">
  103. <div id="'.$this->id.'-grp" class="g-position">
  104. <div class="navigation">
  105. <span class="title">Positions:</span>
  106. <ul class="list">'.$lis.'</ul>
  107. </div>
  108. <div class="clr"></div>
  109. <div id="'.$this->id.'-wrapper" class="col'.$this->maxGrid.' miniatures">
  110. <div class="mini-container layout-grid-'.$currentLayout.'">'."\n";
  111. for ($i = 0; $i < $max_positions; $i++) {
  112. $output .= "<div class=\"mini-grid mini-grid-2\">".$letters[$i]."</div>\n";
  113. }
  114. $output .= '
  115. </div>
  116. <div class="clr"></div>
  117. <div class="position">
  118. <div class="position2"></div>
  119. <div class="knob"></div>
  120. </div>
  121. </div>
  122. <!-- <div class="current-positions">
  123. <span class="title">'.str_replace('%d', "<strong class='".$this->id."-currentPosition countPositions'>".$realCount."</strong>", JText::_('Current Positions')).'</span>
  124. </div>-->
  125. <input type="hidden" id="'.$this->id.'" name="'.$this->name.'" value=\'';
  126. $output .= $this->value;
  127. $output .= '\' />
  128. </div>
  129. </div>
  130. ';
  131. return $output;
  132. }
  133. function permutations($letters, $num, $filter = 12) {
  134. // hardcoded cases for speed optimization
  135. $letter0 = base_convert($letters{0}, 24, 10);
  136. $letter1 = base_convert($this->lastchar($letters), 24, 10);
  137. if ($letter0 + $letter1 > $filter) return array();
  138. if ($filter == 12 && $num == 6) return array("222222");
  139. if ($num == 1) return $this->oneCharConversion(array($filter));
  140. $last = str_repeat($letters{0}, $num);
  141. $result = array();
  142. while($last != str_repeat($this->lastchar($letters), $num)) {
  143. $tmp = 0;
  144. for ($i = 0; $i < strlen($last); $i++) $tmp += base_convert($last[$i], 24, 10);
  145. if ($tmp == $filter) $result[] = $last;
  146. $last = $this->char_add($letters, $last ,$num-1);
  147. }
  148. $tmp = 0;
  149. for ($i = 0; $i < strlen($last); $i++) $tmp += base_convert($last[$i], 24, 10);
  150. if ($tmp == $filter) $result[] = $last;
  151. return $result;
  152. }
  153. function char_add($digits, $string, $char) {
  154. if ($string{$char} <> $this->lastchar($digits)) {
  155. $string{$char} = $digits{strpos($digits, $string{$char}) + 1};
  156. return $string;
  157. } else {
  158. $string = $this->changeall($string, $digits{0}, $char);
  159. return $this->char_add($digits, $string, $char - 1);
  160. }
  161. }
  162. function lastchar($string) {
  163. return $string{strlen($string)-1};
  164. }
  165. function changeall($string, $char, $start = 0, $end = 0) {
  166. if ($end == 0) $end = strlen($string) - 1;
  167. for ($i = $start; $i <= $end; $i++) {
  168. $string{$i} = $char;
  169. }
  170. return $string;
  171. }
  172. function tryCache($implode, $scheme, $words, $grid = 12) {
  173. global $gantry;
  174. $md5 = md5($grid . implode("", $words) . $scheme);
  175. $data = $gantry->positions[$grid]->get($md5);
  176. if (null == $data) {
  177. $permutation = $this->permutations($implode, (int) $scheme, $grid);
  178. $save = array();
  179. $save[$grid][$implode][$scheme] = $permutation;
  180. //file_put_contents($file, serialize($save));
  181. $gantry->positions[$grid]->set($md5, serialize($save));
  182. return $permutation;
  183. } else {
  184. $unserial = unserialize($data);
  185. return $unserial[$grid][$implode][$scheme];
  186. }
  187. }
  188. function getCombinations() {
  189. global $gantry;
  190. if ($this->type == 'custom') return $this->getCustomCombinations();
  191. $grid = $this->maxGrid;
  192. $words = $this->words;
  193. $sets = $this->schemas;
  194. $result = "{";
  195. $words = $this->oneCharConversion($words);
  196. foreach($sets as $set) {
  197. $implode = implode("", $words);
  198. $output[$grid][$implode][$set] = $this->tryCache($implode, (int) $set, $words, $grid);
  199. $current = $output;
  200. $tmp = $current[$grid][$implode][$set];
  201. sort($tmp);
  202. $result .= "'$set': ['".implode("', '", $tmp)."'],";
  203. }
  204. $result = substr($result, 0, -1) . "}";
  205. return $result;
  206. }
  207. function getCustomCombinations() {
  208. $sets = $this->schemas;
  209. $name = str_replace("Position", "Schemas", $this->element['name']);
  210. $results = "{";
  211. $keysref = "{";
  212. foreach($this->$name as $key => $set) {
  213. $results .= "'$key': [";
  214. $keysref .= "'$key': [";
  215. foreach($set as $combination) {
  216. $combination = $this->oneCharConversion($combination);
  217. $results .= "'" . implode("", $combination) . "', ";
  218. $keysref .= "['" . implode("', '", array_keys($combination)) . "'], ";
  219. }
  220. $results = substr($results, 0, -2) . "],";
  221. $keysref = substr($keysref, 0, -2) . "],";
  222. }
  223. $results = substr($results, 0, -1) . "}";
  224. $keysref = substr($keysref, 0, -1) . "}";
  225. return array($results, $keysref);
  226. }
  227. function oneCharConversion($words, $decode = false) {
  228. $dummy = array();
  229. foreach($words as $key => $word) {
  230. if (!$decode) $dummy[$key] = base_convert((int) $word, 10, 24);
  231. else $dummy[$key] = base_convert((int) $word, 24, 10);
  232. }
  233. return $dummy;
  234. }
  235. function outputCombinations($type = 'combinations') {
  236. if (!is_array($this->combinations) && $this->type != 'custom') return $this->combinations;
  237. return ($type == 'combinations') ? $this->customCombinations[0] : $this->customCombinations[1];
  238. }
  239. function getLoadValue() {
  240. $defaultValue = array($this->defaultCount => $this->default);
  241. if ($this->type == 'custom') {
  242. $defaultValue = array($this->defaultCount => $this->defaultMainbodySchemas[$this->maxGrid][$this->defaultCount]);
  243. }
  244. if (preg_match("/{/", $this->value)) {
  245. $value = unserialize($this->value);
  246. if (isset($value[$this->maxGrid]))
  247. $value = $value[$this->maxGrid];
  248. else
  249. $value = $defaultValue;
  250. } else {
  251. $value = $defaultValue;
  252. }
  253. $merge = $value + $this->layoutSchemas;
  254. $result = "{";
  255. $keynames = '';
  256. if ($this->type == 'custom') {
  257. foreach($this->defaultMainbodySchemas[$this->maxGrid] as $key => $defaults) {
  258. if (!array_key_exists($key, $value)) {
  259. $value[$key] = $defaults;
  260. }
  261. }
  262. foreach($value as $key => $array) {
  263. $array = $this->oneCharConversion($array);
  264. $result .= $key . ': {';
  265. $result .= "'values': ['" . implode("", $array) . "'], ";
  266. $result .= "'keys': [";
  267. foreach($array as $mb => $arr) {
  268. $result .= '"'. $mb . '", ';
  269. }
  270. $result = substr($result, 0, -2);
  271. $result .= "]}, ";
  272. }
  273. } else {
  274. foreach($merge as $key => $array) {
  275. $array = $this->oneCharConversion($array);
  276. $result .= $key . ': [';
  277. $result .= "'".implode("", $array)."'";
  278. $result .= "], ";
  279. }
  280. }
  281. $result = substr($result, 0, -2);
  282. $result .= "}";
  283. return $result;
  284. }
  285. function sliderInit($name, $max = 12) {
  286. $name2 = str_replace("-", "_", $name);
  287. $slider = "document.id('".$name."-grp').getElement('.position')";
  288. $knob = "document.id('".$name."-grp').getElement('.knob')";
  289. $hidden = "document.id('".$this->id."')";
  290. $activeNav = array_search((string)$this->defaultCount, $this->schemas);
  291. // hidden, name, maxgrid, loadValue, keyName, type, combinations, defaultCount, schemas
  292. return "GantryPositions.add('".$this->id."', '".$name."', ".$this->maxGrid.", ".$this->getLoadValue().", '".$this->keyName."', '".$this->type."', ".$this->outputCombinations('combinations').", ".$this->outputCombinations('keys').", ".$activeNav.");";
  293. }
  294. function showmax($name) {
  295. $name2 = str_replace("-", "_", $name);
  296. return "GantryPositionsTools.showMax('".$name."', '".$name2."');";
  297. }
  298. }
  299. ?>