";
$dcroom = new DCRoom();
if ($dcroom->canCreate()) {
echo "";
}
echo "
";
foreach ($cells as $cell) {
if ($rack->getFromDB($cell['id'])) {
echo self::getCell($rack, $cell);
}
}
// add a locked element to bottom to display a full grid
echo "";
echo "
"; //.grid-stack
echo $blueprint;
echo "
"; //.grid-room
echo "";
echo "";
echo "
"; // #viewgraph
$rack_add_tip = __s('Insert a rack here');
$js = <<' + getBijectiveIndex(x) + '');
}
for (var y = 1; y <= $rows; y++) {
$('.indexes-y').append('
' + y + '
');
}
// append cells for adding racks
for (var y = 1; y <= $rows; y++) {
for (var x = 1; x <= $cols; x++) {
$('.racks_add')
.append('
{$rack_add_tip}
');
}
}
var x_before_drag = 0;
var y_before_drag = 0;
var dirty = false;
$('.grid-stack')
.on('change', function(event, items) {
if (dirty) {
return;
}
var grid = $(event.target).data('gridstack');
$.each(items, function(index, item) {
$.post('{$ajax_url}', {
id: item.id,
dcrooms_id: $room_id,
action: 'move_rack',
x: item.x + 1,
y: item.y + 1,
}, function(answer) {
var answer = jQuery.parseJSON(answer);
// revert to old position
if (!answer.status) {
dirty = true;
grid.move(item.el, x_before_drag, y_before_drag);
dirty = false;
displayAjaxMessageAfterRedirect();
}
});
});
})
.on('dragstart', function(event, ui) {
var element = $(event.target);
var node = element.data('_gridstack_node');
// store position before drag
x_before_drag = Number(node.x);
y_before_drag = Number(node.y);
// disable qtip
element.qtip('hide', true);
})
.on('click', function(event, ui) {
var grid = this;
var element = $(event.target);
var el_url = element.find('a').attr('href');
if (el_url) {
window.location = el_url;
}
});
$('#viewgraph .cell_add').on('click', function(){
var _this = $(this);
if (_this.find('div').length == 0) {
var _x = _this.data('x');
var _y = _this.data('y');
$.ajax({
url : "{$rack->getFormURL()}",
data: {
room: $room_id,
position: _x + ',' + _y,
ajax: true
},
success: function(data) {
$('#grid-dialog')
.html(data)
.dialog({
modal: true,
width: 'auto'
});
}
});
}
});
$('#viewgraph .cell_add, #viewgraph .grid-stack-item').each(function() {
var tipcontent = $(this).find('.tipcontent');
if (tipcontent.length) {
$(this).qtip({
position: {
my: 'left center',
at: 'right center',
},
content: {
text: tipcontent
},
style: {
classes: 'qtip-shadow qtip-bootstrap rack_tipcontent'
}
});
}
});
});
JAVASCRIPT;
echo Html::scriptBlock($js);
}
function prepareInputForAdd($input) {
if ($this->prepareInput($input)) {
if (isset($input["id"]) && ($input["id"] > 0)) {
$input["_oldID"] = $input["id"];
}
unset($input['id']);
unset($input['withtemplate']);
return $input;
}
return false;
}
function prepareInputForUpdate($input) {
return $this->prepareInput($input);
}
function post_getEmpty() {
$this->fields['bgcolor'] = '#FEC95C';
}
/**
* Prepares input (for update and add)
*
* @param array $input Input data
*
* @return array
*/
private function prepareInput($input) {
if (!array_key_exists('dcrooms_id', $input) || $input['dcrooms_id'] == 0) {
// Position is not set if room not selected
return $input;
}
if ($input['position'] == 0) {
return $input;
Session::addMessageAfterRedirect(
__('Position must be set'),
true,
ERROR
);
return false;
}
$where = [
'dcrooms_id' => $input['dcrooms_id'],
'position' => $input['position'],
'is_deleted' => false
];
if (!$this->isNewItem()) {
$where['NOT'] = ['id' => $input['id']];
}
$existing = countElementsInTable(self::getTable(), $where);
if ($existing > 0) {
Session::addMessageAfterRedirect(
sprintf(
__('%1$s position is not available'),
$input['position']
),
true,
ERROR
);
return false;
}
return $input;
}
/**
* Get already filled places
*
* @param string $current Current position to exclude; defaults to null
*
* @return array [x => [left => [depth, depth, depth, depth]], [right => [depth, depth, depth, depth]]]
*/
public function getFilled($itemtype = null, $items_id = null) {
global $DB;
$iterator = $DB->request([
'FROM' => Item_Rack::getTable(),
'WHERE' => [
'racks_id' => $this->getID()
]
]);
$filled = [];
while ($row = $iterator->next()) {
$item = new $row['itemtype'];
if (!$item->getFromDB($row['items_id'])) {
continue;
}
$units = 1;
$width = 1;
$depth = 1;
if ($item->fields[strtolower($item->getType()) . 'models_id'] != 0) {
$model_class = $item->getType() . 'Model';
$modelsfield = strtolower($item->getType()) . 'models_id';
$model = new $model_class;
if ($model->getFromDB($item->fields[$modelsfield])) {
$units = $model->fields['required_units'];
$depth = $model->fields['depth'];
$width = $model->fields['is_half_rack'] == 0 ? 1 : 0.5;
}
}
$position = $row['position'];
if (empty($itemtype) || empty($items_id)
|| $itemtype != $row['itemtype'] || $items_id != $row['items_id']
) {
while (--$units >= 0) {
$content_filled = [
self::POS_LEFT => [0, 0, 0, 0],
self::POS_RIGHT => [0, 0, 0, 0]
];
if (isset($filled[$position + $units])) {
$content_filled = $filled[$position + $units];
}
if ($row['hpos'] == self::POS_NONE || $row['hpos'] == self::POS_LEFT) {
$d = 0;
while ($d/4 < $depth) {
$pos = ($row['orientation'] == self::REAR) ? 3 - $d : $d;
$val = 1;
if (isset($content_filled[self::POS_LEFT][$pos]) && $content_filled[self::POS_LEFT][$pos] != 0) {
Toolbox::logError('Several elements exists in rack at same place :(');
$val += $content_filled[self::POS_LEFT][$pos];
}
$content_filled[self::POS_LEFT][$pos] = $val;
++$d;
}
}
if ($row['hpos'] == self::POS_NONE || $row['hpos'] == self::POS_RIGHT) {
$d = 0;
while ($d/4 < $depth) {
$pos = ($row['orientation'] == self::REAR) ? 3 - $d : $d;
$val = 1;
if (isset($content_filled[self::POS_RIGHT][$pos]) && $content_filled[self::POS_RIGHT][$pos] != 0) {
Toolbox::logError('Several elements exists in rack at same place :(');
$val += $content_filled[self::POS_RIGHT][$pos];
}
$content_filled[self::POS_RIGHT][$pos] = $val;
++$d;
}
}
$filled[$position + $units] = $content_filled;
}
}
}
return $filled;
}
public function getEmpty() {
if (!parent::getEmpty()) {
return false;
}
$this->fields['number_units'] = 42;
return true;
}
function cleanDBonPurge() {
$this->deleteChildrenAndRelationsFromDb(
[
Item_Rack::class,
PDU_Rack::class,
]
);
}
/**
* Get cell content
*
* @param Rack $rack Rack instance
* @param mixed $cell Rack cell (array or false)
*
* @return string
*/
private static function getCell(Rack $rack, $cell) {
$bgcolor = $rack->getField('bgcolor');
$fgcolor = Html::getInvertedColor($bgcolor);
return "