Exemplo n.º 1
0
 /**
  * Preparing form elements for editing Entity Type
  *
  * @return $this
  */
 protected function _prepareForm()
 {
     $form = new Varien_Data_Form(array('id' => 'edit_form', 'action' => $this->getUrl('*/*/save'), 'method' => 'post'));
     $entityType = Mage::registry('etm_entity_type');
     $fieldSet = $form->addFieldset('entity_type_data', array('class' => 'fieldset-wide', 'legend' => Mage::helper('goodahead_etm')->__('General')));
     $validateClass = sprintf('required-entry validate-code validate-length maximum-length-%d', Goodahead_Core_Helper_Data::getConstValue('Mage_Eav_Model_Entity_Attribute::ATTRIBUTE_CODE_MAX_LENGTH', 50));
     $fieldSet->addField('entity_type_code', 'text', array('label' => Mage::helper('goodahead_etm')->__('Entity Type Code'), 'name' => 'entity_type_code', 'class' => $validateClass, 'required' => true));
     $fieldSet->addField('entity_type_name', 'text', array('label' => Mage::helper('goodahead_etm')->__('Entity Type Name'), 'name' => 'entity_type_name', 'class' => 'required-entry', 'required' => true));
     if ($entityType->getId()) {
         //            $form->addField('entity_type_id', 'hidden', array(
         //                'name' => 'entity_type_id',
         //            ));
         // TODO: Rework
         $entityTypeAttributes = Mage::getModel('goodahead_etm/source_entity_attribute')->toOptionsArrayWithoutExcludedTypes($entityType, true, array('boolean', 'multiselect', 'select', 'image', 'static'));
         $fieldSet->addField('default_attribute_id', 'select', array('label' => Mage::helper('goodahead_etm')->__('Default Attribute'), 'name' => 'default_attribute_id', 'required' => false, 'values' => $entityTypeAttributes, 'note' => Mage::helper('goodahead_etm')->__('This attribute is used to display entity label')), 'entity_type_name');
         $form->getElement('entity_type_code')->setReadonly('readonly');
         $form->getElement('entity_type_code')->setDisabled(1);
         Mage::dispatchEvent('goodahead_etm_entity_types_edit_prepare_form_main_section', array('form' => $form));
         $form->setValues($entityType->getData());
     } else {
         Mage::dispatchEvent('goodahead_etm_entity_types_edit_prepare_form_main_section', array('form' => $form));
     }
     $form->setFieldNameSuffix('data');
     $this->setForm($form);
     return parent::_prepareForm();
 }
Exemplo n.º 2
0
 /**
  * Create entity tables
  *
  * @param $baseTableName
  * @param array $options
  * - no-main
  * - no-default-types
  * - types
  * @throws Mage_Core_Exception
  * @return Goodahead_Etm_Model_Entity_Setup
  */
 public function createEntityTables($baseTableName, array $options = array())
 {
     $isNoCreateMainTable = $this->_getValue($options, 'no-main', false);
     $isNoDefaultTypes = $this->_getValue($options, 'no-default-types', false);
     $customTypes = $this->_getValue($options, 'types', array());
     $tables = array();
     if (!$isNoCreateMainTable) {
         /**
          * Create table main eav table
          */
         $connection = $this->getConnection();
         $mainTable = $connection->newTable($this->getTable($baseTableName))->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array('unsigned' => true, 'identity' => true, 'nullable' => false, 'primary' => true), 'Entity Id')->addColumn('entity_type_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array('unsigned' => true, 'nullable' => false, 'default' => '0'), 'Entity Type Id')->addColumn('attribute_set_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array('unsigned' => true, 'nullable' => false, 'default' => '0'), 'Attribute Set Id')->addColumn('increment_id', Varien_Db_Ddl_Table::TYPE_VARCHAR, 50, array('nullable' => false, 'default' => ''), 'Increment Id')->addColumn('store_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array('unsigned' => true, 'nullable' => false, 'default' => '0'), 'Store Id')->addColumn('created_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array('nullable' => false), 'Created At')->addColumn('updated_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array('nullable' => false), 'Updated At')->addColumn('is_active', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array('unsigned' => true, 'nullable' => false, 'default' => '1'), 'Defines Is Entity Active')->addIndex($this->getIdxName($baseTableName, array('entity_type_id')), array('entity_type_id'))->addIndex($this->getIdxName($baseTableName, array('store_id')), array('store_id'))->addForeignKey($this->getFkName($baseTableName, 'entity_type_id', 'eav/entity_type', 'entity_type_id'), 'entity_type_id', $this->getTable('eav/entity_type'), 'entity_type_id', Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)->addForeignKey($this->getFkName($baseTableName, 'store_id', 'core/store', 'store_id'), 'store_id', $this->getTable('core/store'), 'store_id', Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE);
         $mainTable->setOption('comment', 'Eav Entity Main Table');
         $tables[$this->getTable($baseTableName)] = $mainTable;
     }
     $types = array();
     if (!$isNoDefaultTypes) {
         $types = array('datetime' => array(Goodahead_Core_Helper_Data::getConstValue('Varien_Db_Ddl_Table::TYPE_DATETIME', Varien_Db_Ddl_Table::TYPE_TIMESTAMP), null), 'decimal' => array(Varien_Db_Ddl_Table::TYPE_DECIMAL, '12,4'), 'int' => array(Varien_Db_Ddl_Table::TYPE_INTEGER, null), 'text' => array(Varien_Db_Ddl_Table::TYPE_LONGVARCHAR, '64k'), 'varchar' => array(Varien_Db_Ddl_Table::TYPE_VARCHAR, '255'), 'char' => array(Varien_Db_Ddl_Table::TYPE_VARCHAR, '255'));
     }
     if (!empty($customTypes)) {
         foreach ($customTypes as $type => $fieldType) {
             if (count($fieldType) != 2) {
                 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Wrong type definition for %s', $type));
             }
             $types[$type] = $fieldType;
         }
     }
     $_updateTableName = null;
     /**
      * Create table array($baseTableName, $type)
      */
     foreach ($types as $type => $fieldType) {
         $eavTableName = array($baseTableName, $type);
         $eavTable = $connection->newTable($this->getTable($eavTableName));
         $eavTable->addColumn('value_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array('unsigned' => true, 'identity' => true, 'nullable' => false, 'primary' => true), 'Value Id')->addColumn('entity_type_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array('unsigned' => true, 'nullable' => false, 'default' => '0'), 'Entity Type Id')->addColumn('attribute_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array('unsigned' => true, 'nullable' => false, 'default' => '0'), 'Attribute Id')->addColumn('store_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array('unsigned' => true, 'nullable' => false, 'default' => '0'), 'Store Id')->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array('unsigned' => true, 'nullable' => false, 'default' => '0'), 'Entity Id')->addColumn('value', $fieldType[0], $fieldType[1], array('nullable' => true), 'Attribute Value')->addIndex($this->getIdxName($eavTableName, array('entity_type_id')), array('entity_type_id'))->addIndex($this->getIdxName($eavTableName, array('attribute_id')), array('attribute_id'))->addIndex($this->getIdxName($eavTableName, array('store_id')), array('store_id'))->addIndex($this->getIdxName($eavTableName, array('entity_id')), array('entity_id'));
         if ($type !== 'text') {
             $eavTable->addIndex($this->getIdxName($eavTableName, array('attribute_id', 'value')), array('attribute_id', 'value'));
             $eavTable->addIndex($this->getIdxName($eavTableName, array('entity_type_id', 'value')), array('entity_type_id', 'value'));
         }
         if ($fieldType[0] == Varien_Db_Ddl_Table::TYPE_TIMESTAMP) {
             $_updateTableName = $this->getTable($eavTableName);
         }
         $eavTable->addForeignKey($this->getFkName($eavTableName, 'entity_id', $baseTableName, 'entity_id'), 'entity_id', $this->getTable($baseTableName), 'entity_id', Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)->addForeignKey($this->getFkName($eavTableName, 'entity_type_id', 'eav/entity_type', 'entity_type_id'), 'entity_type_id', $this->getTable('eav/entity_type'), 'entity_type_id', Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)->addForeignKey($this->getFkName($eavTableName, 'store_id', 'core/store', 'store_id'), 'store_id', $this->getTable('core/store'), 'store_id', Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE);
         $eavTable->setOption('comment', 'Eav Entity Value Table');
         $tables[$this->getTable($eavTableName)] = $eavTable;
     }
     //$connection->beginTransaction();
     try {
         foreach ($tables as $tableName => $table) {
             $this->createTable($table);
             if (isset($_updateTableName) && $_updateTableName == $tableName) {
                 $connection->changeColumn($tableName, 'value', 'value', 'DATETIME');
             }
         }
         //$connection->commit();
     } catch (Exception $e) {
         //$connection->rollBack();
         throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Can\'t create table: %s', $tableName));
     }
     return $this;
 }
        if (empty($item['source_model'])) {
            $update['source_model'] = $helper->getAttributeSourceModelByInputType($item['frontend_input']);
        }
        if (empty($item['backend_model'])) {
            $update['backend_model'] = $helper->getAttributeBackendModelByInputType($item['frontend_input']);
        }
        if (empty($item['frontend_model'])) {
            $update['frontend_model'] = $helper->getAttributeFrontendModelByInputType($item['frontend_input']);
        }
        $update = array_filter($update);
        if (!empty($update)) {
            $installer->getConnection()->update($installer->getTable('eav/attribute'), $update, $installer->getConnection()->quoteInto('attribute_id = ?', $item['attribute_id']));
        }
    }
}
$installer->getConnection()->dropColumn($installer->getTable('goodahead_etm/eav_attribute'), 'attribute_name');
$installer->getConnection()->addColumn($installer->getTable('goodahead_etm/eav_attribute'), 'sort_order', 'INT(4) UNSIGNED NOT NULL DEFAULT 0');
$select = $installer->getConnection()->select();
$select->from(array('main_table' => $installer->getTable('eav/entity_type')), array('entity_type_id', 'entity_type_code'));
$select->joinInner(array('g' => $installer->getTable('goodahead_etm/eav_entity_type')), 'main_table.entity_type_id = g.entity_type_id', array());
$items = $installer->getConnection()->fetchAll($select);
if ($items && is_array($items)) {
    foreach ($items as $item) {
        $installer->getConnection()->update($installer->getTable('eav/entity_type'), array('entity_model' => sprintf('goodahead_etm/custom_%s_entity', $item['entity_type_code']), 'attribute_model' => 'goodahead_etm/entity_attribute', 'entity_table' => 'goodahead_etm/entity', 'additional_attribute_table' => 'goodahead_etm/eav_attribute', 'entity_attribute_collection' => 'goodahead_etm/entity_attribute_collection'), $installer->getConnection()->quoteInto('entity_type_id = ?', $item['entity_type_id']));
    }
}
$tableTypes = array('int', 'varchar', 'char', 'text', 'decimal', 'datetime');
foreach ($tableTypes as $type) {
    $installer->getConnection()->addKey($installer->getTable('goodahead_etm/entity') . '_' . $type, $installer->getIdxName($installer->getTable('goodahead_etm/entity') . '_' . $type, array('entity_type_id', 'attribute_id', 'store_id', 'entity_id')), array('entity_type_id', 'attribute_id', 'store_id', 'entity_id'), Goodahead_Core_Helper_Data::getConstValue('Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE', Goodahead_Core_Model_Resource_Setup_Compatibility::INDEX_TYPE_UNIQUE));
}
$installer->endSetup();