1<?php
2
3/**
4 *
5 * bareos-webui - Bareos Web-Frontend
6 *
7 * @link      https://github.com/bareos/bareos-webui for the canonical source repository
8 * @copyright Copyright (c) 2013-2017 Bareos GmbH & Co. KG (http://www.bareos.org/)
9 * @license   GNU Affero General Public License (http://www.gnu.org/licenses/)
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26namespace Media\Model;
27
28class MediaModel
29{
30
31   /**
32    * Get all Volumes/Media
33    *
34    * @param $bsock
35    *
36    * @return array
37    */
38   public function getVolumes(&$bsock=null)
39   {
40      if(isset($bsock)) {
41         $cmd = 'llist volumes all';
42         $limit = 1000;
43         $offset = 0;
44         $retval = array();
45         while (true) {
46            $result = $bsock->send_command($cmd . ' limit=' . $limit . ' offset=' . $offset, 2, null);
47            if (preg_match('/Failed to send result as json. Maybe result message to long?/', $result)) {
48               $error = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
49               return $error['result']['error'];
50            } else {
51               $volumes = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
52               if ( empty($volumes['result']) ) {
53                  return false; // No matching records found
54               }
55               if ( empty($volumes['result']['volumes']) && $volumes['result']['meta']['range']['filtered'] === 0 ) {
56                  return $retval;
57               } else {
58                  $retval = array_merge($retval, $volumes['result']['volumes']);
59               }
60            }
61            $offset = $offset + $limit;
62         }
63      } else {
64         throw new \Exception('Missing argument.');
65      }
66   }
67
68   /**
69    * Get a single Volume
70    *
71    * @param $bsock
72    * @param $volume
73    *
74    * @return array
75    */
76   public function getVolume(&$bsock=null, $volume=null)
77   {
78      if(isset($bsock, $volume)) {
79         $cmd = 'llist volume="'.$volume.'"';
80         $result = $bsock->send_command($cmd, 2, null);
81         $volume = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
82         return $volume['result']['volume'];
83      }
84      else {
85         throw new \Exception('Missing argument.');
86      }
87   }
88
89   /**
90    * Get Volume Jobs
91    *
92    * @param $bsock
93    * @param $volume
94    *
95    * @return array
96    */
97   public function getVolumeJobs(&$bsock=null, $volume=null)
98   {
99      if(isset($bsock, $volume)) {
100         $cmd = 'llist jobs volume="'.$volume.'"';
101         $result = $bsock->send_command($cmd, 2, null);
102         if(preg_match('/Failed to send result as json. Maybe result message to long?/', $result)) {
103            $error = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
104            return $error['result']['error'];
105         }
106         else {
107            $volume = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
108            return $volume['result']['jobs'];
109         }
110      }
111      else {
112         throw new \Exception('Missing argument.');
113      }
114   }
115
116}
117