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

/View/Helper/AdvindexHelper.php

http://github.com/morrislaptop/advindex
PHP | 307 lines | 229 code | 32 blank | 46 comment | 48 complexity | 5dc5382d8cb5cfb4afc7091ea9f4b4e9 MD5 | raw file
  1. <?php
  2. class AdvindexHelper extends FormHelper {
  3. var $helpers = array('Html', 'Paginator', 'Form', 'Session', 'Text', 'Time');
  4. /**
  5. * @var AdvformHelper
  6. */
  7. var $AdvformHelper;
  8. var $pluginFolder;
  9. function create($model = null, $options = array()) {
  10. // we have to set the url manually as the id in the form data causes Router errors.
  11. return parent::create($model, array('url' => array('action' => 'index')));
  12. }
  13. function search() {
  14. return parent::submit('Search', array('class' => 'submit tiny'));
  15. }
  16. function filter($fieldName, $options = array())
  17. {
  18. $this->setEntity($fieldName);
  19. $options = array_merge(array(
  20. 'fromTo' => true
  21. ),$options);
  22. $modelKey = $this->model();
  23. $fieldKey = $this->field();
  24. $fieldDef = $this->_introspectModel($modelKey, 'fields', $fieldKey);
  25. $columnType = $fieldDef['type'];
  26. // override column type if in the options
  27. if ( !empty($options['type']) ) {
  28. $columnType = $options['type'];
  29. unset($options['type']);
  30. }
  31. else if ( 'datetime' == $columnType ) {
  32. // pretty safe to assume we want to turn date times into dates
  33. $columnType = 'date';
  34. }
  35. else if ( '_id' === substr($fieldName, -strlen('_id')) ) {
  36. $columnType = 'foreign';
  37. }
  38. // dont escape by defualt.
  39. if ( !isset($options['escape']) ) {
  40. $options['escape'] = false;
  41. }
  42. // qualify model.
  43. if ( strpos($fieldName, '.') === false ) {
  44. $fieldName = $modelKey . '.' . $fieldName;
  45. }
  46. // text types just get a textbox.
  47. switch ($columnType)
  48. {
  49. case 'boolean':
  50. $selOptions = array(
  51. 'False',
  52. 'True'
  53. );
  54. $options = array_merge(array('type' => 'select', 'label' => false, 'div' => false, 'empty' => true, 'options' => $selOptions), $options);
  55. return $this->Form->input($fieldName, $options);
  56. break;
  57. case 'integer':
  58. case 'float':
  59. if ( $options['fromTo'] ) {
  60. $from = $this->Form->input($fieldName . '.from', array_merge(array('type' => 'text', 'class' => 'range'), $options));
  61. $to = $this->Form->input($fieldName . '.to', array_merge(array('type' => 'text', 'class' => 'range'), $options));
  62. return $from . $to;
  63. } else {
  64. return $this->Form->input($fieldName . '.from', array_merge(array('label' => false, 'type' => 'text', 'class' => 'range'), $options));
  65. }
  66. break;
  67. case 'date':
  68. case 'datetime':
  69. case 'timestamp':
  70. if ( $options['fromTo'] ) {
  71. $from = $this->Form->input($fieldName . '.from', array('label' => 'From', 'class' => 'text date_picker', 'type' => 'text'));
  72. $to = $this->Form->input($fieldName . '.to', array('label' => 'To', 'class' => 'text date_picker', 'type' => 'text'));
  73. return $from . $to;
  74. } else {
  75. return $this->Form->input($fieldName . '.from', array('label' => false, 'class' => 'text date_picker', 'type' => 'text'));
  76. }
  77. break;
  78. case 'time':
  79. if ( $options['fromTo'] ) {
  80. $options = array_merge(array('empty' => true, 'type' => 'time'), $options);
  81. $from = $this->Form->input($fieldName . '.from', $options);
  82. $to = $this->Form->input($fieldName . '.to', $options);
  83. return $from . $to;
  84. } else {
  85. $options = array_merge(array('label' => false, 'empty' => true, 'type' => 'time'), $options);
  86. return $this->Form->input($fieldName . '.from', $options);
  87. }
  88. break;
  89. case 'text':
  90. case 'string':
  91. case 'foreign':
  92. default:
  93. $options = array_merge(array('label' => false, 'empty' => true), $options);
  94. return $this->Form->input($fieldName, $options);
  95. break;
  96. }
  97. return $this->Form->input($fieldName, $options);
  98. }
  99. function export($label) {
  100. $url = array(
  101. 'action' => 'export',
  102. );
  103. if ( !empty($this->params['named']['sort']) ) {
  104. $url['sort'] = $this->params['named']['sort'];
  105. $url['direction'] = $this->params['named']['direction'];
  106. }
  107. return $this->Html->link($label, $url);
  108. }
  109. function perPage() {
  110. $opts = array(10, 20, 50, 100, 'All');
  111. $opts = array_combine($opts, $opts);
  112. $paging = $this->params['paging'];
  113. $paging = array_pop($paging);
  114. $limit = $paging['options']['limit'];
  115. if ( $limit == PHP_INT_MAX ) {
  116. $limit = 'All';
  117. }
  118. return $this->Form->select('perPage', $opts, $limit, array('onchange' => "this.form.submit();"), false);
  119. }
  120. /**
  121. * Returns the columns for the current model.
  122. */
  123. function cols() {
  124. $model = $this->model();
  125. $var = Inflector::pluralize(Inflector::variable($model));
  126. $view = ClassRegistry::getObject('view');
  127. $rows = $view->viewVars[$var];
  128. if ( $rows ) {
  129. $cols = array_keys($rows[0][$model]);
  130. }
  131. else {
  132. // what do we do, no keys!
  133. $model = ClassRegistry::getObject($model);
  134. $cols = array_keys($model->schema());
  135. }
  136. return $cols;
  137. }
  138. /**
  139. * AdminWorks specific functions
  140. */
  141. /**
  142. * Simple template parser for strings
  143. *
  144. * Enables you to use {field} in a string to inject the `fields`
  145. * data in to the string to create dynamic strings
  146. *
  147. * @param mixed $output
  148. * @param mixed $data
  149. */
  150. function parseTemplate($output,$data){
  151. foreach($data as $k => $v){
  152. $output = str_replace('{'.$k.'}',$v,$output);
  153. }
  154. return $output;
  155. }
  156. /**
  157. * Set template variables
  158. *
  159. * @param array $scaffold
  160. * @param array $structure
  161. * @param array $scaffoldFields
  162. * @param string $modelClass
  163. */
  164. function setTemplateVariables(&$scaffold, &$structure, &$scaffoldFields, $modelClass){
  165. $scaffold = Configure::read('scaffold');
  166. if(!empty($scaffold[$modelClass][$this->params['action']])){
  167. $structure = $scaffold[$modelClass][$this->params['action']];
  168. $errors = array();
  169. foreach($structure as $k => $v){
  170. if(substr($k,0,strlen('_special_')) == '_special_'){ // Special field
  171. }elseif(is_integer($k) || in_array($k,$scaffoldFields)){ // Normal field OR Field with options
  172. if(!in_array($v,$scaffoldFields) && !in_array($k,$scaffoldFields) && $v != 'actions'){ // Doesn't exist
  173. unset($structure[$k]);
  174. $errors[] = 'Field "'.$v.'" doesn\'t exist so it was removed';
  175. }
  176. }else{ // Group of fields
  177. foreach($structure[$k] as $gk => $gv){
  178. if(is_integer($gk) || in_array($gk,$scaffoldFields)){ // Normal field OR Field with options
  179. if(!in_array($gv,$scaffoldFields) && !in_array($gk,$scaffoldFields)){ // Doesn't exist
  180. unset($structure[$k][$gk]);
  181. debug('Field "'.$k.'->'.$gv.'" doesn\'t exist so it was removed');
  182. }
  183. }else{ // Doesn't exist
  184. unset($structure[$k][$gk]);
  185. $errors[] = 'Field "'.$k.'->'.$gk.'" doesn\'t exist so it was removed';
  186. }
  187. }
  188. }
  189. }
  190. if(count($errors) > 0){
  191. debug(implode('<br/>',$errors).'<br/><br/>These are the fields available for $structure[\''.$modelClass.'\']:<br/>'.print_r($scaffoldFields,true));
  192. }
  193. if(!count($structure) > 0){
  194. $structure = $scaffoldFields;
  195. $structure[] = 'actions'; // Add actions in so they show up
  196. }
  197. }else{
  198. $structure = $scaffoldFields;
  199. $structure[] = 'actions'; // Add actions in so they show up
  200. }
  201. }
  202. /**
  203. * Return output for templates
  204. *
  205. * @param array $array
  206. * @param array $data
  207. * @param string $field
  208. */
  209. function getOutput(&$array, &$data, &$field){
  210. if(is_array($array) && isset($array['call_user_func'])){
  211. return $this->parseTemplate(call_user_func($array['call_user_func'],$data[$field]),$data);
  212. }elseif(is_array($array) && !empty($array['switch'])){
  213. if(isset($array['switch'][$data[$field]])){
  214. return $this->parseTemplate($array['switch'][$data[$field]],$data);
  215. }else{
  216. return $this->parseTemplate($data[$field],$data);
  217. }
  218. }elseif(isset($array['format'])){
  219. switch($array['format']){
  220. case 'dateTime':
  221. return $this->Time->niceShort($data[$field]);
  222. break;
  223. case 'json':
  224. return '<pre>'.print_r(json_decode($data[$field]),true).'</pre>';
  225. break;
  226. }
  227. }elseif(isset($array['yesno']) && $array['yesno'] === true){
  228. return ($data[$field] == 0?'No':($data[$field] == 1?'Yes':$data[$field]));
  229. }elseif(!empty($array['html']) && is_array($array['html'])){
  230. $output = '';
  231. foreach($array['html'] as $part){
  232. if(!empty($data[$part])){
  233. $output .= $data[$part];
  234. }else{
  235. $output .= $part;
  236. }
  237. }
  238. return $this->parseTemplate($output,$data);
  239. }
  240. if($array != $field){
  241. return $this->parseTemplate($array,$data);
  242. }
  243. return $this->parseTemplate($this->Text->autoLink($data[$field]),$data);
  244. }
  245. /**
  246. * Return output for special fields
  247. *
  248. * @param array $array
  249. * @param array $data
  250. */
  251. function getOutputSpecial(&$array, &$data){
  252. if(!empty($array['image'])){
  253. $path = '';
  254. foreach($array['image'] as $part){
  255. if(!empty($data[$part])){
  256. $path .= $data[$part];
  257. }else{
  258. $path .= $part;
  259. }
  260. }
  261. return $this->parseTemplate('<img src="'.$path.'" />',$data);
  262. }elseif(!empty($array['html'])){
  263. $output = '';
  264. foreach($array['html'] as $part){
  265. if(!empty($data[$part])){
  266. $output .= $data[$part];
  267. }else{
  268. $output .= $part;
  269. }
  270. }
  271. return $this->parseTemplate($output,$data);
  272. }
  273. }
  274. }
  275. ?>