Esempio n. 1
0
 /**
  * When saving a table that links to a database for the first time we
  * need to create all the elements based on the database table fields and their
  * column type
  *
  * @param   int     $groupId    group id
  * @param   string  $tableName  table name - if not set then use jform's db_table_name (@since 3.1)
  *
  * @return  void
  */
 private function createLinkedElements($groupId, $tableName = '')
 {
     $db = FabrikWorker::getDbo(true);
     $app = JFactory::getApplication();
     $input = $app->input;
     $user = JFactory::getUser();
     $config = JFactory::getConfig();
     $createdate = JFactory::getDate();
     $createdate = $createdate->toSql();
     if ($tableName === '') {
         $jform = $input->get('jform', array(), 'array');
         $tableName = JArrayHelper::getValue($jform, 'db_table_name');
     }
     $formModel = $this->getFormModel();
     $pluginManager = FabrikWorker::getPluginManager();
     $ordering = 0;
     $fabrikDb = $this->getFEModel()->getDb();
     $groupTable = FabTable::getInstance('Group', 'FabrikTable');
     $groupTable->load($groupId);
     // Here we're importing directly from the database schema
     $query = $db->getQuery(true);
     $query->select('id')->from('#__{package}_lists')->where('db_table_name = ' . $db->quote($tableName));
     $db->setQuery($query);
     $id = $db->loadResult();
     $dispatcher = JDispatcher::getInstance();
     $elementModel = new PlgFabrik_Element($dispatcher);
     if ($id) {
         // A fabrik table already exists - so we can copy the formatting of its elements
         $groupListModel = JModelLegacy::getInstance('list', 'FabrikFEModel');
         $groupListModel->setId($id);
         $groupListModel->getTable();
         $groups = $groupListModel->getFormGroupElementData();
         $newElements = array();
         $ecount = 0;
         foreach ($groups as $groupModel) {
             /**
              * If we are saving a new table and the previously found tables group is a join
              * then don't add its elements to the table as they don't exist in the database table
              * we are linking to
              * $$$ hugh - why the test for task and new table?  When creating elements for a copy of a table,
              * surely we NEVER want to include elements which were joined to the original,
              * regardless of whether this is a new List?  Bearing in mind that this routine gets called from
              * the makeNewJoin() method, when adding a join to an existing list, to build the "Foo - [bar]" join
              * group, as well as from save() when creating a new List.
              *
              *  if ($groupModel->isJoin() && $input->get('task') == 'save' && $input->getInt('id') == 0)
              */
             if ($groupModel->isJoin()) {
                 continue;
             }
             $elementModels =& $groupModel->getMyElements();
             foreach ($elementModels as $elementModel) {
                 $ecount++;
                 $element = $elementModel->getElement();
                 $copy = $elementModel->copyRow($element->id, $element->label, $groupId);
                 $newElements[$element->id] = $copy->id;
             }
         }
         foreach ($newElements as $origId => $newId) {
             $plugin = $pluginManager->getElementPlugin($newId);
             $plugin->finalCopyCheck($newElements);
         }
         // Hmm table with no elements - lets create them from the structure anyway
         if ($ecount == 0) {
             $this->makeElementsFromFields($groupId, $tableName);
         }
     } else {
         // No previously found fabrik list
         $this->makeElementsFromFields($groupId, $tableName);
     }
 }