Advertisement:

Author Topic: Temporary Solution for Ordering/Sorting Custom Fields  (Read 1125 times)

Francesko

  • Newbie
  • *
  • Posts: 9
Temporary Solution for Ordering/Sorting Custom Fields
« on: September 15, 2017, 06:35:17 pm »
Do this modification on your own responsibility!
Make a backup of database & (Field.php , Item.php) files in "\oc-includes\osclass\model\" before doing any changes!


NOTE: After this modification you need to edit manually the order/position for each custom field on the database! You can't do it on oc-admin!
If you add a new custom field make sure to edit the position number or it will be displayed at the top of custom fields!


Requirements:

1.Basic knowledge of PHP and SQL (MySQL).
2.Access to database, (phpmyadmin) or mysql command line.

Tasks:
1.Add a column to oc_t_meta_fields table.
2.Edit files (Field.php , Item.php) in "\oc-includes\osclass\model\"

===========================================
First you need to create a column to oc_t_meta_fields table. To do that, login to PhpMyAdmin, Select the database that you have installed Osclass, click on SQL tab and run this query:

Code: [Select]
ALTER TABLE `oc_t_meta_fields` ADD `c_position` INT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `b_searchable`;
Warning: If you have changed the default Database Table Prefix don't forget to change the oc prefix from code above! (For example: customprefix_t_meta_fields)





===========================================

Go to \oc-includes\osclass\model\ and edit:

Field.php:

Find: (Line 62)
Code: [Select]
$this->setFields( array('pk_i_id', 's_name', 'e_type', 'b_required', 'b_searchable', 's_slug', 's_options') );
Replace with:
Code: [Select]
$this->setFields( array('pk_i_id', 's_name', 'e_type', 'b_required', 'b_searchable', 's_slug', 's_options', 'c_position') );

Find: (Line 96)
Code: [Select]
        public function findByCategory($id)
        {
            $this->dao->select('mf.*');
            $this->dao->from(sprintf('%st_meta_fields mf, %st_meta_categories mc', DB_TABLE_PREFIX, DB_TABLE_PREFIX));
            $this->dao->where('mc.fk_i_category_id', $id);
            $this->dao->where('mf.pk_i_id = mc.fk_i_field_id');

            $result = $this->dao->get();

            if( $result == false ) {
                return array();
            }

            return $result->result();
        }
Replace with:
Code: [Select]
        public function findByCategory($id)
        {
            $this->dao->select('mf.*');
            $this->dao->from(sprintf('%st_meta_fields mf, %st_meta_categories mc', DB_TABLE_PREFIX, DB_TABLE_PREFIX));
            $this->dao->where('mc.fk_i_category_id', $id);
            $this->dao->where('mf.pk_i_id = mc.fk_i_field_id');
            $this->dao->orderBy('c_position', 'ASC');
            $result = $this->dao->get();

            if( $result == false ) {
                return array();
            }

            return $result->result();
        }

Find: (Line 164)
Code: [Select]
        public function findByCategoryItem($catId, $itemId)
        {
            if( !is_numeric($catId) || (!is_numeric($itemId) && $itemId != null) ) {
                return array();
            }

            $result = $this->dao->query(sprintf("SELECT query.*, im.s_value as s_value, im.fk_i_item_id FROM (SELECT mf.* FROM %st_meta_fields mf, %st_meta_categories mc WHERE mc.fk_i_category_id = %d AND mf.pk_i_id = mc.fk_i_field_id) as query LEFT JOIN %st_item_meta im ON im.fk_i_field_id = query.pk_i_id AND im.fk_i_item_id = %d group by pk_i_id", DB_TABLE_PREFIX, DB_TABLE_PREFIX, $catId, DB_TABLE_PREFIX, $itemId));

            if( $result == false ) {
                return array();
            }

            return $result->result();
        }
Replace with:
Code: [Select]
        public function findByCategoryItem($catId, $itemId)
        {
            if( !is_numeric($catId) || (!is_numeric($itemId) && $itemId != null) ) {
                return array();
            }

            $result = $this->dao->query(sprintf("SELECT query.*, im.s_value as s_value, im.fk_i_item_id FROM (SELECT mf.* FROM %st_meta_fields mf, %st_meta_categories mc WHERE mc.fk_i_category_id = %d AND mf.pk_i_id = mc.fk_i_field_id) as query LEFT JOIN %st_item_meta im ON im.fk_i_field_id = query.pk_i_id AND im.fk_i_item_id = %d group by pk_i_id order by c_position", DB_TABLE_PREFIX, DB_TABLE_PREFIX, $catId, DB_TABLE_PREFIX, $itemId));

            if( $result == false ) {
                return array();
            }

            return $result->result();
        }

===========================================
Go to \oc-includes\osclass\model\ and edit:

Item.php:

Find: (Line 904)
Code: [Select]
        public function metaFields($id)
        {
            $this->dao->select('im.s_value as s_value,mf.pk_i_id as pk_i_id, mf.s_name as s_name, mf.e_type as e_type, im.s_multi as s_multi, mf.s_slug as s_slug ');
            $this->dao->from($this->getTableName().' i, '.DB_TABLE_PREFIX.'t_item_meta im, '.DB_TABLE_PREFIX.'t_meta_categories mc, '.DB_TABLE_PREFIX.'t_meta_fields mf');
            $this->dao->where('mf.pk_i_id = im.fk_i_field_id');
            $this->dao->where('mf.pk_i_id = mc.fk_i_field_id');
            $this->dao->where('mc.fk_i_category_id = i.fk_i_category_id');
            $array_where = array(
                'im.fk_i_item_id'       => $id,
                'i.pk_i_id'             => $id
            );
            $this->dao->where($array_where);
            $result = $this->dao->get();
            if($result == false) {
                return array();
            }
            $aTemp = $result->result();
Replace with:
Code: [Select]
public function metaFields($id)
        {
            $this->dao->select('im.s_value as s_value,mf.pk_i_id as pk_i_id, mf.s_name as s_name, mf.e_type as e_type, im.s_multi as s_multi, mf.s_slug as s_slug ');
            $this->dao->from($this->getTableName().' i, '.DB_TABLE_PREFIX.'t_item_meta im, '.DB_TABLE_PREFIX.'t_meta_categories mc, '.DB_TABLE_PREFIX.'t_meta_fields mf');
            $this->dao->where('mf.pk_i_id = im.fk_i_field_id');
            $this->dao->where('mf.pk_i_id = mc.fk_i_field_id');
            $this->dao->where('mc.fk_i_category_id = i.fk_i_category_id');
            $array_where = array(
                'im.fk_i_item_id'       => $id,
                'i.pk_i_id'             => $id
            );
            $this->dao->where($array_where);
$this->dao->orderBy('c_position', 'ASC');
            $result = $this->dao->get();
            if($result == false) {
                return array();
            }
            $aTemp = $result->result();

Done!


To edit order/position of the custom field go to phpmyadmin, --> osclass database, select oc_t_meta_fields table and modify the value of c_position.



Hope this helps! :)

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Temporary Solution for Ordering/Sorting Custom Fields
« Reply #1 on: September 15, 2017, 06:57:44 pm »
This is like in opencart. You manually enter the position.
1.What happens if you leave them all to 0? It will order them by id?

2. What happens if you add more custom fields after you've made these changes?

jacques1966

  • Newbie
  • *
  • Posts: 43
  • Wannabe Advertising Mogul
Re: Temporary Solution for Ordering/Sorting Custom Fields
« Reply #2 on: September 16, 2017, 07:13:16 am »
@Francesko

I sort of understand what you are trying to say (But my inability to fully comprehend is NOT the reason I am posting here)

The reason for my posting is to compliment you on the way you have described the process !

Using red for the "before" and green for the "after" code.
Your image attachments are also very descriptive.
From the looks of things you may either be a very good orator or a great teacher - either way - very good illustrative display method !!!!

Pepper

  • Newbie
  • *
  • Posts: 13
Re: Temporary Solution for Ordering/Sorting Custom Fields
« Reply #3 on: October 07, 2017, 03:49:56 pm »
Forum should put like buttons!
Thanks, very use full Francesko

ayman83

  • Jr. Member
  • **
  • Posts: 82
Re: Temporary Solution for Ordering/Sorting Custom Fields
« Reply #4 on: October 17, 2017, 07:17:24 pm »
Great Great Great  :D