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 | */ |
---|
27 | class 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 | 'Doctype' => array('unique' => true), |
---|
35 | 'Format' => array('unique' => true), |
---|
36 | ); |
---|
37 | |
---|
38 | /** @var array Job queries can be quite complex, so use Containable */ |
---|
39 | public $actsAs = array('Containable'); |
---|
40 | |
---|
41 | /** |
---|
42 | * Set validation rules in here so we can have i18n messages |
---|
43 | */ |
---|
44 | public function beforeValidate() |
---|
45 | { |
---|
46 | $this->validate = array( |
---|
47 | 'version' => array( |
---|
48 | 'rule' => array('custom', '/^[a-z0-9(). -]{1,}$/i'), |
---|
49 | 'message' => __('Only letters, numbers, dashes, parenthesis, the dot (.) and spaces allowed.', true) |
---|
50 | ) |
---|
51 | ); |
---|
52 | |
---|
53 | return True; |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Get a list of all active worker types. That is, unique combinations of Platform, Doctype and Application |
---|
58 | * @param boolean $stable If true, return only stable factories that want to participate in test suites |
---|
59 | * @return array |
---|
60 | */ |
---|
61 | public function getActive($stable = false) |
---|
62 | { |
---|
63 | App::import('Sanitize'); |
---|
64 | $time = date('Y-m-d H:i:s', time() - Configure::read('Factory.polltime')); |
---|
65 | |
---|
66 | if ($stable) { |
---|
67 | $stableTime = date('Y-m-d H:i:s', time() - Configure::read('Factory.stabletime')); |
---|
68 | $sqlWhere = "AND `Factory`.`active_since` != '0000-00-00 00:00:00' |
---|
69 | AND `Factory`.`active_since` < '$stableTime' |
---|
70 | AND `Worker`.`testsuite` = 1"; |
---|
71 | } else { |
---|
72 | $sqlWhere = ''; |
---|
73 | } |
---|
74 | |
---|
75 | $active = $this->query("SELECT DISTINCT |
---|
76 | `Worker`.`version`, |
---|
77 | `Worker`.`development`, |
---|
78 | `Platform`.`id`, |
---|
79 | `Platform`.`name`, |
---|
80 | `Doctype`.`id`, |
---|
81 | `Doctype`.`name`, |
---|
82 | `Doctype`.`code`, |
---|
83 | `Application`.`id`, |
---|
84 | `Application`.`name` |
---|
85 | FROM `workers` AS `Worker` |
---|
86 | LEFT JOIN `factories` AS `Factory` ON (`Worker`.`factory_id` = `Factory`.`id`) |
---|
87 | LEFT JOIN `operatingsystems` AS `Operatingsystem` ON (`Factory`.`operatingsystem_id` = `Operatingsystem`.`id`) |
---|
88 | LEFT JOIN `platforms` AS `Platform` ON (`Operatingsystem`.`platform_id` = `Platform`.`id`) |
---|
89 | LEFT JOIN `applications` AS `Application` ON (`Worker`.`application_id` = `Application`.`id`) |
---|
90 | LEFT JOIN `doctypes_workers` AS `DoctypesWorker` ON `Worker`.`id` = `DoctypesWorker`.`worker_id` |
---|
91 | LEFT JOIN `doctypes` AS `Doctype` ON (`DoctypesWorker`.`doctype_id` = `Doctype`.`id`) |
---|
92 | WHERE `Factory`.`last_poll` > '$time' |
---|
93 | $sqlWhere |
---|
94 | ORDER BY `Platform`.`name` ASC, `Application`.`name` ASC, `Worker`.`version` ASC"); |
---|
95 | |
---|
96 | if (is_array($active)) { |
---|
97 | foreach ($active as &$app) { |
---|
98 | $app['id'] = $app['Platform']['id'] . '_' . $app['Doctype']['code'] . '_' . $app['Application']['id'] . '_' |
---|
99 | . $app['Worker']['version']; |
---|
100 | |
---|
101 | $formats = $this->query("SELECT DISTINCT |
---|
102 | `Format`.`id`, |
---|
103 | `Format`.`code` |
---|
104 | FROM `workers` AS `Worker` |
---|
105 | LEFT JOIN `formats_workers` AS `FormatsWorker` ON (`Worker`.`id` = `FormatsWorker`.`worker_id`) |
---|
106 | LEFT JOIN `formats` AS `Format` ON (`FormatsWorker`.`format_id` = `Format`.`id`) |
---|
107 | LEFT JOIN `factories` AS `Factory` ON (`Worker`.`factory_id` = `Factory`.`id`) |
---|
108 | LEFT JOIN `operatingsystems` AS `Operatingsystem` ON (`Factory`.`operatingsystem_id` = `Operatingsystem`.`id`) |
---|
109 | LEFT JOIN `applications` AS `Application` ON (`Worker`.`application_id` = `Application`.`id`) |
---|
110 | LEFT JOIN `doctypes_workers` AS `DoctypesWorker` ON `Worker`.`id` = `DoctypesWorker`.`worker_id` |
---|
111 | WHERE `Factory`.`last_poll` > '$time' |
---|
112 | $sqlWhere |
---|
113 | AND `Worker`.`version` = '" . Sanitize::escape($app['Worker']['version']) . "' |
---|
114 | AND `Application`.`id` = '" . $app['Application']['id'] . "' |
---|
115 | AND `Operatingsystem`.`platform_id` = '" . $app['Platform']['id'] . "' |
---|
116 | AND `DoctypesWorker`.`doctype_id` = '" . $app['Doctype']['id'] . "'"); |
---|
117 | |
---|
118 | $app['Format'] = array(); |
---|
119 | foreach ($formats as $format) { |
---|
120 | $app['Format'][] = $format['Format']; |
---|
121 | } |
---|
122 | } |
---|
123 | return $active; |
---|
124 | } |
---|
125 | return array(); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | ?> |
---|