"Fossies" - the Fresh Open Source Software Archive

Member "fogproject-1.5.9/packages/web/lib/fog/group.class.php" (13 Sep 2020, 30027 Bytes) of package /linux/misc/fogproject-1.5.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "group.class.php": 1.5.8_vs_1.5.9.

    1 <?php
    2 /**
    3  * Main class for group objects.
    4  *
    5  * PHP version 5
    6  *
    7  * @category Group
    8  * @package  FOGProject
    9  * @author   Tom Elliott <tommygunsster@gmail.com>
   10  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
   11  * @link     https://fogproject.org
   12  */
   13 /**
   14  * Main class for group objects.
   15  *
   16  * @category Group
   17  * @package  FOGProject
   18  * @author   Tom Elliott <tommygunsster@gmail.com>
   19  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
   20  * @link     https://fogproject.org
   21  */
   22 class Group extends FOGController
   23 {
   24     /**
   25      * The database table.
   26      *
   27      * @var string
   28      */
   29     protected $databaseTable = 'groups';
   30     /**
   31      * Common to db field associations.
   32      *
   33      * @var array
   34      */
   35     protected $databaseFields = array(
   36         'id' => 'groupID',
   37         'name' => 'groupName',
   38         'description' => 'groupDesc',
   39         'createdBy' => 'groupCreateBy',
   40         'createdTime' => 'groupDateTime',
   41         'building' => 'groupBuilding',
   42         'kernel' => 'groupKernel',
   43         'kernelArgs' => 'groupKernelArgs',
   44         'kernelDevice' => 'groupPrimaryDisk',
   45     );
   46     /**
   47      * Required fields.
   48      *
   49      * @var array
   50      */
   51     protected $databaseFieldsRequired = array(
   52         'name',
   53     );
   54     /**
   55      * Additional fields.
   56      *
   57      * @var array
   58      */
   59     protected $additionalFields = array(
   60         'hosts',
   61         'hostsnotinme',
   62     );
   63     /**
   64      * Destroy the group object and all associations.
   65      *
   66      * @param string $field the field to scan for
   67      *
   68      * @return bool
   69      */
   70     public function destroy($field = 'id')
   71     {
   72         self::getClass('GroupAssociationManager')
   73             ->destroy(
   74                 array(
   75                     'groupID' => $this->get('id'),
   76                 )
   77             );
   78         return parent::destroy($field);
   79     }
   80     /**
   81      * Saves the group elements.
   82      *
   83      * @return object
   84      */
   85     public function save()
   86     {
   87         parent::save();
   88         return $this
   89             ->assocSetter('Group', 'host')
   90             ->load();
   91     }
   92     /**
   93      * Returns the host count.
   94      *
   95      * @return int
   96      */
   97     public function getHostCount()
   98     {
   99         return self::getClass('HostManager')
  100             ->count(
  101                 array(
  102                     'id' => $this->get('hosts'),
  103                 )
  104             );
  105     }
  106     /**
  107      * Add or remove printers from all hosts in the group
  108      * Sets printer management level for all hosts as well.
  109      *
  110      * @param mixed $printerAdd the printers to add
  111      * @param mixed $printerDel the printers to remove
  112      * @param int   $level      the management level to set
  113      *
  114      * @return object
  115      */
  116     public function addPrinter(
  117         $printerAdd,
  118         $printerDel,
  119         $level = 0
  120     ) {
  121         self::getClass('HostManager')->update(
  122             array(
  123                 'id' => $this->get('hosts'),
  124             ),
  125             '',
  126             array(
  127                 'printerLevel' => $level,
  128             )
  129         );
  130         if (count($printerDel) > 0) {
  131             self::getClass('PrinterAssociationManager')
  132                 ->destroy(
  133                     array(
  134                         'hostID' => $this->get('hosts'),
  135                         'printerID' => $printerDel,
  136                     )
  137                 );
  138         }
  139         if (count($printerAdd) > 0) {
  140             $insert_fields = array('hostID', 'printerID');
  141             $insert_values = array();
  142             $hosts = $this->get('hosts');
  143             if (count($hosts) > 0) {
  144                 array_walk(
  145                     $hosts,
  146                     function (
  147                         &$hostID,
  148                         $index
  149                     ) use (
  150                         &$insert_values,
  151                         $printerAdd
  152                     ) {
  153                         foreach ((array) $printerAdd as &$printerID) {
  154                             $insert_values[] = array($hostID, $printerID);
  155                             unset($printerID);
  156                         }
  157                     }
  158                 );
  159             }
  160             if (count($insert_values) > 0) {
  161                 self::getClass('PrinterAssociationManager')
  162                     ->insertBatch(
  163                         $insert_fields,
  164                         $insert_values
  165                     );
  166             }
  167         }
  168 
  169         return $this;
  170     }
  171     /**
  172      * Add Snapins to all hosts in the group.
  173      *
  174      * @param array $addArray the items to add
  175      *
  176      * @return object
  177      */
  178     public function addSnapin($addArray)
  179     {
  180         $insert_fields = array('hostID', 'snapinID');
  181         $insert_values = array();
  182         $hosts = $this->get('hosts');
  183         if (count($hosts) > 0) {
  184             array_walk(
  185                 $hosts,
  186                 function (
  187                     &$hostID,
  188                     $index
  189                 ) use (
  190                     &$insert_values,
  191                     $addArray
  192                 ) {
  193                     foreach ($addArray as $snapinID) {
  194                         $insert_values[] = array($hostID, $snapinID);
  195                     }
  196                 }
  197             );
  198         }
  199         if (count($insert_values) > 0) {
  200             self::getClass('SnapinAssociationManager')
  201                 ->insertBatch(
  202                     $insert_fields,
  203                     $insert_values
  204                 );
  205         }
  206 
  207         return $this;
  208     }
  209     /**
  210      * Remove snapin from all hosts in group.
  211      *
  212      * @param array $removeArray the items to remove
  213      *
  214      * @return object
  215      */
  216     public function removeSnapin($removeArray)
  217     {
  218         self::getClass('SnapinAssociationManager')
  219             ->destroy(
  220                 array(
  221                     'hostID' => $this->get('hosts'),
  222                     'snapinID' => $removeArray,
  223                 )
  224             );
  225 
  226         return $this;
  227     }
  228     /**
  229      * Add modules to all hosts in group.
  230      *
  231      * @param array $addArray the items to add
  232      *
  233      * @return object
  234      */
  235     public function addModule($addArray)
  236     {
  237         $insert_fields = array('hostID', 'moduleID', 'state');
  238         $insert_values = array();
  239         $hostids = $this->get('hosts');
  240         foreach ((array) $hostids as &$hostid) {
  241             foreach ((array) $addArray as &$moduleid) {
  242                 $insert_values[] = array($hostid, $moduleid, 1);
  243                 unset($moduleid);
  244             }
  245             unset($hostid);
  246         }
  247         if (count($insert_values) > 0) {
  248             self::getClass('ModuleAssociationManager')
  249                 ->insertBatch(
  250                     $insert_fields,
  251                     $insert_values
  252                 );
  253             unset($insert_value);
  254         }
  255 
  256         return $this;
  257     }
  258     /**
  259      * Remove modules from hosts in group.
  260      *
  261      * @param array $removeArray The items to remove
  262      *
  263      * @return object
  264      */
  265     public function removeModule($removeArray)
  266     {
  267         self::getClass('ModuleAssociationManager')
  268             ->destroy(
  269                 array(
  270                     'hostID' => $this->get('hosts'),
  271                     'moduleID' => $removeArray,
  272                 )
  273             );
  274 
  275         return $this;
  276     }
  277     /**
  278      * Set's the display for all hosts in group.
  279      *
  280      * @param mixed $x the width to set
  281      * @param mixed $y the height to set
  282      * @param mixed $r the refresh rate to set
  283      *
  284      * @return object
  285      */
  286     public function setDisp(
  287         $x,
  288         $y,
  289         $r
  290     ) {
  291         self::getClass('HostScreenSettingManager')
  292             ->destroy(
  293                 array(
  294                     'hostID' => $this->get('hosts'),
  295                 )
  296             );
  297         $insert_fields = array(
  298             'hostID',
  299             'width',
  300             'height',
  301             'refresh',
  302         );
  303         $insert_items = array();
  304         foreach ((array) $this->get('hosts') as &$hostID) {
  305             $insert_items[] = array($hostID, $x, $y, $r);
  306             unset($hostID);
  307         }
  308         self::getClass('HostScreenSettingManager')
  309             ->insertBatch(
  310                 $insert_fields,
  311                 $insert_items
  312             );
  313 
  314         return $this;
  315     }
  316     /**
  317      * Set's the auto logout time for all hosts.
  318      *
  319      * @param mixed $time the time to set to
  320      *
  321      * @return object
  322      */
  323     public function setAlo($time)
  324     {
  325         self::getClass('HostAutoLogoutManager')
  326             ->destroy(
  327                 array(
  328                     'hostID' => $this->get('hosts'),
  329                 )
  330             );
  331         $insert_fields = array(
  332             'hostID',
  333             'time',
  334         );
  335         $insert_items = array();
  336         foreach ((array) $this->get('hosts') as &$hostID) {
  337             $insert_items[] = array(
  338                 $hostID,
  339                 $time,
  340             );
  341             unset($hostID);
  342         }
  343         self::getClass('HostAutoLogoutManager')
  344             ->insertBatch(
  345                 $insert_fields,
  346                 $insert_items
  347             );
  348 
  349         return $this;
  350     }
  351     /**
  352      * Add host to the group.
  353      *
  354      * @param array $addArray the host to add
  355      *
  356      * @return object
  357      */
  358     public function addHost($addArray)
  359     {
  360         return $this->addRemItem(
  361             'hosts',
  362             (array)$addArray,
  363             'merge'
  364         );
  365     }
  366     /**
  367      * Remove host from the group.
  368      *
  369      * @param array $removeArray the host to remove
  370      *
  371      * @return object
  372      */
  373     public function removeHost($removeArray)
  374     {
  375         return $this->addRemItem(
  376             'hosts',
  377             (array)$removeArray,
  378             'diff'
  379         );
  380     }
  381     /**
  382      * Add image to all hosts.
  383      *
  384      * @param int $imageID the image id to associate
  385      *
  386      * @throws Exception
  387      *
  388      * @return object
  389      */
  390     public function addImage($imageID)
  391     {
  392         $Image = new Image($imageID);
  393         if (!$Image->isValid() && is_numeric($imageID)) {
  394             throw new Exception(_('Select a valid image'));
  395         }
  396         $states = self::fastmerge(
  397             self::getQueuedStates(),
  398             (array)self::getProgressState()
  399         );
  400         $TaskCount = self::getClass('TaskManager')
  401             ->count(
  402                 array(
  403                     'hostID' => $this->get('hosts'),
  404                     'stateID' => $states,
  405                 )
  406             );
  407         if ($TaskCount > 0) {
  408             throw new Exception(_('There is a host in a tasking'));
  409         }
  410         self::getClass('HostManager')
  411             ->update(
  412                 array(
  413                     'id' => $this->get('hosts'),
  414                 ),
  415                 '',
  416                 array('imageID' => $imageID)
  417             );
  418 
  419         return $this;
  420     }
  421     /**
  422      * Creates image packages for all hosts associated.
  423      *
  424      * @param int    $taskTypeID    the task type id
  425      * @param string $taskName      the name of the tasking
  426      * @param bool   $shutdown      whether to shutdown the hosts
  427      * @param bool   $debug         is tasking debug
  428      * @param mixed  $deploySnapins All, false, or specified snapin
  429      * @param bool   $isGroupTask   will always be true here
  430      * @param string $username      username creating the task
  431      * @param string $passreset     which account to reset if pass reset
  432      * @param mixed  $sessionjoin   the multicast session to join
  433      * @param bool   $wol           whether to wake on lan or not
  434      *
  435      * @return array
  436      */
  437     public function createImagePackage(
  438         $taskTypeID,
  439         $taskName = '',
  440         $shutdown = false,
  441         $debug = false,
  442         $deploySnapins = false,
  443         $isGroupTask = true,
  444         $username = '',
  445         $passreset = '',
  446         $sessionjoin = false,
  447         $wol = false
  448     ) {
  449         $taskName .= ' - ' . $this->get('name');
  450         $hostCount = $this->getHostCount();
  451         if ($hostCount < 1) {
  452             throw new Exception(_('No hosts to task'));
  453         }
  454         $hostids = $this->get('hosts');
  455         $TaskType = new TaskType($taskTypeID);
  456         if (!$TaskType->isValid()) {
  457             throw new Exception(self::$foglang['TaskTypeNotValid']);
  458         }
  459         $hostids = array_diff(
  460             $hostids,
  461             self::getSubObjectIDs(
  462                 'Task',
  463                 array(
  464                     'hostID' => $hostids,
  465                     'stateID' => self::fastmerge(
  466                         self::getQueuedStates(),
  467                         (array)self::getProgressState()
  468                     ),
  469                     'typeID' => $TaskType->isInitNeededTasking(true)
  470                 ),
  471                 'hostID'
  472             )
  473         );
  474         if (count($hostids) < 1) {
  475             throw new Exception(_('No hosts available to task'));
  476         }
  477         $imagingTypes = $TaskType->isImagingTask();
  478         $now = $this->niceDate();
  479         if ($imagingTypes) {
  480             $imageID = @min(
  481                 self::getSubObjectIDs(
  482                     'Host',
  483                     array(
  484                         'id' => $hostids,
  485                     ),
  486                     'imageID'
  487                 )
  488             );
  489             $Image = new Image($imageID);
  490             if (!$Image->isValid()) {
  491                 throw new Exception(self::$foglang['ImageNotValid']);
  492             }
  493             if (!$Image->get('isEnabled')) {
  494                 throw new Exception(_('Image is not enabled'));
  495             }
  496             $StorageGroup = $Image->getStorageGroup();
  497             if (!$StorageGroup->isValid()) {
  498                 throw new Exception(self::$foglang['ImageGroupNotValid']);
  499             }
  500             $StorageNode = $StorageGroup->getMasterStorageNode();
  501             if (!$StorageNode->isValid()) {
  502                 throw new Exception(_('Unable to find master Storage Node'));
  503             }
  504             if ($TaskType->isMulticast()) {
  505                 list(
  506                     $portOverride,
  507                     $defaultPort
  508                 ) = self::getSubObjectIDs(
  509                     'Service',
  510                     array(
  511                         'name' => array(
  512                             'FOG_MULTICAST_PORT_OVERRIDE',
  513                             'FOG_UDPCAST_STARTINGPORT',
  514                         ),
  515                     ),
  516                     'value',
  517                     false,
  518                     'AND',
  519                     'name',
  520                     false,
  521                     ''
  522                 );
  523                 if ($portOverride) {
  524                     $port = $portOverride;
  525                 } else {
  526                     $port = $defaultPort;
  527                 }
  528                 $MulticastSession = self::getClass('MulticastSession')
  529                     ->set('name', $taskName)
  530                     ->set('port', $port)
  531                     ->set('logpath', $Image->get('path'))
  532                     ->set('image', $Image->get('id'))
  533                     ->set('interface', $StorageNode->get('interface'))
  534                     ->set('stateID', 0)
  535                     ->set('starttime', $now->format('Y-m-d H:i:s'))
  536                     ->set('percent', 0)
  537                     ->set('isDD', $Image->get('imageTypeID'))
  538                     ->set('storagegroupID', $StorageGroup->get('id'));
  539                 if ($MulticastSession->save()) {
  540                     self::getClass('MulticastSessionAssociationManager')
  541                         ->destroy(
  542                             array(
  543                                 'hostID' => $hostids,
  544                             )
  545                         );
  546                     $randomnumber = mt_rand(24576, 32766) * 2;
  547                     while ($randomnumber == $MulticastSession->get('port')) {
  548                         $randomnumber = mt_rand(24576, 32766) * 2;
  549                     }
  550                     self::setSetting('FOG_UDPCAST_STARTINGPORT', $randomnumber);
  551                 }
  552                 $hostIDs = $hostids;
  553                 $batchFields = array(
  554                     'name',
  555                     'createdBy',
  556                     'hostID',
  557                     'isForced',
  558                     'stateID',
  559                     'typeID',
  560                     'storagenodeID',
  561                     'wol',
  562                     'imageID',
  563                     'shutdown',
  564                     'isDebug',
  565                     'passreset',
  566                 );
  567                 $batchTask = array();
  568                 for ($i = 0; $i < $hostCount; ++$i) {
  569                     $batchTask[] = array(
  570                         $taskName,
  571                         $username,
  572                         $hostIDs[$i],
  573                         0,
  574                         self::getQueuedState(),
  575                         $TaskType->get('id'),
  576                         $StorageNode->get('id'),
  577                         $wol,
  578                         $Image->get('id'),
  579                         $shutdown,
  580                         $debug,
  581                         $passreset,
  582                     );
  583                 }
  584                 if (count($batchTask) > 0) {
  585                     list(
  586                         $first_id,
  587                         $affected_rows
  588                     ) = self::getClass('TaskManager')
  589                     ->insertBatch(
  590                         $batchFields,
  591                         $batchTask
  592                     );
  593                     $ids = range($first_id, ($first_id + $affected_rows - 1));
  594                     $multicastsessionassocs = array();
  595                     foreach ((array) $batchTask as $index => &$val) {
  596                         $multicastsessionassocs[] = array(
  597                             $MulticastSession->get('id'),
  598                             $ids[$index],
  599                         );
  600                         unset($val);
  601                     }
  602                     if (count($multicastsessionassocs) > 0) {
  603                         self::getClass('MulticastSessionAssociationManager')
  604                             ->insertBatch(
  605                                 array(
  606                                     'msID',
  607                                     'taskID',
  608                                 ),
  609                                 $multicastsessionassocs
  610                             );
  611                     }
  612                 }
  613                 unset(
  614                     $hostCount,
  615                     $batchTask,
  616                     $first_id,
  617                     $affected_rows,
  618                     $ids,
  619                     $multicastsessionassocs
  620                 );
  621                 $this->_createSnapinTasking($now, -1);
  622             } elseif ($TaskType->isDeploy()) {
  623                 $hostIDs = $hostids;
  624                 $imageIDs = self::getSubObjectIDs(
  625                     'Host',
  626                     array(
  627                         'id' => $hostIDs,
  628                     ),
  629                     'imageID',
  630                     false,
  631                     'AND',
  632                     'name',
  633                     false,
  634                     ''
  635                 );
  636                 if (!is_array($imageIDs)) {
  637                     $imageIDs = array($imageIDs);
  638                 }
  639                 $batchFields = array(
  640                     'name',
  641                     'createdBy',
  642                     'hostID',
  643                     'isForced',
  644                     'stateID',
  645                     'typeID',
  646                     'storagenodeID',
  647                     'wol',
  648                     'imageID',
  649                     'shutdown',
  650                     'isDebug',
  651                     'passreset',
  652                 );
  653                 $batchTask = array();
  654                 for ($i = 0; $i < $hostCount; ++$i) {
  655                     $batchTask[] = array(
  656                         $taskName,
  657                         $username,
  658                         $hostIDs[$i],
  659                         0,
  660                         self::getQueuedState(),
  661                         $TaskType->get('id'),
  662                         $StorageNode->get('id'),
  663                         $wol,
  664                         $imageIDs[$i],
  665                         $shutdown,
  666                         $debug,
  667                         $passreset,
  668                     );
  669                 }
  670                 if (count($batchTask) > 0) {
  671                     self::getClass('TaskManager')
  672                         ->insertBatch(
  673                             $batchFields,
  674                             $batchTask
  675                         );
  676                 }
  677                 unset(
  678                     $hostCount,
  679                     $batchTask,
  680                     $first_id,
  681                     $affected_rows,
  682                     $ids,
  683                     $multicastsessionassocs
  684                 );
  685                 if ($TaskType->isSnapinTask()) {
  686                     $this->_createSnapinTasking($now, $deploySnapins);
  687                 }
  688             }
  689         } elseif ($TaskType->isSnapinTasking()) {
  690             $hostIDs = $this->_createSnapinTasking($now, $deploySnapins);
  691             $hostCount = count($hostIDs);
  692             $batchFields = array(
  693                 'name',
  694                 'createdBy',
  695                 'hostID',
  696                 'stateID',
  697                 'typeID',
  698                 'wol',
  699             );
  700             $batchTask = array();
  701             for ($i = 0; $i < $hostCount; ++$i) {
  702                 $batchTask[] = array(
  703                     $taskName,
  704                     $username,
  705                     $hostIDs[$i],
  706                     self::getQueuedState(),
  707                     $TaskType->get('id'),
  708                     $wol,
  709                 );
  710             }
  711             if (count($batchTask) > 0) {
  712                 self::getClass('TaskManager')
  713                     ->insertBatch($batchFields, $batchTask);
  714             }
  715         } else {
  716             if ($TaskType->get('id') != 14) {
  717                 $hostIDs = $this->get('hosts');
  718                 $hostCount = count($hostIDs);
  719                 $batchFields = array(
  720                     'name',
  721                     'createdBy',
  722                     'hostID',
  723                     'stateID',
  724                     'typeID',
  725                     'wol',
  726                     'shutdown',
  727                 );
  728                 $batchTask = array();
  729                 for ($i = 0; $i < $hostCount; ++$i) {
  730                     $batchTask[] = array(
  731                         $taskName,
  732                         $username,
  733                         $hostIDs[$i],
  734                         self::getQueuedState(),
  735                         $TaskType->get('id'),
  736                         $wol,
  737                         $shutdown,
  738                     );
  739                 }
  740                 if (count($batchTask) > 0) {
  741                     self::getClass('TaskManager')
  742                         ->insertBatch($batchFields, $batchTask);
  743                 }
  744             }
  745         }
  746         if ($wol) {
  747             session_write_close();
  748             ignore_user_abort(true);
  749             set_time_limit(0);
  750             $this->wakeOnLAN();
  751         }
  752         Route::listem(
  753             'host',
  754             'name',
  755             false,
  756             array('id' => $hostIDs)
  757         );
  758         $Hosts = json_decode(
  759             Route::getData()
  760         );
  761         $Hosts = $Hosts->hosts;
  762         $str = '';
  763         foreach ((array)$Hosts as &$Host) {
  764             $str .= '<li>';
  765             $str .= '<a href="?node=host&sub=edit&id='
  766                 . $Host->id
  767                 . '">';
  768             $str .= $Host->name;
  769             $str .= ' &ndash; ';
  770             $str .= $Host->imagename;
  771             $str .= '</a>';
  772             $str .= '</li>';
  773             unset($Host);
  774         }
  775         unset($Hosts);
  776         return $str;
  777     }
  778     /**
  779      * Perform wake on lan to all hosts in group.
  780      *
  781      * @return void
  782      */
  783     public function wakeOnLAN()
  784     {
  785         $hostMACs = self::getSubObjectIDs(
  786             'MACAddressAssociation',
  787             array(
  788                 'hostID' => $this->get('hosts'),
  789                 'pending' => array(0, '')
  790             ),
  791             'mac'
  792         );
  793         $hostMACs = self::parseMacList($hostMACs);
  794         if (count($hostMACs) > 0) {
  795             $macStr = implode(
  796                 '|',
  797                 $hostMACs
  798             );
  799             self::wakeUp($hostMACs);
  800         }
  801     }
  802     /**
  803      * Create snapin tasks for hosts.
  804      *
  805      * @param mixed $now    the current time
  806      * @param int   $snapin the snapin to task (all is -1)
  807      *
  808      * @return array
  809      */
  810     private function _createSnapinTasking($now, $snapin = -1)
  811     {
  812         if ($snapin === false) {
  813             return;
  814         }
  815         $hostIDs = $this->get('hosts');
  816         $hostCount = count($hostIDs);
  817         $snapinJobs = array();
  818         for ($i = 0; $i < $hostCount; ++$i) {
  819             $hostID = $hostIDs[$i];
  820             $snapins[$hostID] = (
  821                 $snapin == -1 ?
  822                 self::getSubObjectIDs(
  823                     'SnapinAssociation',
  824                     array(
  825                         'hostID' => $hostID,
  826                     ),
  827                     'snapinID'
  828                 ) :
  829                 array($snapin)
  830             );
  831             if (count($snapins[$hostID]) < 1) {
  832                 continue;
  833             }
  834             $snapinJobs[] = array(
  835                 $hostID,
  836                 self::getQueuedState(),
  837                 $now->format('Y-m-d H:i:s'),
  838             );
  839         }
  840         if (count($snapinJobs) > 0) {
  841             list(
  842                 $first_id,
  843                 $affected_rows
  844             ) = self::getClass('SnapinJobManager')
  845             ->insertBatch(
  846                 array(
  847                     'hostID',
  848                     'stateID',
  849                     'createdTime',
  850                 ),
  851                 $snapinJobs
  852             );
  853             $ids = range($first_id, ($first_id + $affected_rows - 1));
  854             for ($i = 0; $i < $hostCount; ++$i) {
  855                 $hostID = $hostIDs[$i];
  856                 $jobID = $ids[$i];
  857                 $snapinCount = count($snapins[$hostID]);
  858                 for ($j = 0; $j < $snapinCount; ++$j) {
  859                     $snapinTasks[] = array(
  860                         $jobID,
  861                         self::getQueuedState(),
  862                         $snapins[$hostID][$j],
  863                     );
  864                 }
  865             }
  866             if (count($snapinTasks) > 0) {
  867                 self::getClass('SnapinTaskManager')
  868                     ->insertBatch(
  869                         array(
  870                             'jobID',
  871                             'stateID',
  872                             'snapinID',
  873                         ),
  874                         $snapinTasks
  875                     );
  876             }
  877         }
  878 
  879         return $hostIDs;
  880     }
  881     /**
  882      * Sets all hosts AD information.
  883      *
  884      * @param int    $useAD   tells whether to enable/disable AD
  885      * @param string $domain  the domain to associate
  886      * @param string $ou      the ou to associate
  887      * @param string $user    the user to join domain with
  888      * @param string $pass    the user password for domain join
  889      * @param string $legacy  the legacy password for legacy client
  890      * @param int    $enforce sets whether to enforce changes
  891      *
  892      * @return object
  893      */
  894     public function setAD(
  895         $useAD,
  896         $domain,
  897         $ou,
  898         $user,
  899         $pass,
  900         $legacy,
  901         $enforce
  902     ) {
  903         $pass = trim($pass);
  904         $adpasspat = "/^\*{32}$/";
  905         $pass = (preg_match($adpasspat, $pass) ? $this->get('ADPass') : $pass);
  906         self::getClass('HostManager')
  907             ->update(
  908                 array(
  909                     'id' => $this->get('hosts'),
  910                 ),
  911                 '',
  912                 array(
  913                     'useAD' => $useAD,
  914                     'ADDomain' => trim($domain),
  915                     'ADOU' => trim($ou),
  916                     'ADUser' => trim($user),
  917                     'ADPass' => $pass,
  918                     'ADPassLegacy' => $legacy,
  919                     'enforce' => $enforce,
  920                 )
  921             );
  922 
  923         return $this;
  924     }
  925     /**
  926      * Checks all hosts have the same image associated.
  927      *
  928      * @return bool
  929      */
  930     public function doMembersHaveUniformImages()
  931     {
  932         $test = self::getClass('HostManager')
  933             ->distinct(
  934                 'imageID',
  935                 array('id' => $this->get('hosts'))
  936             );
  937 
  938         return $test == 1;
  939     }
  940     /**
  941      * Updates all host's default printers.
  942      *
  943      * @param int $printerid the printer id to set as default
  944      *
  945      * @return object
  946      */
  947     public function updateDefault($printerid)
  948     {
  949         $AllGroupHostsPrinters = self::getSubObjectIDs(
  950             'PrinterAssociation',
  951             array('hostID' => $this->get('hosts'))
  952         );
  953         self::getClass('PrinterAssociationManager')
  954             ->update(
  955                 array('id' => $AllGroupHostsPrinters),
  956                 '',
  957                 array('isDefault' => '0')
  958             );
  959         self::getClass('PrinterAssociationManager')
  960             ->update(
  961                 array(
  962                     'printerID' => $printerid,
  963                     'hostID' => $this->get('hosts'),
  964                 ),
  965                 '',
  966                 array('isDefault' => 1)
  967             );
  968 
  969         return $this;
  970     }
  971     /**
  972      * Loads hosts in this group.
  973      *
  974      * @return void
  975      */
  976     protected function loadHosts()
  977     {
  978         $groupid = $this->get('id');
  979         if ($groupid > 0) {
  980             $this->set(
  981                 'hosts',
  982                 (array)self::getSubObjectIDs(
  983                     'GroupAssociation',
  984                     array('groupID' => $groupid),
  985                     'hostID'
  986                 )
  987             );
  988             $this->getHostCount();
  989         }
  990     }
  991     /**
  992      * Loads hosts not in this group.
  993      *
  994      * @return void
  995      */
  996     protected function loadHostsnotinme()
  997     {
  998         $hosts = array_diff(
  999             self::getSubObjectIDs('Host'),
 1000             $this->get('hosts')
 1001         );
 1002         $this->set('hostsnotinme', (array)$hosts);
 1003     }
 1004 }