Exemple #1
0
 /**
  * Take a table name and make elements for all of its fields
  *
  * @param   int    $groupId   group id
  * @param   string $tableName table name
  *
  * @return  void
  */
 protected function makeElementsFromFields($groupId, $tableName)
 {
     $fabrikDb = $this->getFEModel()->getDb();
     $dispatcher = JEventDispatcher::getInstance();
     $input = $this->app->input;
     $elementModel = new PlgFabrik_Element($dispatcher);
     $pluginManager = FabrikWorker::getPluginManager();
     $fbConfig = JComponentHelper::getParams('com_fabrik');
     $elementTypes = $input->get('elementtype', array(), 'array');
     $fields = $fabrikDb->getTableColumns($tableName, false);
     $createDate = JFactory::getDate()->toSQL();
     $key = $this->getFEModel()->getPrimaryKeyAndExtra($tableName);
     $ordering = 0;
     /**
      * no existing fabrik table so we take a guess at the most
      * relevant element types to  create
      */
     $elementLabels = $input->get('elementlabels', array(), 'array');
     foreach ($fields as $label => $properties) {
         $plugin = 'field';
         $type = $properties->Type;
         $maxLength = 255;
         $maxLength2 = 0;
         if (preg_match("/\\((.*)\\)/i", $type, $matches)) {
             $maxLength = FArrayHelper::getValue($matches, 1, 255);
             $maxLength = explode(',', $maxLength);
             if (count($maxLength) > 1) {
                 $maxLength2 = $maxLength[1];
                 $maxLength = $maxLength[0];
             } else {
                 $maxLength = $maxLength[0];
                 $maxLength2 = 0;
             }
         }
         // Get the basic type
         $type = explode(" ", $type);
         $type = FArrayHelper::getValue($type, 0, '');
         $type = preg_replace("/\\((.*)\\)/i", '', $type);
         $element = FabTable::getInstance('Element', 'FabrikTable');
         if (array_key_exists($ordering, $elementTypes)) {
             // If importing from a CSV file then we have userselect field definitions
             $plugin = $elementTypes[$ordering];
         } else {
             // If the field is the primary key and it's an INT type set the plugin to be the fabrik internal id
             if ($key[0]['colname'] == $label && JString::strtolower(substr($key[0]['type'], 0, 3)) === 'int') {
                 $plugin = 'internalid';
             } else {
                 // Otherwise set default type
                 switch ($type) {
                     case "int":
                     case "decimal":
                     case "tinyint":
                     case "smallint":
                     case "mediumint":
                     case "bigint":
                     case "varchar":
                     case "time":
                         $plugin = 'field';
                         break;
                     case "text":
                     case "tinytext":
                     case "mediumtext":
                     case "longtext":
                         $plugin = 'textarea';
                         break;
                     case "datetime":
                     case "date":
                     case "timestamp":
                         $plugin = 'date';
                         break;
                     default:
                         $plugin = 'field';
                         break;
                 }
             }
             // Then alter if defined in Fabrik global config
             // Jaanus: but first check if there are any pk field and if yes then create as internalid
             $defType = JString::strtolower(substr($key[0]['type'], 0, 3));
             $plugin = $key[0]['colname'] == $label && $defType === 'int' ? 'internalid' : $fbConfig->get($type, $plugin);
         }
         $element->plugin = $plugin;
         $element->hidden = $element->label == 'id' ? '1' : '0';
         $element->group_id = $groupId;
         $element->name = $label;
         $element->created = $createDate;
         $element->created_by = $this->user->get('id');
         $element->created_by_alias = $this->user->get('username');
         $element->published = '1';
         $element->show_in_list_summary = '1';
         switch ($plugin) {
             case 'textarea':
                 $element->width = '40';
                 break;
             case 'date':
                 $element->width = '10';
                 break;
             default:
                 $element->width = '30';
                 break;
         }
         if ($element->width > $maxLength) {
             $element->width = $maxLength;
         }
         $element->height = '6';
         $element->ordering = $ordering;
         $p = json_decode($elementModel->getDefaultAttribs());
         if (in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && $plugin == 'field') {
             $p->integer_length = $maxLength;
             $p->text_format = 'integer';
             $p->maxlength = '255';
             $element->width = '30';
         } elseif ($type == 'decimal' && $plugin == 'field') {
             $p->text_format = 'decimal';
             $p->decimal_length = $maxLength2;
             $p->integer_length = $maxLength - $maxLength2;
             $p->maxlength = '255';
             $element->width = '30';
         } else {
             $p->maxlength = $maxLength;
         }
         $element->params = json_encode($p);
         $element->label = FArrayHelper::getValue($elementLabels, $ordering, str_replace("_", " ", $label));
         //Format Label
         $labelConfig = $fbConfig->get('format_labels', '0');
         switch ($labelConfig) {
             case '1':
                 $element->label = strtolower($element->label);
                 break;
             case '2':
                 $element->label = ucwords($element->label);
                 break;
             case '3':
                 $element->label = ucfirst($element->label);
                 break;
             case '4':
                 $element->label = strtoupper($element->label);
                 break;
             default:
                 break;
         }
         $element->store();
         $elementModel = $pluginManager->getPlugIn($element->plugin, 'element');
         $elementModel->setId($element->id);
         $elementModel->element = $element;
         // Hack for user element
         $details = array('group_id' => $element->group_id);
         $input->set('details', $details);
         $elementModel->onSave(array());
         $ordering++;
     }
 }