Ejemplo n.º 1
0
 /**
  * Edit a table
  */
 function edit()
 {
     $app =& JFactory::getApplication();
     $session =& JFactory::getSession();
     $session->clear('com_fabrik.admin.table.edit.model');
     require_once JPATH_COMPONENT . DS . 'views' . DS . 'table.php';
     $user =& JFactory::getUser();
     $db =& JFactory::getDBO();
     $row =& JTable::getInstance('table', 'Table');
     $acl =& JFactory::getACL();
     $config =& JFactory::getConfig();
     if ($this->_task == 'edit') {
         $cid = JRequest::getVar('cid', array(0), 'method', 'array');
         $cid = array((int) $cid[0]);
     } else {
         $cid = array(0);
     }
     $connectionTables = array();
     //  this only appears if you are automatically creating the table from an existing form - which has no table associated with it
     $fabrikid = JRequest::getVar('fabrikid', '', 'get');
     $row->load($cid[0]);
     $params = new fabrikParams($row->attribs, JPATH_COMPONENT . DS . 'model' . DS . 'table.xml');
     $lists['tablejoin'] = "";
     //$lists['defaultJoinTables'] = '[]';
     $lists['defaultJoinTables'] = array();
     $lists['linkedtables'] = array();
     $connModel =& JModel::getInstance('Connection', 'FabrikModel');
     $model =& JModel::getInstance('Table', 'FabrikModel');
     $model->setId($cid[0]);
     $model->_table =& $row;
     $formModel =& $model->getForm();
     $aJoinObjs = array();
     if ($this->_task != 'edit') {
         $row->template = 'default';
         $realCnns = $connModel->getConnections();
         $defaultCid = '';
         foreach ($realCnns as $realCnn) {
             if ($realCnn->default == 1) {
                 $defaultCid = $realCnn->id;
             }
         }
         $javascript = "onchange=\"changeDynaList('db_table_name', connectiontables, document.adminForm.connection_id.options[document.adminForm.connection_id.selectedIndex].value, 0, 0);\"";
         $lists['connections'] = $connModel->getConnectionsDd($realCnns, $javascript, 'connection_id', $defaultCid);
         $connectionTables = $connModel->getConnectionTables($realCnns);
         $javascript = '';
         $lists['tablename'] = '<select name="db_table_name" id="tablename" class="inputbox" size="1" >';
         if ($defaultCid == '') {
             $lists['tablename'] .= '<option value="" selected="selected">' . JText::_('CHOOSE A CONNECTION FIRST') . '</option>';
         } else {
             foreach ($connectionTables[$defaultCid] as $t) {
                 $lists['tablename'] .= '<option value="' . $t->value . '">' . $t->text . '</option>';
             }
         }
         $lists['tablename'] .= '</select>';
         $lists['order_by'][] = JText::_('AVAILABLE AFTER TABLE SAVED');
         $lists['group_by'] = JText::_('AVAILABLE AFTER TABLE SAVED');
         $lists['filter-fields'] = JText::_('AVAILABLE AFTER TABLE SAVED');
         $lists['db_primary_key'] = JText::_('AVAILABLE AFTER TABLE SAVED');
     } else {
         //if record already exists then you can't change the form or table it points to
         // fail if checked out not by 'me'
         if ($row->checked_out && $row->checked_out != $user->get('id')) {
             $app->redirect('index.php?option=com_fabrik', 'The connection ' . $row->description . ' is currently being edited by another administrator');
         }
         $row->checkout($user->get('id'));
         if ($row->connection_id != "-1") {
             $sql = "SELECT description FROM #__fabrik_connections WHERE id = " . (int) $row->connection_id;
             $db->setQuery($sql);
             $lists['connections'] = $db->loadResult();
             $lists['tablename'] = "<input type='hidden' name='db_table_name' value='{$row->db_table_name}' />{$row->db_table_name}";
         } else {
             $lists['connections'] = "no database";
             $lists['tablename'] = "no table";
         }
         $lists['connections'] .= "<input type=\"hidden\" value=\"{$row->connection_id}\" id=\"connection_id\" name=\"connection_id\" />";
         $formModel->setId($row->form_id);
         $formTable =& $formModel->getForm();
         $formModel->getGroupsHiarachy();
         //table join info
         $sql = "SELECT * FROM #__fabrik_joins WHERE table_id = " . (int) $row->id . " AND element_id = 0";
         $db->setQuery($sql);
         $aJoinObjs = $db->loadObjectList();
         $lists['joins'] =& $aJoinObjs;
         $lists['order_by'] = array();
         $orderbys = explode(GROUPSPLITTER2, $row->order_by);
         foreach ($orderbys as $orderby) {
             $lists['order_by'][] = $formModel->getElementList('order_by[]', $orderby, true, false, true);
         }
         $lists['group_by'] = $formModel->getElementList('group_by', $row->group_by, true, false, true);
         //needs to be table.element format for where statement to work
         $formModel->_addDbQuote = true;
         $lists['filter-fields'] = $formModel->getElementList('params[filter-fields][]', '', false, false, true);
         $lists['db_primary_key'] = $formModel->getElementList('db_primary_key', $row->db_primary_key);
         $formModel->_addDbQuote = false;
         //but you can now add table joins
         $connModel->setId($row->connection_id);
         $connModel->getConnection($row->connection_id);
         ///load in current connection
         $joinFromTables[] = JHTML::_('select.option', '', '-');
         $joinFromTables[] = JHTML::_('select.option', $row->db_table_name, $row->db_table_name);
         $lists['defaultjoin'] = $connModel->getTableDdForThisConnection('', 'table_join[]', '', 'inputbox table_key');
         $lists['tablejoin'] = $connModel->getTableDdForThisConnection('', 'table_join[]', '', 'inputbox table_join_key');
         //make a drop down validation type for each validation
         $aActiveJoinTypes = array();
         if (is_array($aJoinObjs)) {
             for ($ff = 0; $ff < count($aJoinObjs); $ff++) {
                 $oJoin = $aJoinObjs[$ff];
                 $fields = array();
                 $aFields = $model->getDBFields($oJoin->join_from_table);
                 foreach ($aFields as $o) {
                     if (is_array($o)) {
                         foreach ($o as $f) {
                             $fields[] = $f->Field;
                         }
                     } else {
                         $fields[] = $o->Field;
                     }
                 }
                 $aJoinObjs[$ff]->joinFormFields = $fields;
                 $aFields = $model->getDBFields($oJoin->table_join);
                 $fields = array();
                 foreach ($aFields as $o) {
                     if (is_array($o)) {
                         foreach ($o as $f) {
                             $fields[] = $f->Field;
                         }
                     } else {
                         $fields[] = $o->Field;
                     }
                 }
                 $aJoinObjs[$ff]->joinToFields = $fields;
             }
             $lists['defaultJoinTables'] = $connModel->getThisTables(true);
         }
     }
     if ($row->id != '') {
         //only existing tables can have a menu linked to them
         $and = "\n AND link LIKE '%index.php?option=com_fabrik%' AND link LIKE '%view=table%'";
         $and .= " AND params LIKE '%tableid=" . $row->id . "'";
         $menus = FabrikHelperMenu::Links2Menu('component', $and);
     } else {
         $menus = null;
     }
     $lists['filter-access'] = $this->getFilterACLList($row);
     $lists['menuselect'] = FabrikHelperMenu::MenuSelect();
     $lists['tableTemplates'] = FabrikHelperAdminHTML::templateList('table', 'template', $row->template);
     // make the filter action drop down
     $filterActions[] = JHTML::_('select.option', 'onchange', JText::_('ON CHANGE'));
     $filterActions[] = JHTML::_('select.option', 'submitform', JText::_('SUBMIT FORM'));
     $lists['filter_action'] = JHTML::_('select.genericlist', $filterActions, 'filter_action', 'class="inputbox" size="1" ', 'value', 'text', $row->filter_action);
     //make the order direction drop down
     $orderDir[] = JHTML::_('select.option', 'ASC', JText::_('ASCENDING'));
     $orderDir[] = JHTML::_('select.option', 'DESC', JText::_('DESCENDING'));
     $orderdirs = explode(GROUPSPLITTER2, $row->order_dir);
     $lists['order_dir'] = array();
     foreach ($orderdirs as $orderdir) {
         $lists['order_dir'][] = JHTML::_('select.genericlist', $orderDir, 'order_dir[]', 'class="inputbox" size="1" ', 'value', 'text', $orderdir);
     }
     $linkedTables = $model->getJoinsToThisKey();
     $aExisitngLinkedTables = $params->get('linkedtable', '', '_default', 'array');
     $aExisitngLinkedForms = $params->get('linkedform', '', '_default', 'array');
     $aExistingTableHeaders = $params->get('linkedtableheader', '', '_default', 'array');
     $aExistingFormHeaders = $params->get('linkedformheader', '', '_default', 'array');
     $linkedform_linktype = $params->get('linkedform_linktype', '', '_default', 'array');
     $linkedtable_linktype = $params->get('linkedtable_linktype', '', '_default', 'array');
     $tableLinkTexts = $params->get('linkedtabletext', '', '_default', 'array');
     $formLinkTexts = $params->get('linkedformtext', '', '_default', 'array');
     $lists['linkedtables'] = array();
     $f = 0;
     $used = array();
     foreach ($linkedTables as $linkedTable) {
         $key = $linkedTable->table_id . '-' . $linkedTable->form_id . '-' . $linkedTable->element_id;
         if (!array_key_exists($f, $aExisitngLinkedTables)) {
             $aExisitngLinkedTables[$f] = '0';
         }
         if (!array_key_exists($f, $linkedtable_linktype)) {
             $linkedtable_linktype[$f] = '0';
         }
         //fiddle ordering
         $index = array_search($key, $aExisitngLinkedTables);
         $index = $index === false ? $f : $index;
         if (!array_search($key, $aExisitngLinkedTables)) {
             for ($fcounter = 0; $fcounter <= count($linkedTables); $fcounter++) {
                 if (!in_array($fcounter, $used)) {
                     $index = $fcounter;
                     break;
                 }
             }
         }
         $used[] = $index;
         $yeschecked = in_array($linkedTable->db_table_name, $aExisitngLinkedTables) || JArrayHelper::getValue($aExisitngLinkedTables, $index, 0) != '0' ? 'checked="checked"' : ($checked = '');
         $nochecked = $yeschecked == '' ? 'checked="checked"' : ($checked = '');
         $el = '<label><input name="params[linkedtable][' . $key . ']" value="0" ' . $nochecked . ' type="radio">' . JText::_('NO') . '</label>';
         $el .= '<label><input name="params[linkedtable][' . $key . ']" value="' . $key . '" ' . $yeschecked . ' type="radio">' . JText::_('YES') . '</label>';
         $yeschecked = in_array($linkedTable->db_table_name, $linkedtable_linktype) || JArrayHelper::getValue($linkedtable_linktype, $index, 0) != '0' ? 'checked="checked"' : ($checked = '');
         $nochecked = $yeschecked == '' ? 'checked="checked"' : ($checked = '');
         $linkType1 = '<label><input name="params[linkedtable_linktype][' . $key . ']" value="0" ' . $nochecked . ' type="radio">' . JText::_('NO') . '</label>';
         $linkType1 .= '<label><input name="params[linkedtable_linktype][' . $key . ']" value="' . $key . '" ' . $yeschecked . ' type="radio">' . JText::_('YES') . '</label>';
         $tableHeader = '<input name="params[linkedtableheader][' . $key . ']" value="' . @$aExistingTableHeaders[$index] . '" size="16" >';
         $label = str_replace(array("\n", "\r", "<br>", "</br>"), '', $linkedTable->tablelabel);
         $hover = JText::_('ELEMENT') . ': ' . $linkedTable->element_label . " [{$linkedTable->plugin}]. {tmpl key =" . $linkedTable->element_id . "_table_heading}";
         $tableLinkText = '<input name="params[linkedtabletext][' . $key . ']" value="' . @$tableLinkTexts[$index] . '" size="16" >';
         $linkedArray = array($label, $hover, $el, $tableHeader, $linkType1, $tableLinkText);
         $lists['linkedtables'][$index] = $linkedArray;
     }
     ksort($lists['linkedtables']);
     $lists['linkedforms'] = array();
     $f = 0;
     $used = array();
     /***/
     foreach ($linkedTables as $linkedTable) {
         $key = $linkedTable->table_id . '-' . $linkedTable->form_id . '-' . $linkedTable->element_id;
         if (!array_key_exists($f, $aExisitngLinkedForms)) {
             $aExisitngLinkedForms[$f] = '0';
         }
         if (!array_key_exists($f, $linkedform_linktype)) {
             $linkedform_linktype[$f] = '0';
         }
         //fiddle ordering
         $index = array_search($key, $aExisitngLinkedForms);
         $index = $index === false ? $f : $index;
         if (!array_search($key, $aExisitngLinkedForms)) {
             for ($fcounter = 0; $fcounter <= count($linkedTables); $fcounter++) {
                 if (!in_array($fcounter, $used)) {
                     $index = $fcounter;
                     break;
                 }
             }
         }
         $used[] = $index;
         $yeschecked = in_array($linkedTable->db_table_name, $aExisitngLinkedForms) || JArrayHelper::getValue($aExisitngLinkedForms, $index, 0) != '0' ? 'checked="checked"' : ($checked = '');
         $nochecked = $yeschecked == '' ? 'checked="checked"' : ($checked = '');
         $el2 = '<label><input name="params[linkedform][' . $key . ']" value="0" ' . $nochecked . ' type="radio">' . JText::_('NO') . '</label>';
         $el2 .= '<label><input name="params[linkedform][' . $key . ']" value="' . $key . '" ' . $yeschecked . ' type="radio">' . JText::_('YES') . '</label>';
         $yeschecked = in_array($linkedTable->db_table_name, $linkedform_linktype) || JArrayHelper::getValue($linkedform_linktype, $index, 0) != '0' ? 'checked="checked"' : ($checked = '');
         $nochecked = $yeschecked == '' ? 'checked="checked"' : ($checked = '');
         $linkType2 = '<label><input name="params[linkedform_linktype][' . $key . ']" value="0" ' . $nochecked . ' type="radio">' . JText::_('NO') . '</label>';
         $linkType2 .= '<label><input name="params[linkedform_linktype][' . $key . ']" value="' . $key . '" ' . $yeschecked . ' type="radio">' . JText::_('YES') . '</label>';
         $formHeader = '<input name="params[linkedformheader][' . $key . ']" value="' . @$aExistingFormHeaders[$index] . '" size="16" >';
         $label = str_replace(array("\n", "\r", "<br>", "</br>"), '', $linkedTable->tablelabel);
         $formLinkText = '<input name="params[linkedformtext][' . $key . ']" value="' . @$formLinkTexts[$index] . '" size="16" >';
         $linkedArray = array($label, $el2, $formHeader, $linkType2, $formLinkText);
         $linkedArray['formhover'] = JText::_('ELEMENT') . ': ' . $linkedTable->element_label . " [{$linkedTable->plugin}]. {tmpl key =" . $linkedTable->element_id . "_form_heading}";
         $lists['linkedforms'][$index] = $linkedArray;
     }
     ksort($lists['linkedforms']);
     /*****/
     $pluginManager = JModel::getInstance('Pluginmanager', 'FabrikModel');
     $pluginManager->getPlugInGroup('table');
     // Create the form (publish dates should be stored as UTC)
     $form = new JParameter('', JPATH_COMPONENT . DS . 'models' . DS . 'table.xml');
     $form->bind($row);
     $form->set('created', JHTML::_('date', $row->created, '%Y-%m-%d %H:%M:%S'));
     $form->set('publish_up', JHTML::_('date', $row->publish_up, '%Y-%m-%d %H:%M:%S'));
     if ($cid[0] == 0 || $form->get('publish_down') == '' || $form->get('publish_down') == $db->getNullDate()) {
         $form->set('publish_down', JText::_('NEVER'));
     } else {
         $form->set('publish_down', JHTML::_('date', $row->publish_down, '%Y-%m-%d %H:%M:%S'));
     }
     $form->loadINI($row->attribs);
     $session->set('com_fabrik.admin.table.edit.model', $model);
     FabrikViewTable::edit($row, $lists, $connectionTables, $menus, $fabrikid, $params, $pluginManager, $model, $form);
 }