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

/admin/templates/parts/tables/admin_map/part.php

https://github.com/elkuku/EasyCreator
PHP | 326 lines | 213 code | 61 blank | 52 comment | 16 complexity | 971474cd00f3ab248fb1ae891dbc8939 MD5 | raw file
  1. <?php
  2. /**
  3. * @package EasyCreator
  4. * @subpackage Parts
  5. * @author Nikolai Plath (elkuku)
  6. * @author Created on 20-Apr-2009
  7. */
  8. //-- No direct access
  9. defined('_JEXEC') || die('=;)');
  10. /**
  11. * Enter description here ...@todo class doccomment.
  12. *
  13. */
  14. class PartTablesAdmin_map
  15. {
  16. public $group = 'tables';
  17. private $logging = false;
  18. private $logger = null;
  19. /**
  20. * Info about the thing.
  21. *
  22. * @return EcrProjectTemplateInfo
  23. */
  24. public function info()
  25. {
  26. $info = new EcrProjectTemplateInfo;
  27. $info->group = $this->group;
  28. $info->title = 'Map and Admin';
  29. $info->description = jgettext('This will map an existing table and create an admin interface and menu link');
  30. return $info;
  31. }
  32. /**
  33. * Get insert options.
  34. *
  35. * @return void
  36. */
  37. public function getOptions()
  38. {
  39. $db = JFactory::getDBO();
  40. $tables = $db->getTableList();
  41. $ajaxLink = 'index.php?option=com_easycreator&tmpl=component&controller=ajax&task=part_task';
  42. $ajaxLink .= '&group=tables&part=admin_map';
  43. $ajaxLink .= '&part_task=show_tablefields';
  44. $ajax = "new Request({ url: '".$ajaxLink
  45. ."'+'&table_name='+this.value, update:'addPartTableFields', onComplete: function() '
  46. .'{ div_new_element.show(); } }).send();";
  47. echo '<strong id="table_name_label">'.jgettext('Table').' :</strong>';
  48. echo '<select name="table_name" id="table_name" onchange="'.$ajax.'">';
  49. echo '<option value="">'.jgettext('Choose').'...</option>';
  50. $prefix = $db->getPrefix();
  51. foreach($tables as $table)
  52. {
  53. $v = str_replace($prefix, '', $table);
  54. echo '<option value="'.$v.'">'.$v.'</option>';
  55. }
  56. echo '</select>';
  57. echo '<div id="addPartTableFields"></div>';
  58. EcrHtmlSelect::name();
  59. echo '<strong>'.jgettext('Menu link').' :</strong><br />';
  60. echo '<input type="checkbox" name="create_menu_link" checked="checked"'
  61. .' id="create_menu_link" value="create_menu_link">';
  62. echo '<label for="create_menu_link">'.jgettext('Create a menu link').'</label><br />';
  63. echo '<hr />';
  64. EcrHtmlOptions::logging();
  65. $requireds = array('element_name', 'table_name');
  66. EcrHtmlButton::submitParts($requireds);
  67. }
  68. /**
  69. * Shows the fields of a given table.
  70. *
  71. * AJAX called
  72. *
  73. * @return void
  74. */
  75. public function show_tablefields()
  76. {
  77. $table_name = JFactory::getApplication()->input->get('table_name');
  78. if( ! $table_name)
  79. {
  80. return;
  81. }
  82. $db = JFactory::getDBO();
  83. $table_name = $db->getPrefix().$table_name;
  84. $fields = $db->getTableColumns($table_name);
  85. if( ! count($fields) || ! count($fields[$table_name]))
  86. {
  87. JFactory::getApplication()->enqueueMessage(jgettext('No table fields found'), 'error');
  88. }
  89. ?>
  90. <table>
  91. <tr>
  92. <th colspan="2"><?php echo jgettext('Include')?></th>
  93. <th><?php echo jgettext('Editable'); ?></th>
  94. <th><?php echo jgettext('Type'); ?></th>
  95. </tr>
  96. <?php
  97. foreach($fields[$table_name] as $key => $value)
  98. {
  99. switch($value)
  100. {
  101. case 'int':
  102. case 'tinyint':
  103. $def = '0';
  104. break;
  105. default:
  106. $def = 'NULL';
  107. break;
  108. }
  109. $javascript1 = "$('tblfield_edit_".$key."').disabled=(this.checked)?false:true;";
  110. $javascript2 = "$('tblfield_type_".$key."').disabled=(this.checked && $('tblfield_edit_"
  111. .$key."').checked)?false:true;";
  112. $javascript = $javascript1.$javascript2;
  113. ?>
  114. <tr>
  115. <td><input type="checkbox" onchange="<?php echo $javascript; ?>"
  116. name="table_fields[]" checked="checked"
  117. id="tblfield_<?php echo $key; ?>"
  118. value="<?php echo $key; ?>">
  119. </td>
  120. <td><label for="tblfield_<?php echo $key; ?>">
  121. <?php echo $key.'<br />('.$value.')'; ?>
  122. </label></td>
  123. <td><input type="checkbox" onchange="<?php echo $javascript2; ?>"
  124. name="table_fields_edits[]" checked="checked"
  125. id="tblfield_edit_<?php echo $key; ?>"
  126. value="<?php echo $key; ?>"></td>
  127. <td><select name="table_fields_types[<?php echo $key; ?>]"
  128. id="tblfield_type_<?php echo $key; ?>">
  129. <option>text</option>
  130. <option>text area</option>
  131. <option>radio bool</option>
  132. <option>html</option>
  133. </select></td>
  134. </tr>
  135. <?php
  136. }
  137. ?>
  138. </table>
  139. <?php
  140. }
  141. /**
  142. * Inserts the part into the project.
  143. *
  144. * @param EcrProjectBase $project The project.
  145. * @param array $options Insert options.
  146. * @param EcrLogger $logger The EcrLogger.
  147. *
  148. * @return boolean
  149. */
  150. public function insert(EcrProjectBase $project, $options, EcrLogger $logger)
  151. {
  152. $input = JFactory::getApplication()->input;
  153. $element_name = $input->get('element_name');
  154. $table_name = $input->get('table_name');
  155. $req_table_fields = $input->get('table_fields', array(), 'array');
  156. $req_table_fields_edits = $input->get('table_fields_edits', array(), 'array');
  157. $req_table_fields_types = $input->get('table_fields_types', array(), 'array');
  158. if( ! $table_name)
  159. {
  160. JFactory::getApplication()->enqueueMessage(jgettext('No table given'), 'error');
  161. return false;
  162. }
  163. $db = JFactory::getDBO();
  164. $prefix = $db->getPrefix();
  165. $fields = $db->getTableFields($prefix.$table_name);
  166. $tableFields = array();
  167. $table_vars = '';
  168. if(count($fields))
  169. {
  170. $tableFields = $fields[$prefix.$table_name];
  171. foreach($tableFields as $key => $value)
  172. {
  173. if( ! in_array($key, $req_table_fields))
  174. continue;
  175. $table_vars .= EcrTableHelper::formatTableVar($key, $value);
  176. }
  177. }
  178. /*
  179. * Add substitutes
  180. */
  181. $project->addSubstitute('ECR_ELEMENT_NAME', $element_name);
  182. $project->addSubstitute('_ECR_LOWER_ELEMENT_NAME_', strtolower($element_name));
  183. $project->addSubstitute('_ECR_TABLE_NAME_', $table_name);
  184. $project->addSubstitute('##ECR_TABLE_VARS##', $table_vars);
  185. /*
  186. * Read part options files
  187. */
  188. $files = JFolder::files($options->pathSource.DS.'options', '.', true, true);
  189. foreach($files as $file)
  190. {
  191. $fileContents = JFile::read($file);
  192. if(strpos($fileContents, '<?php') === 0)
  193. {
  194. $fileContents = substr($fileContents, 6);
  195. }
  196. $project->substitute($fileContents);
  197. $project->addSubstitute('##'.strtoupper(JFile::stripExt(JFile::getName($file))).'##', $fileContents);
  198. }
  199. /*
  200. * Add manual substitutes
  201. */
  202. $substitutes['##ECR_VIEW1_TMPL1_THS##'] = '?>';
  203. $substitutes['##ECR_VIEW1_TMPL1_TDS##'] = '?>';
  204. $substitutes['##ECR_VIEW2_TMPL1_OPTION2##'] = '?>';
  205. $i = 0;
  206. foreach($tableFields as $key => $value)
  207. {
  208. if( ! in_array($key, $req_table_fields))
  209. continue;
  210. $substitutes['##ECR_VIEW1_TMPL1_THS##'] .= ' <th>'.NL;
  211. $substitutes['##ECR_VIEW1_TMPL1_THS##'] .= " <?php echo JHTML::_('grid.sort', '"
  212. .$key."', '".$key."', \$this->lists['order_Dir'], \$this->lists['order']);?>".NL;
  213. $substitutes['##ECR_VIEW1_TMPL1_THS##'] .= ' </th>'.NL;
  214. $substitutes['##ECR_VIEW1_TMPL1_TDS##'] .= ' <td>'.NL;
  215. $substitutes['##ECR_VIEW1_TMPL1_TDS##'] .= ' <?php echo $row->'.$key.'; ?>'.NL;
  216. $substitutes['##ECR_VIEW1_TMPL1_TDS##'] .= ' </td>'.NL;
  217. if(in_array($key, $req_table_fields_edits))
  218. {
  219. $s = $project->getSubstitute('##ECR_VIEW2_TMPL1_OPTION2VAL##');
  220. $s = str_replace('_ECR_FIELDVALUE_1_', $key, $s);
  221. $s = str_replace('_ECR_FIELDVALUE_2_', $req_table_fields_types[$key], $s);
  222. $substitutes['##ECR_VIEW2_TMPL1_OPTION2##'] .= $s;
  223. }
  224. $i ++;
  225. }
  226. $substitutes['##ECR_VIEW1_TMPL1_THS##'] .= ' <?php $coloumnCount += '.$i.'; ?>'.NL;
  227. $substitutes['##ECR_VIEW1_TMPL1_THS##'] .= ' <?php '.NL;
  228. $substitutes['##ECR_VIEW1_TMPL1_TDS##'] .= ' <?php '.NL;
  229. $substitutes['##ECR_VIEW2_TMPL1_OPTION2##'] .= ' <?php '.NL;
  230. foreach($substitutes as $key => $value)
  231. {
  232. $project->addSubstitute($key, $value);
  233. }
  234. /*
  235. * Remove options
  236. */
  237. if( ! in_array('ordering', $req_table_fields))
  238. {
  239. $project->addSubstitute('##ECR_CONTROLLER1_OPTION1##', '');
  240. }
  241. if( ! in_array('published', $req_table_fields))
  242. {
  243. }
  244. $project->addSubstitute('ECR_SUBPACKAGE', 'Tables');
  245. $input->set('element_scope', 'admin');
  246. if( ! $project->insertPart($options, $logger))
  247. {
  248. return false;
  249. }
  250. /*
  251. * Create menu link
  252. */
  253. if($input->get('create_menu_link', false))
  254. {
  255. $link = 'option='.$options->ecr_project.'&view='.strtolower($element_name).$project->listPostfix
  256. .'&controller='.strtolower($element_name).$project->listPostfix;
  257. if( ! $project->addSubmenuEntry($element_name, $link))
  258. {
  259. JFactory::getApplication()->enqueueMessage(jgettext('Unable to create menu link'), 'error');
  260. return false;
  261. }
  262. }
  263. return true;
  264. }
  265. }