Esempio n. 1
0
 /**
  * Create a table to store the forms' data depending upon what groups are assigned to the form
  *
  * @param   string $dbTableName Taken from the table object linked to the form
  * @param   array  $fields      List of default elements to add. (key = element name, value = plugin
  * @param   array  $opts        Additional options, e.g. collation
  *
  * @return mixed false if fail otherwise array of primary keys
  */
 public function createDBTable($dbTableName = null, $fields = array('id' => 'internalid', 'date_time' => 'date'), $opts = array())
 {
     $db = FabrikWorker::getDbo(true);
     $fabrikDb = $this->getDb();
     $formModel = $this->getFormModel();
     if (is_null($dbTableName)) {
         $dbTableName = $this->getTable()->db_table_name;
     }
     $sql = 'CREATE TABLE IF NOT EXISTS ' . $db->qn($dbTableName) . ' (';
     $input = $this->app->input;
     $jForm = $input->get('jform', array(), 'array');
     if ($jForm['id'] == 0 && array_key_exists('current_groups', $jForm)) {
         // Saving a new form
         $groupIds = $jForm['current_groups'];
     } else {
         $query = $db->getQuery(true);
         $formId = (int) $this->get('form.id', $this->getFormModel()->id);
         $query->select('group_id')->from('#__{package}_formgroup')->where('form_id = ' . $formId);
         $db->setQuery($query);
         $groupIds = $db->loadColumn();
     }
     $i = 0;
     foreach ($fields as $name => $plugin) {
         // $$$ hugh - testing corner case where we are called from form model's updateDatabase,
         // and the underlying table has been deleted.  So elements already exist.
         $element = $formModel->getElement($name);
         if ($element === false) {
             // Installation demo data sets 2 group ids
             if (is_string($plugin)) {
                 $plugin = array('plugin' => $plugin, 'group_id' => $groupIds[0]);
             }
             $plugin['ordering'] = $i;
             $element = $this->makeElement($name, $plugin);
             if (!$element) {
                 return false;
             }
         }
         $elementModels[] = clone $element;
         $i++;
     }
     $arAddedObj = array();
     $keys = array();
     $lines = array();
     foreach ($elementModels as $elementModel) {
         $element = $elementModel->getElement();
         // Replace all non alphanumeric characters with _
         $objName = FabrikString::dbFieldName($element->name);
         if ($element->get('primary_key') || $element->get('plugin') === 'internalid') {
             $keys[] = $objName;
         }
         // Any elements that are names the same (eg radio buttons) can not be entered twice into the database
         if (!in_array($objName, $arAddedObj)) {
             $arAddedObj[] = $objName;
             $objType = $elementModel->getFieldDescription();
             if ($objName != '' && !is_null($objType)) {
                 if (JString::stristr($objType, 'not null')) {
                     $lines[] = $fabrikDb->qn($objName) . ' ' . $objType;
                 } else {
                     $lines[] = $fabrikDb->qn($objName) . ' ' . $objType . ' null';
                 }
             }
         }
     }
     $func = create_function('$value', '$db = FabrikWorker::getDbo(true);;return $db->qn($value);');
     $sql .= implode(', ', $lines);
     if (!empty($keys)) {
         $sql .= ', PRIMARY KEY (' . implode(',', array_map($func, $keys)) . '))';
     } else {
         $sql .= ')';
     }
     foreach ($opts as $k => $v) {
         if ($v != '') {
             $sql .= ' ' . $k . ' ' . $v;
         }
     }
     $sql .= ' ENGINE = MYISAM ';
     $fabrikDb->setQuery($sql);
     $fabrikDb->execute();
     return $keys;
 }
Esempio n. 2
0
 /**
  * Create a table to store the forms' data depending upon what groups are assigned to the form
  *
  * @param   string  $dbTableName  taken from the table oject linked to the form
  * @param   array   $fields       list of default elements to add. (key = element name, value = plugin
  *
  * @return mixed false if fail otherwise array of primary keys
  */
 public function createDBTable($dbTableName = null, $fields = array('id' => 'internalid', 'date_time' => 'date'))
 {
     $db = FabrikWorker::getDbo(true);
     $fabrikDb = $this->getDb();
     $user = JFactory::getUser();
     $config = JFactory::getConfig();
     $formModel = $this->getFormModel();
     if (is_null($dbTableName)) {
         $dbTableName = $this->getTable()->db_table_name;
     }
     $sql = 'CREATE TABLE IF NOT EXISTS ' . $db->quoteName($dbTableName) . ' (';
     $post = JRequest::get('post');
     if (array_key_exists('jform', $post) && ($post['jform']['id'] == 0 && array_key_exists('current_groups', $post['jform']))) {
         // Saving a new form
         $groupIds = $post['jform']['current_groups'];
     } else {
         $query = $db->getQuery(true);
         $formid = (int) $this->get('form.id', $this->getFormModel()->id);
         $query->select('group_id')->from('#__{package}_formgroup')->where('form_id = ' . $formid);
         $db->setQuery($query);
         $groupIds = $db->loadResultArray();
     }
     $i = 0;
     foreach ($fields as $name => $plugin) {
         // $$$ hugh - testing corner case where we are called from form model's updateDatabase,
         // and the underlying table has been deleted.  So elements already exist.
         $element = $formModel->getElement($name);
         if ($element === false) {
             // Installation demo data sets 2 groud ids
             if (is_string($plugin)) {
                 $plugin = array('plugin' => $plugin, 'group_id' => $groupIds[0]);
             }
             $plugin['ordering'] = $i;
             $element = $this->makeElement($name, $plugin);
             if (!$element) {
                 return false;
             }
         }
         $elementModels[] = clone $element;
         $i++;
     }
     $arAddedObj = array();
     $keys = array();
     $lines = array();
     foreach ($elementModels as $elementModel) {
         $element = $elementModel->getElement();
         // Replace all non alphanumeric characters with _
         $objname = FabrikString::dbFieldName($element->name);
         //preg_replace("/[^A-Za-z0-9]/", "_", $element->name);
         if ($element->primary_key) {
             $keys[] = $objname;
         }
         // Any elements that are names the same (eg radio buttons) can not be entered twice into the database
         if (!in_array($objname, $arAddedObj)) {
             $arAddedObj[] = $objname;
             $objtype = $elementModel->getFieldDescription();
             if ($objname != "" && !is_null($objtype)) {
                 if (JString::stristr($objtype, 'not null')) {
                     $lines[] = $fabrikDb->quoteName($objname) . " {$objtype}";
                 } else {
                     $lines[] = $fabrikDb->quoteName($objname) . " {$objtype} null";
                 }
             }
         }
     }
     $func = create_function('$value', '$db = FabrikWorker::getDbo(true);;return $db->quoteName($value);');
     $sql .= implode(', ', $lines);
     if (!empty($keys)) {
         $sql .= ', PRIMARY KEY (' . implode(',', array_map($func, $keys)) . '))';
     } else {
         $sql .= ')';
     }
     $sql .= ' ENGINE = MYISAM ';
     $fabrikDb->setQuery($sql);
     if (!$fabrikDb->query()) {
         JError::raiseError(500, $fabrikDb->getErrorMsg());
         return false;
     }
     return $keys;
 }
Esempio n. 3
0
 /**
  * Make or update the list from the CSV file
  *
  * @return  null
  */
 public function makeTableFromCSV()
 {
     // Called when creating new elements from csv import into existing list
     $session = JFactory::getSession();
     $app = JFactory::getApplication();
     $input = $app->input;
     $model = $this->getModel('Importcsv', 'FabrikFEModel');
     $model->import();
     $listId = $input->getInt('fabrik_list', $input->get('listid'));
     if ($listId == 0) {
         $plugins = $input->get('plugin', array(), 'array');
         $createElements = $input->get('createElements', array(), 'array');
         $newElements = array();
         $c = 0;
         $dbName = $input->get('db_table_name', '', 'string');
         $model->matchedHeadings = array();
         foreach ($createElements as $elname => $add) {
             if ($add) {
                 $name = FabrikString::dbFieldName($elname);
                 $plugin = $plugins[$c];
                 $newElements[$name] = $plugin;
                 $model->matchedHeadings[$dbName . '.' . $name] = $name;
             }
             $c++;
         }
         // Stop id and date_time being added to the table and instead use $newElements
         $input->set('defaultfields', $newElements);
         // Create db
         $listModel = $this->getModel('list', 'FabrikAdminModel');
         $data = array('id' => 0, '_database_name' => $dbName, 'connection_id' => $input->getInt('connection_id'), 'access' => 0, 'rows_per_page' => 10, 'template' => 'default', 'published' => 1, 'access' => 1, 'label' => $input->getString('label'), 'jform' => array('id' => 0, '_database_name' => $dbName, 'db_table_name' => ''));
         $input->set('jform', $data['jform']);
         $listModel->save($data);
         $model->listModel = null;
         $input->set('listid', $listModel->getItem()->id);
     } else {
         $headings = $session->get('com_fabrik.matchedHeadings');
         $model->matchedHeadings = $this->addElements($model, $headings);
         $model->listModel = null;
         $input->set('listid', $listId);
     }
     $model->insertData();
     $msg = $model->updateMessage();
     $this->setRedirect('index.php?option=com_fabrik&view=lists', $msg);
 }
Esempio n. 4
0
 /**
  * Make or update the list from the CSV file
  *
  * @return  null
  */
 public function makeTableFromCSV()
 {
     // Called when creating new elements from csv import into existing list
     $session = JFactory::getSession();
     $app = JFactory::getApplication();
     $input = $app->input;
     $jform = $input->get('jform', null, 'array');
     /** @var FabrikFEModelImportcsv $model */
     $model = $this->getModel('Importcsv', 'FabrikFEModel');
     $listId = (int) $input->getInt('fabrik_list', $input->get('listid'));
     if ($listId === 0) {
         $plugins = $input->get('plugin', array(), 'array');
         $createElements = $input->get('createElements', array(), 'array');
         $newElements = array();
         $c = 0;
         $dbName = $input->get('db_table_name', '', 'string');
         $model->matchedHeadings = array();
         foreach ($createElements as $elName => $add) {
             if ($add) {
                 $name = FabrikString::dbFieldName($elName);
                 $plugin = $plugins[$c];
                 $newElements[$name] = $plugin;
                 $model->matchedHeadings[$dbName . '.' . $name] = $name;
             }
             $c++;
         }
         // Stop id and date_time being added to the table and instead use $newElements
         $input->set('defaultfields', $newElements);
         /** @var FabrikAdminModelList $listModel */
         $listModel = $this->getModel('list', 'FabrikAdminModel');
         /**
          * Create db
          *
          * @TODO should probably add an ACL option to the import options, as we now have to set 'access'
          * to something for the elementtype import.  Defaulting to 1 for now.
          */
         $data = array('id' => 0, '_database_name' => $dbName, 'connection_id' => $input->getInt('connection_id'), 'rows_per_page' => 10, 'template' => 'default', 'published' => 1, 'access' => 1, 'label' => $input->getString('label'), 'jform' => array('id' => 0, '_database_name' => $dbName, 'db_table_name' => '', 'contenttype' => null));
         $input->set('jform', $data['jform']);
         $listModel->save($data);
         $model->listModel = null;
         $input->set('listid', $listModel->getItem()->id);
         // Reset back to import CSV jForm data.
         $input->set('jform', $jform);
     } else {
         $headings = $session->get('com_fabrik.matchedHeadings');
         $model->matchedHeadings = $this->addElements($model, $headings);
         $model->listModel = null;
         $input->set('listid', $listId);
     }
     $model->readCSV($model->getCSVFileName());
     $model->insertData();
     $msg = $model->updateMessage();
     $this->setRedirect('index.php?option=com_fabrik&view=lists', $msg);
 }