PageRenderTime 106ms CodeModel.GetById 41ms app.highlight 23ms RepoModel.GetById 37ms 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
  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 */
  8defined('GANTRY_VERSION') or die();
  9
 10
 11/**
 12 * @package     gantry
 13 * @subpackage  admin.elements
 14 */
 15gantry_import('core.config.gantryformfield');
 16
 17class GantryFormFieldPositions extends GantryFormField {
 18    protected $type = 'positions';
 19    protected $basetype = 'hidden';
 20
 21    protected $maxGrid;
 22    protected $layoutSchemas;
 23    protected $defaultMainbodySchemas;
 24    protected $template;
 25    protected $default;
 26    protected $mainbodySchemas;
 27    protected $defaultCount;
 28
 29
 30
 31    var $schemas = array("1", "2", "3", "4", "5", "6"), $words = array("2", "3", "4", "5", "6", "7", "8", "9"), $combinations, $customCombinations, $settings, $keyName = "";
 32
 33    public $position_info;
 34
 35	public function getInput(){
 36		global $gantry;
 37		gantry_import('core.gantrypositions');
 38		
 39		$output = ''; $lis = ''; $currentScheme = '';
 40
 41		$this->template = end(explode(DS, $gantry->templatePath));
 42		
 43		$this->default = explode(',', str_replace(' ', '', $this->element['default']));
 44		$this->defaultCount = count($this->default);
 45		
 46		// [0] => schemas | [1] => words | [2] => maxgrid | [3] => type
 47		//$opts = $node->children();
 48        
 49        $this->maxGrid = (int) $gantry->get('grid_system');
 50		if (!$this->maxGrid) $this->maxGrid = 12;
 51
 52		if ($this->element->grid){
 53			$this->maxGrid = (int)$this->element->grid;
 54		} else {
 55			$this->maxGrid = (int) $gantry->get('grid_system');
 56		}
 57
 58		
 59		$this->words = explode(",", $this->element->words);
 60		$this->schemas = explode(",", $this->element->schemas);
 61		$this->type = ($this->element->type) ? $this->element->type : 'regular';
 62		
 63		
 64		$this->layoutSchemas = $gantry->layoutSchemas[$this->maxGrid];
 65		$this->defaultMainbodySchemas = $gantry->mainbodySchemas;
 66		$this->mainbodySchemas = $gantry->mainbodySchemasCombos[$this->maxGrid];
 67		
 68		
 69		$this->keyName = '';
 70		if ($this->type == 'custom') {
 71			$tmpName = str_replace("Position", "Schemas", $this->element['name']);
 72			$tmpSchema = $this->$tmpName;
 73			$this->keyName = key($tmpSchema[1][0]);
 74		}
 75		
 76        if (!defined('GANTRY_CSS')) {
 77			$gantry->addStyle($gantry->gantryUrl.'/admin/widgets/gantry.css');
 78			define('GANTRY_CSS', 1);
 79		}
 80		
 81		if (!defined('POSITIONS')) {
 82			
 83			if (!defined('GANTRY_SLIDER')) {
 84				$gantry->addScript($gantry->gantryUrl.'/admin/widgets/slider/js/slider.js');
 85				define('GANTRY_SLIDER', 1);
 86			}
 87			$gantry->addScript($gantry->gantryUrl.'/admin/widgets/slider/js/unserialize.js');
 88			$gantry->addScript($gantry->gantryUrl.'/admin/widgets/positions/js/positions-utils.js');
 89
 90			$this->settings = array("words" => $this->words, "schemas" => $this->schemas, "maxGrid" => $this->maxGrid);
 91			
 92			if ($this->type == 'custom') $this->customCombinations = $this->getCombinations();
 93			else $this->combinations = $this->getCombinations();
 94			define('POSITIONS', 1);
 95
 96		}
 97		
 98		$posName = ($this->element['name'] == "mainbodyPosition") ? "sidebar" : $this->position_info->id;
 99		$realCount = $gantry->countModules($posName);
100		if ($posName == 'sidebar') $realCount += 1;
101		if ($realCount > 0) {
102			if (!in_array($realCount, $this->schemas)) $realCount = $this->schemas[0];
103			$this->default = $this->oneCharConversion($this->layoutSchemas[$realCount]);
104			$this->defaultCount = $realCount;
105		}
106		
107		// if the same type of combinations are requested, use the cached ones, otherwise get the new set
108		if ($this->type != "custom" && ($this->words != $this->settings["words"] || $this->schemas != $this->settings["schemas"] || $this->maxGrid != $this->settings["maxGrid"])) {
109			$this->combinations = $this->getCombinations();
110		}
111		
112		if ($this->type == "custom") $this->customCombinations = $this->getCombinations();
113
114		if (!in_array((string)$this->defaultCount, $this->schemas)) $this->defaultCount = (int)$this->schemas[0];
115
116		$i = 0;
117		$max_positions =  isset($this->position_info)?$this->position_info->max_positions:false;
118		if (!$max_positions) $max_positions = 6;
119		foreach($this->schemas as $scheme) {
120			$active = "";
121			if ($i >= $max_positions) break;
122			if ((int)$scheme == $this->defaultCount) {
123				$active = ' class="active"';
124				$currentLayout = $scheme;
125			}
126			$lis .= '<li'.$active.'><a href="#"><span>'.$scheme.'</span></a></li>';
127			$i++;
128		}
129		
130		$scriptinit = $this->sliderInit($this->id);	
131		$gantry->addDomReadyScript($scriptinit);
132		$gantry->addDomReadyScript($this->showmax($this->id));
133		
134		$letters = array('a', 'b', 'c', 'd', 'e', 'f');
135		
136		
137		$output = '
138		<div class="wrapper">
139		<div id="'.$this->id.'-grp" class="g-position">
140			<div class="navigation">
141				<span class="title">Positions:</span>
142				<ul class="list">'.$lis.'</ul>
143			</div>
144			<div class="clr"></div>
145			<div id="'.$this->id.'-wrapper" class="col'.$this->maxGrid.' miniatures">
146				<div class="mini-container layout-grid-'.$currentLayout.'">'."\n";
147				
148				for ($i = 0; $i < $max_positions; $i++) {
149					$output .= "<div class=\"mini-grid mini-grid-2\">".$letters[$i]."</div>\n";
150				}
151					
152		$output .= '
153				</div>
154				<div class="clr"></div>
155				<div class="position">
156					<div class="position2"></div>
157					<div class="knob"></div>
158				</div>
159			</div>
160<!--			<div class="current-positions">
161				<span class="title">'.str_replace('%d', "<strong class='".$this->id."-currentPosition countPositions'>".$realCount."</strong>", JText::_('Current Positions')).'</span>
162			</div>-->
163			<input type="hidden" id="'.$this->id.'" name="'.$this->name.'" value=\'';
164			$output .= $this->value;
165			$output .= '\' />
166		</div>
167		</div>
168		';
169		return $output;
170	}
171	
172	
173	function permutations($letters, $num, $filter = 12) {
174		// hardcoded cases for speed optimization
175		$letter0 = base_convert($letters{0}, 24, 10);
176		$letter1 = base_convert($this->lastchar($letters), 24, 10);
177		if ($letter0 + $letter1 > $filter) return array();
178		if ($filter == 12 && $num == 6) return array("222222");
179		if ($num == 1) return $this->oneCharConversion(array($filter));
180		
181		$last = str_repeat($letters{0}, $num);
182		$result = array();
183		
184		while($last != str_repeat($this->lastchar($letters), $num)) {
185			$tmp = 0;
186			for ($i = 0; $i < strlen($last); $i++) $tmp += base_convert($last[$i], 24, 10);
187			if ($tmp == $filter) $result[] = $last;
188
189			$last = $this->char_add($letters, $last ,$num-1);
190		}
191
192		$tmp = 0;
193		for ($i = 0; $i < strlen($last); $i++) $tmp += base_convert($last[$i], 24, 10);
194		if ($tmp == $filter) $result[] = $last;
195				
196		return $result;
197	}
198	
199	function char_add($digits, $string, $char) {
200	    if ($string{$char} <> $this->lastchar($digits)) {
201			$string{$char} = $digits{strpos($digits, $string{$char}) + 1};
202			return $string;
203	    } else {
204			$string = $this->changeall($string, $digits{0}, $char);
205			return $this->char_add($digits, $string, $char - 1);
206	    }
207	} 
208	
209	function lastchar($string) {
210		return $string{strlen($string)-1};
211	} 
212	
213	function changeall($string, $char, $start = 0, $end = 0) {
214	    if ($end == 0) $end = strlen($string) - 1;
215	    for ($i = $start; $i <= $end; $i++) {
216			$string{$i} = $char;
217		}
218		
219		return $string;
220	}
221	
222	function tryCache($implode, $scheme, $words, $grid = 12) {
223		global $gantry;
224		
225		$md5 = md5($grid . implode("", $words) . $scheme);
226
227        $data = $gantry->positions[$grid]->get($md5);
228
229        if (null == $data) {
230			$permutation = $this->permutations($implode, (int) $scheme, $grid);
231			$save = array();
232			$save[$grid][$implode][$scheme] = $permutation;
233
234			//file_put_contents($file, serialize($save));
235            $gantry->positions[$grid]->set($md5, serialize($save));
236			return $permutation;
237		} else {
238			$unserial = unserialize($data);
239			return $unserial[$grid][$implode][$scheme];
240		}
241	}
242	
243	function getCombinations() {
244		global $gantry;
245
246		if ($this->type == 'custom') return $this->getCustomCombinations();
247		
248		$grid = $this->maxGrid;
249		$words = $this->words;
250		$sets = $this->schemas;
251		
252		$result = "{";
253
254		$words = $this->oneCharConversion($words);
255		
256		foreach($sets as $set) {
257			$implode = implode("", $words);
258			$output[$grid][$implode][$set] = $this->tryCache($implode, (int) $set, $words, $grid);
259			$current = $output;
260
261			$tmp = $current[$grid][$implode][$set];			
262			sort($tmp);
263			$result .= "'$set': ['".implode("', '", $tmp)."'],";
264		}
265		$result = substr($result, 0, -1) . "}";
266		return $result;
267	}
268	
269	function getCustomCombinations() {
270		$sets = $this->schemas;
271		$name = str_replace("Position", "Schemas", $this->element['name']);
272		
273		$results = "{";
274		$keysref = "{";
275
276		foreach($this->$name as $key => $set) {
277			$results .= "'$key': [";
278			$keysref .= "'$key': [";
279
280			foreach($set as $combination) {
281				$combination = $this->oneCharConversion($combination);
282
283				$results .= "'" . implode("", $combination) . "', ";
284				$keysref .= "['" . implode("', '", array_keys($combination)) . "'], ";
285			}
286			$results = substr($results, 0, -2) . "],";
287			$keysref = substr($keysref, 0, -2) . "],";
288		}
289		$results = substr($results, 0, -1) . "}";
290		$keysref = substr($keysref, 0, -1) . "}";
291		
292		return array($results, $keysref);
293	}
294	
295	function oneCharConversion($words, $decode = false) {
296		$dummy = array();
297		
298		foreach($words as $key => $word) {
299			if (!$decode) $dummy[$key] = base_convert((int) $word, 10, 24);
300			else $dummy[$key] = base_convert((int) $word, 24, 10);
301		}
302		
303		return $dummy;
304	}
305	
306	function outputCombinations($type = 'combinations') {
307		if (!is_array($this->combinations) && $this->type != 'custom') return $this->combinations;
308		
309		return ($type == 'combinations') ? $this->customCombinations[0] : $this->customCombinations[1];
310	}
311	
312	function getLoadValue() {
313		$defaultValue = array($this->defaultCount => $this->default);
314
315		if ($this->type == 'custom') {
316			$defaultValue = array($this->defaultCount => $this->defaultMainbodySchemas[$this->maxGrid][$this->defaultCount]);
317		}
318
319		if (preg_match("/{/", $this->value)) {
320			$value = unserialize($this->value);
321            if (isset($value[$this->maxGrid]))
322			    $value = $value[$this->maxGrid];
323            else
324                $value = $defaultValue;
325		} else {
326			$value = $defaultValue;
327		}
328		
329		$merge = $value + $this->layoutSchemas;
330
331		$result = "{";
332		
333		$keynames = '';
334
335		if ($this->type == 'custom') {
336			foreach($this->defaultMainbodySchemas[$this->maxGrid] as $key => $defaults) {
337				if (!array_key_exists($key, $value)) {
338					$value[$key] = $defaults;
339				}
340			}
341
342			foreach($value as $key => $array) {
343				$array = $this->oneCharConversion($array);
344				$result .= $key . ': {';
345				$result .= "'values': ['" . implode("", $array) . "'], ";
346				$result .= "'keys': [";
347				foreach($array as $mb => $arr) {
348					$result .= '"'. $mb . '", ';
349				}
350				$result = substr($result, 0, -2);
351				$result .= "]}, ";
352			}
353			
354		} else {
355			foreach($merge as $key => $array) {
356				$array = $this->oneCharConversion($array);
357				$result .= $key . ': [';
358				$result .= "'".implode("", $array)."'";
359				$result .= "], ";	
360			}
361		}
362		
363		$result = substr($result, 0, -2);
364		$result .= "}";
365		
366		return $result;
367	}
368	
369	function sliderInit($name, $max = 12) {
370		$name2 = str_replace("-", "_", $name);
371		$slider = "document.id('".$name."-grp').getElement('.position')";
372		$knob = "document.id('".$name."-grp').getElement('.knob')";
373		$hidden = "document.id('".$this->id."')";
374		$activeNav = array_search((string)$this->defaultCount, $this->schemas);
375		
376		// hidden, name, maxgrid, loadValue, keyName, type, combinations, defaultCount, schemas
377		return "GantryPositions.add('".$this->id."', '".$name."', ".$this->maxGrid.", ".$this->getLoadValue().", '".$this->keyName."', '".$this->type."', ".$this->outputCombinations('combinations').", ".$this->outputCombinations('keys').", ".$activeNav.");";
378	}
379	
380	function showmax($name) {
381		$name2 = str_replace("-", "_", $name);
382		return "GantryPositionsTools.showMax('".$name."', '".$name2."');";
383	}
384}
385
386?>