PageRenderTime 63ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/Opl/Opt/Instruction/Grid.php

https://bitbucket.org/kandsten/hitta.sverok.se
PHP | 141 lines | 101 code | 22 blank | 18 comment | 13 complexity | fa0afc9e587ad73430ae5cfad8c60e85 MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. /*
  3. * OPEN POWER LIBS <http://www.invenzzia.org>
  4. *
  5. * This file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE. It is also available through
  7. * WWW at this URL: <http://www.invenzzia.org/license/new-bsd>
  8. *
  9. * Copyright (c) Invenzzia Group <http://www.invenzzia.org>
  10. * and other contributors. See website for details.
  11. *
  12. * $Id: Grid.php 231 2009-09-19 07:13:14Z zyxist $
  13. */
  14. class Opt_Instruction_Grid extends Opt_Instruction_BaseSection
  15. {
  16. protected $_name = 'grid';
  17. protected $_extraAttributes = array('cols' => array(self::REQUIRED, self::EXPRESSION));
  18. public function configure()
  19. {
  20. $this->_addInstructions(array('opt:grid', 'opt:gridelse', 'opt:item', 'opt:emptyItem'));
  21. } // end configure();
  22. public function processNode(Opt_Xml_Node $node)
  23. {
  24. $name = '_process'.ucfirst($node->getName());
  25. $this->$name($node);
  26. } // end processNode();
  27. public function postprocessNode(Opt_Xml_Node $node)
  28. {
  29. $name = '_postprocess'.ucfirst($node->getName());
  30. $this->$name($node);
  31. } // end postprocessNode();
  32. private function _processGrid(Opt_Xml_Node $node)
  33. {
  34. $section = $this->_sectionCreate($node, array(), array('cols' => array(self::REQUIRED, self::EXPRESSION)));
  35. // Error checking
  36. $itemNode = $node->getElementsExt('opt', 'item');
  37. $emptyItemNode = $node->getElementsExt('opt', 'emptyItem');
  38. if(sizeof($itemNode) != 1)
  39. {
  40. throw new Opt_InstructionTooManyItems_Exception('opt:item', 'opt:grid', 'One');
  41. }
  42. if(sizeof($emptyItemNode) != 1)
  43. {
  44. throw new Opt_InstructionTooManyItems_Exception('opt:emptyItem', 'opt:grid', 'One');
  45. }
  46. // Link those nodes to this section
  47. $itemNode[0]->set('priv:section', $section);
  48. $emptyItemNode[0]->set('priv:section', $section);
  49. // Code generation
  50. $node->addAfter(Opt_Xml_Buffer::TAG_BEFORE, '$_'.$section['name'].'_rows = ceil('.$section['format']->get('section:count').' / '.$section['cols'].'); $_'.$section['name'].'_remain = ('.$section['cols'].
  51. ' - ('.$section['format']->get('section:count').' % '.$section['cols'].')) % '.$section['cols'].'; '.$section['format']->get('section:loopBefore').' '.$section['format']->get('section:reset').' '.
  52. ' for($_'.$section['name'].'_j = 0; $_'.$section['name'].'_j < $_'.$section['name'].'_rows; $_'.$section['name'].'_j++){ ');
  53. $node->addAfter(Opt_Xml_Buffer::TAG_AFTER, ' } ');
  54. $this->_process($node);
  55. } // end _processGrid();
  56. private function _processItem(Opt_Xml_Node $node)
  57. {
  58. if(is_null($node->get('priv:section')))
  59. {
  60. throw new Opt_InstructionInvalidLocation_Exception('opt:item', 'opt:grid');
  61. }
  62. // We're at home. For this particular node we have to activate the section.
  63. $section = $node->get('priv:section');
  64. $node->addAfter(Opt_Xml_Buffer::TAG_BEFORE, ' for($_'.$section['name'].'_k = 0; $_'.$section['name'].'_k < '.$section['cols'].' && '.$section['format']->get('section:valid').'; $_'.$section['name'].'_k++) { '.$section['format']->get('section:populate'));
  65. $node->addBefore(Opt_Xml_Buffer::TAG_AFTER, $section['format']->get('section:next').' } ');
  66. $this->_sectionStart($section);
  67. $node->set('postprocess', true);
  68. if(!is_null($node->get('call:use')))
  69. {
  70. $this->_compiler->setConversion('##simplevar_'.$node->get('call:use'), $section['name']);
  71. $node->set('postprocess', true);
  72. }
  73. $this->_process($node);
  74. } // end _processItem();
  75. private function _processEmptyItem(Opt_Xml_Node $node)
  76. {
  77. if(is_null($node->get('priv:section')))
  78. {
  79. throw new Opt_InstructionInvalidLocation_Exception('opt:item', 'opt:grid');
  80. }
  81. $section = $node->get('priv:section');
  82. $node->addAfter(Opt_Xml_Buffer::TAG_BEFORE, ' if($_'.$section['name'].'_remain > 0 && !'.$section['format']->get('section:valid').') { for($_'.$section['name'].'_k = 0; $_'.$section['name'].'_k < $_'.$section['name'].'_remain; $_'.$section['name'].'_k++) { ');
  83. $node->addBefore(Opt_Xml_Buffer::TAG_AFTER, ' } } ');
  84. $this->_process($node);
  85. } // end _processItem();
  86. private function _postprocessGrid(Opt_Xml_Element $node)
  87. {
  88. $section = $node->get('priv:section');
  89. if($node->hasAttributes())
  90. {
  91. if(!$node->get('priv:alternative'))
  92. {
  93. $this->_sortSectionContents($node, 'opt', 'gridelse');
  94. }
  95. }
  96. } // end _postprocessGrid();
  97. private function _postprocessItem(Opt_Xml_Element $node)
  98. {
  99. if(!is_null($node->get('call:use')))
  100. {
  101. $section = $node->get('priv:section');
  102. $this->_compiler->unsetConversion('##simplevar_'.$section['name']);
  103. }
  104. // Deactivating the section.
  105. $this->_sectionEnd($node);
  106. } // end _postprocessItem();
  107. private function _processGridelse(Opt_Xml_Element $node)
  108. {
  109. $parent = $node->getParent();
  110. if($parent instanceof Opt_Xml_Element && $parent->getXmlName() == 'opt:grid')
  111. {
  112. $parent->set('priv:alternative', true);
  113. $section = $parent->get('priv:section');
  114. $node->addBefore(Opt_Xml_Buffer::TAG_BEFORE, ' } else { ');
  115. // $this->_deactivateSection($parent->get('sectionName'));
  116. $this->_process($node);
  117. }
  118. } // end _processGridelse();
  119. } // end Opt_Instruction_Grid;