~leeturner/quam-plures/installer_base_config

« back to all changes in this revision

Viewing changes to qp_inc/plugins/model/_plugins_admin.class.php

Give smart names and codes to additional instances of a plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
286
286
 
287
287
 
288
288
        /**
289
 
         * Count # of registrations of same plugin.
 
289
         * Count # of registrations of same plugin. Optionally returns the sequence
 
290
         * number to be used for the next instance of a plugin as well.
290
291
         *
291
292
         * Plugins with negative ID (auto-generated; not installed (yet)) will not get considered.
292
293
         *
293
294
         * @param string class name
294
 
         * @return int # of regs
 
295
         * @param boolean Return next sequence number as well (will be 1 if there are no other
 
296
         *                instances of this plugin).
 
297
         * @return int|array # of regs or array( $regs, $next_seq_number ) if $get_seq_number is true
295
298
         */
296
 
        function count_regs( $classname )
 
299
        function count_regs( $classname, $get_seq_number = false )
297
300
        {
298
301
                $count = 0;
 
302
                $highest_seq_number = 0;
299
303
 
300
304
                foreach( $this->sorted_IDs as $plugin_ID )
301
305
                {
302
 
                        $Plugin = & $this->get_by_ID( $plugin_ID );
 
306
                        $Plugin = $this->get_by_ID( $plugin_ID );
303
307
                        if( $Plugin && $Plugin->classname == $classname && $Plugin->ID > 0 )
304
308
                        {
305
309
                                $count++;
 
310
                                
 
311
                                // We assume that nobody has tinkered with the plugin codes in the DB here!
 
312
                                if( $get_seq_number && $Plugin->code !== NULL &&
 
313
                                        preg_match( '/_(\d+)$/', $Plugin->code, $m ) &&
 
314
                                        $m[1] > $highest_seq_number )
 
315
                                {
 
316
                                        $highest_seq_number = $m[1];
 
317
                                }
306
318
                        }
307
319
                }
 
320
                
 
321
                if( $get_seq_number )
 
322
                {
 
323
                        return array( $count, ( $highest_seq_number ? $highest_seq_number : $count ) + 1 );
 
324
                }
 
325
                
308
326
                return $count;
309
327
        }
310
328
 
491
509
                $this->load_plugins_table();
492
510
 
493
511
                // Register the plugin:
494
 
                $Plugin = & $this->register( $classname, 0, -1, NULL, $classfile_path ); // Auto-generates negative ID; New ID will be set a few lines below
 
512
                // Auto-generates negative ID; New ID will be set a few lines below
 
513
                $Plugin = $this->register( $classname, 0, -1, NULL, $classfile_path, 
 
514
                                                true, $orig_code );
495
515
 
496
516
                if( is_string($Plugin) )
497
517
                { // return error message from register()
498
518
                        return $Plugin;
499
519
                }
500
520
 
 
521
                list( $regs, $seq_number ) = $this->count_regs( $Plugin->classname, true );
501
522
                if( isset($Plugin->number_of_installs)
502
 
                    && ( $this->count_regs( $Plugin->classname ) >= $Plugin->number_of_installs ) )
 
523
                    && ( $regs >= $Plugin->number_of_installs ) )
503
524
                {
504
525
                        $this->unregister( $Plugin, true );
505
526
                        $r = T_('The plugin cannot be installed again.');
540
561
                        $Plugin->code = NULL;
541
562
                }
542
563
 
 
564
                // Do we have multiple instances of this plugin installed? If yes, we
 
565
                // want a sequential name and code to make the admin's life a bit easier.
 
566
                if( $seq_number > 1 )
 
567
                {
 
568
                        if( $Plugin->code === NULL )
 
569
                        {       // We need a code.
 
570
                                $Plugin->code = ! empty( $orig_code ) ? $orig_code : $classname;
 
571
                        }
 
572
                        
 
573
                        $instance_name = $Plugin->name.' #'.$seq_number;
 
574
                        $Plugin->name  = $instance_name;
 
575
                        $Plugin->code .= '_'.$seq_number;
 
576
                }
 
577
 
543
578
                $Plugin->status = $plug_status;
544
579
 
545
580
                // Record into DB
546
581
                $DB->begin();
547
582
 
548
583
                $DB->query( '
549
 
                                INSERT INTO T_plugins( plug_classname, plug_priority, plug_code, plug_apply_rendering, plug_version, plug_status )
550
 
                                VALUES( "'.$classname.'", '.$Plugin->priority.', '.$DB->quote($Plugin->code).', '.$DB->quote($Plugin->apply_rendering).', '.$DB->quote($Plugin->version).', '.$DB->quote($Plugin->status).' ) ' );
 
584
                                INSERT INTO T_plugins( plug_classname, plug_priority, plug_code, plug_apply_rendering, plug_version, plug_name, plug_status )
 
585
                                VALUES( "'.$classname.'", '.$Plugin->priority.', '.$DB->quote($Plugin->code).', '.$DB->quote($Plugin->apply_rendering).', '.$DB->quote($Plugin->version).', '.$DB->quote($Plugin->name).', '.$DB->quote($Plugin->status).' ) ' );
551
586
 
552
587
                // Unset auto-generated ID info
553
588
                unset( $this->index_ID_Plugins[ $Plugin->ID ] );
562
597
                                'plug_classname' => $Plugin->classname,
563
598
                                'plug_code' => $Plugin->code,
564
599
                                'plug_apply_rendering' => $Plugin->apply_rendering,
 
600
                                'plug_version' => $Plugin->version,
 
601
                                'plug_name' => $Plugin->name,
565
602
                                'plug_status' => $Plugin->status,
566
 
                                'plug_version' => $Plugin->version,
567
603
                        );
568
604
                $this->sorted_IDs[$key] = $Plugin->ID;
569
605
 
589
625
                        $Debuglog->add( 'Unregistered plugin, because PluginInit returned false.', 'plugins' );
590
626
                        $Plugin = '';
591
627
                }
 
628
                else if( isset( $instance_name ) )
 
629
                {       // $Plugin->PluginInit( $tmp_params ) lost our new name :(
 
630
                        $Plugin->name = $instance_name;
 
631
                }
592
632
 
593
633
                if( ! defined('QP_IS_INSTALLING') || ! QP_IS_INSTALLING )
594
634
                { // do not sort, if we're installing/upgrading.. instantiating Plugins might cause a fatal error!