Install script for j3 and j4

97 views
Skip to first unread message

Roger Creagh

unread,
Jun 30, 2021, 5:46:37 AM6/30/21
to joomla-dev-general
Possibly two questions for Glen who offered to look at this, but I'm posting here so anyone else who has the same problem can see it (or answer if they know how)...

Q1 - In my install script I like to report to the user in a message what version of the component has just been installed/uninstalled. In J3 I read the version from the manifest file as the best source of truth thus:

    function postflight($type, $parent) {
    	$message = 'Installing xbOSM component v.'.$parent->get('manifest')->version.' '.$parent->get('manifest')->creationDate;
    	Factory::getApplication()->enqueueMessage($message,'Info');
...

And a similar thing when uninstalling in the uninstall function

$parent->get('manifest') causes a "Server Error" in j4,what can I use for a J4 install to get the version instead that will also work in J3?

Q2 - with some components when installing I need to create a default category ('Uncategorised') for the component in the install script. You can't do this in the sql install script because if the component has been previously installed the category will already exist and mysql barfs at the insert statement. You also don't know the correct values for lft, rgt, or created_user_id columns in the sql (the others you can make defaults for)
So what I usually do is this in the install php script

            $message .= 'Creating xbOSM categories : ';
            $category_data['id'] = 0;
            $category_data['parent_id'] = 0;
            $category_data['extension'] = 'com_xbosm';
            $category_data['published'] = 1;
            $category_data['language'] = '*';
            $category_data['params'] = array('category_layout' => '','image' => '');
            $category_data['metadata'] = array('author' => '','robots' => '');
            
            $basePath = JPATH_ADMINISTRATOR.'/components/com_categories';
            require_once $basePath.'/models/category.php';
            $config  = array('table_path' => $basePath.'/tables');
            $category_model = new CategoriesModelCategory($config);
            
            $db = Factory::getDBO();
            $query = $db->getQuery(true);
            $query->select('id')->from($db->quoteName('#__categories'))
            	->where($db->quoteName('alias')." = ".$db->quote('uncategorised'))
            	->where($db->quoteName('extension')." = ".$db->quote('com_xbosm'));
            $db->setQuery($query);
            if ($db->loadResult()>0) {
            	$message .= '"xbOSM Uncategorised" category already exists... ';
            } else {
	            $category_data['title'] = 'Uncategorised';
	            $category_data['alias'] = 'uncategorised';
	            $category_data['description'] = 'Default category for xbOSM items not otherwise assigned';
	            
	            if(!$category_model->save($category_data)){
	                $message .= '<br />[Error creating Uncategorised: '.$category_model->getError().']<br /> ';
	            }else{
	                $message .= '"Uncategorised" (id='. $category_model->getItem()->id.') created... ';
	            }
            }
            $query->clear();

	        Factory::getApplication()->enqueueMessage($message,'Info');        

Thus using the com_category  model to create the new category for the component, or report if it already exists. Works fine in j3, not in j4.

What should this be to work in both j3 and j4?

Hopefully someone has solved this one and can share the correct way....

Roger
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Roger Creagh

unread,
Jul 2, 2021, 2:45:28 AM7/2/21
to joomla-de...@googlegroups.com
Thank you Glenn, this code works in the install script for j4 (and j3) whereas mine fails with a "Server Error" and no log.

You are using 

 $category = Table::getInstance('Category');
...
$category->store(true)

rather than 

$basePath = JPATH_ADMINISTRATOR.'/components/com_categories';
require_once $basePath.'/models/category.php';
$config  = array('table_path' => $basePath.'/tables');

$category_model = new CategoriesModelCategory($config);

...
$category_model->save($category_data)

It seems odd that using the Table works but the Model doesn't, but hey ho. Your code seems neater so I'll switch to that.

Many thanks for this.

Unfortunately my foray into j4 has merely convinced me that it is a very retrograde step - the admin template design is incredibly wasteful of screen real estate and flexibility. 

For me the use of the sidebar for the system menu is much worse that the top bar in j3 - 20% of the screen is gone until you collapse it and then you can't see your component menu to switch admin views. 

The loss of flexibility of having a separate system menu across the top giving access to the components with an optional separate component specific submenu in the (full screen width) component admin space as compared with the j4 way of havingcombining it all in a compulsory sidebar is an enormous backward step. 

I could go on, but I don't think I'll even consider j4 until someone produces a better admin template. Not all progress is better.

Thanks again for your always good advice and help
Roger

On Wed, 2021-06-30 at 03:39 -0700, Glenn Arkell wrote:
Hi Roger,  things changed regarding getting the manifest in J4 that I recall, I need to find my notes on this with a fresh mind in the morning.  But here is how I create categories for the time being.  Cheers.
Glenn
Q2: 
I use the postflight to create categories as required like this -

    /**
     * The extension name. This should be set in the installer script.
     */
    protected $extension = 'com_mycomponent';


    public function postflight($type, $parent)
    {
        // $parent is the class calling this method
        // $type is the type of change (install, update or discover_install)
        // the variable $type was returning lowercase values so have inserted STRTOUPPER
        $type = STRTOUPPER($type);
        echo '<p>' . Text::_('COM_MYCOMPONENT_POSTFLIGHT_' . $type . '_TEXT') . '</p>';

        // setup data for first time install
        if ($type == "INSTALL") {
            
            // Load categories
            $cattitles = array(
                    "2020"
                    );
            $cat_id = $this->createCategory($cattitles);
 
        }
    }

    /**
    * Function to create category records
    * @param array category titles
    * @param string category group or type
    * @return void
    */
    public function createCategory($cat_titles)
    {
        foreach ($cat_titles as $cat) {
            $category = Table::getInstance('Category');
            $category->extension = $this->extension;
            $category->title = $cat;
            $category->description = '';
            $category->published = 1;
            $category->access = 1;
            $category->params = '{"category_layout":"","image":"","image_alt":""}';
            $category->metadata = '{"page_title":"","author":"","robots":""}';
            $category->language = '*';
            // Set the location in the tree
            $category->setLocation(1, 'last-child');
            // Check to make sure our data is valid
            if (!$category->check()) {
                throw new Exception(500, $category->getError());
                return false;
            }
            // Now store the category
            if (!$category->store(true)) {
                throw new Exception(500, $category->getError());
                return false;
            }
         }
        // Build the path for our category
        $category->rebuildPath($category->id);
        echo '<p>' . Text::_('Categories created') . '</p>';
        return $category->id;
    }


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/joomla-dev-general/4ac21382-9a24-4078-a2ac-25e2606afd97n%40googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages