source: trunk/server/www/app/models/worker.php @ 234

Last change on this file since 234 was 234, checked in by sander, 12 years ago

Merge the design branch to the trunk

File size: 5.1 KB
Line 
1<?php
2/**
3 * Officeshots.org - Test your office documents in different applications
4 * Copyright (C) 2009 Stichting Lone Wolves
5 * Written by Sander Marechal <s.marechal@jejik.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21/**
22 * The Application model
23 *
24 * An Application is a specific instance of an office suite application running
25 * on a specific factory.
26 */
27class Worker extends AppModel
28{
29        /** @var string Every application runs on a factory and belongs to an application type */
30        public $belongsTo = array('Factory', 'Application');
31
32        /** @var array Every application supports one or more output formats */
33        public $hasAndBelongsToMany = array(
34                'Format' => array('unique' => true)
35        );
36
37        /** @var array Job queries can be quite complex, so use Containable */
38        public $actsAs = array('Containable');
39
40        /**
41         * Set validation rules in here so we can have i18n messages
42         */
43        public function beforeValidate()
44        {
45                $this->validate = array(
46                        'version' => array(
47                                'rule' => array('custom', '/^[a-z0-9(). -]{1,}$/i'),
48                                'message' => __('Only letters, numbers, dashes, parenthesis, the dot (.) and spaces allowed.', true)
49                        )
50                );
51
52                return True;
53        }
54
55        /**
56         * Get a list of all active worker types. That is, unique combinations of Platform, Doctype and Application
57         *
58         * @param integer $time Maximum time since the last poll, in minutes
59         * @return array
60         */
61        public function getActive()
62        {
63                App::import('Sanitize');
64
65                $time = date('Y-m-d H:i:s', time() - Configure::read('Factory.polltime'));
66                $active = $this->query("SELECT DISTINCT
67                                `Worker`.`version`,
68                                `Worker`.`development`,
69                                `Platform`.`id`,
70                                `Platform`.`name`,
71                                `Doctype`.`id`,
72                                `Doctype`.`name`,
73                                `Doctype`.`code`,
74                                `Application`.`id`,
75                                `Application`.`name`
76                        FROM `workers` AS `Worker`
77                        LEFT JOIN `factories` AS `Factory` ON (`Worker`.`factory_id` = `Factory`.`id`)
78                        LEFT JOIN `operatingsystems` AS `Operatingsystem` ON (`Factory`.`operatingsystem_id` = `Operatingsystem`.`id`)
79                        LEFT JOIN `platforms` AS `Platform` ON (`Operatingsystem`.`platform_id` = `Platform`.`id`)
80                        LEFT JOIN `applications` AS `Application` ON (`Worker`.`application_id` = `Application`.`id`)
81                        LEFT JOIN `applications_doctypes` AS `ApplicationsDoctype` ON `Application`.`id` = `ApplicationsDoctype`.`application_id`
82                        LEFT JOIN `doctypes` AS `Doctype` ON (`ApplicationsDoctype`.`doctype_id` = `Doctype`.`id`)
83                        WHERE `Factory`.`last_poll` > '$time'
84                        ORDER BY `Platform`.`name` ASC, `Application`.`name` ASC, `Worker`.`version` ASC");
85
86                if (is_array($active)) {
87                        foreach ($active as &$app) {
88                                $app['id'] = $app['Platform']['id'] . '_' . $app['Doctype']['code'] . '_' . $app['Application']['id'] . '_'
89                                           . $app['Worker']['version'];
90
91                                $formats = $this->query("SELECT DISTINCT
92                                                `Format`.`id`,
93                                                `Format`.`code`
94                                        FROM `workers` AS `Worker`
95                                        LEFT JOIN `formats_workers` AS `FormatsWorker` ON (`Worker`.`id` = `FormatsWorker`.`worker_id`)
96                                        LEFT JOIN `formats` AS `Format` ON (`FormatsWorker`.`format_id` = `Format`.`id`)
97                                        LEFT JOIN `factories` AS `Factory` ON (`Worker`.`factory_id` = `Factory`.`id`)
98                                        LEFT JOIN `operatingsystems` AS `Operatingsystem` ON (`Factory`.`operatingsystem_id` = `Operatingsystem`.`id`)
99                                        LEFT JOIN `applications` AS `Application` ON (`Worker`.`application_id` = `Application`.`id`)
100                                        LEFT JOIN `applications_doctypes` AS `ApplicationsDoctype` ON `Application`.`id` = `ApplicationsDoctype`.`application_id`
101                                        WHERE `Factory`.`last_poll` > '$time'
102                                                AND `Worker`.`version` = '" . Sanitize::escape($app['Worker']['version']) . "'
103                                                AND `Application`.`id` = '" . $app['Application']['id'] . "'
104                                                AND `Operatingsystem`.`platform_id` = '" . $app['Platform']['id'] . "'
105                                                AND `ApplicationsDoctype`.`doctype_id` = '" . $app['Doctype']['id'] . "'");
106
107                                $app['Format'] = array();
108                                foreach ($formats as $format) {
109                                        $app['Format'][] = $format['Format'];
110                                }
111                        }
112                        return $active;
113                }
114                return array();
115        }
116
117        /**
118         * Get a list of all worker application names and versions, grouped by ODF doctype and platform
119         *
120         * @param integer $time Maximum time since the last poll, in minutes
121         * @return array A nested array
122         */
123        public function getActiveGrouped($time = 10)
124        {
125                $result = array();
126                $doctypes = $this->Application->Doctype->find('all');
127                $platforms = $this->Factory->Operatingsystem->Platform->find('all');
128
129                foreach ($doctypes as $doctype) {
130                        foreach ($platforms as $platform) {
131                                $result[$doctype['Doctype']['name']][$platform['Platform']['name']] = array();
132                        }
133                }
134
135                $active = $this->getActive();
136                return $active;
137        }
138}
139
140?>
Note: See TracBrowser for help on using the repository browser.