bat_api.module 37.7 KB
Newer Older
istos's avatar
istos committed
1 2 3
<?php

/**
istos's avatar
istos committed
4
 * API access to booking data for BAT
istos's avatar
istos committed
5 6
 */

7 8 9 10
use Roomify\Bat\Event\Event;
use Roomify\Bat\Calendar\Calendar;
use Roomify\Bat\Store\DrupalDBStore;
use Roomify\Bat\Unit\Unit;
istos's avatar
istos committed
11 12
use Drupal\bat_fullcalendar\FullCalendarOpenStateEventFormatter;
use Drupal\bat_fullcalendar\FullCalendarFixedStateEventFormatter;
istos's avatar
istos committed
13

istos's avatar
istos committed
14
/**
15
 * Implements hook_ctools_plugin_api().
istos's avatar
istos committed
16
 */
istos's avatar
istos committed
17
function bat_api_ctools_plugin_api($module = NULL, $api = NULL) {
18 19 20 21 22 23 24 25
  if ($module == "services" && $api == "services") {
    return array("version" => "3");
  }
}

/**
 * Implements hook_ctools_plugin_directory().
 */
istos's avatar
istos committed
26
function bat_api_ctools_plugin_directory($owner, $plugin_type) {
istos's avatar
istos committed
27 28
  if ($owner == 'bat_api' && $plugin_type == 'bat_unit_field_handler') {
    return 'plugins/bat_unit_field_handler';
29 30 31 32 33 34
  }
}

/**
 * Implements hook_ctools_plugin_type().
 */
istos's avatar
istos committed
35
function bat_api_ctools_plugin_type() {
36
  return array(
istos's avatar
istos committed
37
    'bat_unit_field_handler' => array(
38 39 40 41 42 43 44 45
      'use hooks' => TRUE,
    ),
  );
}

/**
 * Implements hook_permission()
 */
istos's avatar
istos committed
46
function bat_api_permission() {
47
  return array(
Nicolò Caruso's avatar
Nicolò Caruso committed
48 49 50 51 52 53 54 55 56 57 58 59
    'access types index service' =>  array(
      'title' => t('Access types index service'),
      'description' => t('Access types index service.'),
    ),
    'access edit type service' =>  array(
      'title' => t('Access edit type service'),
      'description' => t('Access edit type service.'),
    ),
    'access add type service' =>  array(
      'title' => t('Access add type service'),
      'description' => t('Access add type service.'),
    ),
60 61 62 63
    'access units index service' =>  array(
      'title' => t('Access units index service'),
      'description' => t('Access units index service.'),
    ),
Nicolò Caruso's avatar
Nicolò Caruso committed
64 65 66 67
    'access edit unit service' =>  array(
      'title' => t('Access edit unit service'),
      'description' => t('Access edit unit service.'),
    ),
68 69 70 71
    'access add unit service' =>  array(
      'title' => t('Access add unit service'),
      'description' => t('Access add unit service.'),
    ),
Nicolò Caruso's avatar
Nicolò Caruso committed
72 73 74 75 76 77 78
    'access units calendar index service' =>  array(
      'title' => t('Access units calendar index service'),
      'description' => t('Access units calendar index service.'),
    ),
    'access events index service' =>  array(
      'title' => t('Access events index service'),
      'description' => t('Access events index service.'),
79
    ),
Nicolò Caruso's avatar
Nicolò Caruso committed
80 81 82
    'access events calendar index service' =>  array(
      'title' => t('Access events calendar index service'),
      'description' => t('Access events calendar index service.'),
83
    ),
Nicolò Caruso's avatar
Nicolò Caruso committed
84 85 86 87
    'access edit event service' =>  array(
      'title' => t('Access edit event service'),
      'description' => t('Access edit event service.'),
    ),
88 89 90 91 92 93
  );
}

/**
 * Implements hook_services_resources().
 */
istos's avatar
istos committed
94
function bat_api_services_resources() {
95 96
  $resources = array();

Nicolò Caruso's avatar
Nicolò Caruso committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  $resources['types'] = array(
    'operations' => array(
      'index' => array(
        'callback' => 'bat_api_services_types_index',
        'help' => t('Returns an object containing types information.'),
        'access arguments' => array('access types index service'),
        'args'     => array(
          array(
            'name'         => 'ids',
            'type'         => 'string',
            'optional'     => TRUE,
            'default value' => '',
            'description'  => t('IDs'),
            'source'       => array('param' => 'ids'),
          ),
          array(
            'name'         => 'offset',
            'type'         => 'string',
            'optional'     => TRUE,
            'default value' => 0,
            'description'  => t('Result offset to start listing'),
            'source'       => array('param' => 'offset'),
          ),
          array(
            'name'         => 'limit',
            'type'         => 'string',
            'optional'     => TRUE,
            'default value' => 100,
            'description'  => t('Number of items to return'),
            'source'       => array('param' => 'limit'),
          ),
        ),
      ),
      'update' => array(
        'callback' => 'bat_api_services_edit_type',
        'access arguments' => array('access edit type service'),
        'help'     => t('Edit an existing BAT Type.'),
        'args'     => array(
          array(
            'name'         => 'type_id',
            'type'         => 'integer',
            'description'  => t('Type ID'),
            'source'       => array('path' => 0),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'bat_type',
            'type'         => 'array',
            'description'  => t('The type data to update'),
            'source'       => 'data',
            'optional'     => FALSE,
          ),
        ),
      ),
    ),
    'actions' => array(
      'add' => array(
        'callback' => 'bat_api_services_add_type',
        'access arguments' => array('access add type service'),
        'help'     => t('Add a new BAT Type.'),
        'args'     => array(
          array(
            'name'         => 'name',
            'type'         => 'string',
            'description'  => t('Name'),
            'source'       => array('data' => 'name'),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'type',
            'type'         => 'string',
            'description'  => t('Type'),
            'source'       => array('data' => 'type'),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'uid',
            'type'         => 'integer',
            'description'  => t('UID'),
            'source'       => array('data' => 'uid'),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'status',
            'type'         => 'integer',
            'description'  => t('Status'),
            'source'       => array('data' => 'status'),
            'optional'     => FALSE,
          ),
        ),
      ),
    ),
  );

191 192 193
  $resources['units'] = array(
    'operations' => array(
      'index' => array(
istos's avatar
istos committed
194
        'callback' => 'bat_api_services_units_index',
Nicolò Caruso's avatar
Nicolò Caruso committed
195
        'help' => t('Returns an object containing units information.'),
196 197 198 199 200 201 202 203 204 205
        'access arguments' => array('access units index service'),
        'args'     => array(
          array(
            'name'         => 'ids',
            'type'         => 'string',
            'optional'     => TRUE,
            'default value' => '',
            'description'  => t('IDs'),
            'source'       => array('param' => 'ids'),
          ),
Nicolò Caruso's avatar
Nicolò Caruso committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
          array(
            'name'         => 'offset',
            'type'         => 'string',
            'optional'     => TRUE,
            'default value' => 0,
            'description'  => t('Result offset to start listing'),
            'source'       => array('param' => 'offset'),
          ),
          array(
            'name'         => 'limit',
            'type'         => 'string',
            'optional'     => TRUE,
            'default value' => 100,
            'description'  => t('Number of items to return'),
            'source'       => array('param' => 'limit'),
          ),
222 223 224
        ),
      ),
      'update' => array(
istos's avatar
istos committed
225
        'callback' => 'bat_api_services_edit_unit',
226
        'access arguments' => array('access edit unit service'),
istos's avatar
istos committed
227
        'help'     => t('Edit an existing Bookable Unit.'),
228 229 230 231 232 233 234 235 236
        'args'     => array(
          array(
            'name'         => 'unit_id',
            'type'         => 'integer',
            'description'  => t('Unit ID'),
            'source'       => array('path' => 0),
            'optional'     => FALSE,
          ),
          array(
Nicolò Caruso's avatar
Nicolò Caruso committed
237 238 239 240 241
            'name'         => 'bat_unit',
            'type'         => 'array',
            'description'  => t('The unit data to update'),
            'source'       => 'data',
            'optional'     => FALSE,
242 243 244 245 246 247
          ),
        ),
      ),
    ),
    'actions' => array(
      'add' => array(
istos's avatar
istos committed
248
        'callback' => 'bat_api_services_add_unit',
249
        'access arguments' => array('access add unit service'),
istos's avatar
istos committed
250
        'help'     => t('Add a new Bookable Unit.'),
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        'args'     => array(
          array(
            'name'         => 'name',
            'type'         => 'string',
            'description'  => t('Name'),
            'source'       => array('data' => 'name'),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'type',
            'type'         => 'string',
            'description'  => t('Type'),
            'source'       => array('data' => 'type'),
            'optional'     => FALSE,
          ),
Nicolò Caruso's avatar
Nicolò Caruso committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279
          array(
            'name'         => 'type_id',
            'type'         => 'integer',
            'description'  => t('Type ID'),
            'source'       => array('data' => 'type_id'),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'uid',
            'type'         => 'integer',
            'description'  => t('UID'),
            'source'       => array('data' => 'uid'),
            'optional'     => FALSE,
          ),
280 281 282
          array(
            'name'         => 'status',
            'type'         => 'integer',
Nicolò Caruso's avatar
Nicolò Caruso committed
283
            'description'  => t('Status'),
284 285 286 287 288 289 290 291
            'source'       => array('data' => 'status'),
            'optional'     => FALSE,
          ),
        ),
      ),
    ),
  );

292
  $resources['units-calendar'] = array(
Nicolò Caruso's avatar
Nicolò Caruso committed
293 294
    'operations' => array(
      'index' => array(
Nicolò Caruso's avatar
Nicolò Caruso committed
295
        'callback' => 'bat_api_services_units_index_calendar',
Nicolò Caruso's avatar
Nicolò Caruso committed
296 297
        'help' => t('Returns an object containing units information.'),
        'access arguments' => array('access units calendar index service'),
Nicolò Caruso's avatar
Nicolò Caruso committed
298 299
        'args'     => array(
          array(
Nicolò Caruso's avatar
Nicolò Caruso committed
300
            'name'         => 'types',
Nicolò Caruso's avatar
Nicolò Caruso committed
301
            'type'         => 'string',
Nicolò Caruso's avatar
Nicolò Caruso committed
302 303 304 305
            'optional'     => TRUE,
            'default value' => '',
            'description'  => t('Types'),
            'source'       => array('param' => 'types'),
Nicolò Caruso's avatar
Nicolò Caruso committed
306 307
          ),
          array(
Nicolò Caruso's avatar
Nicolò Caruso committed
308
            'name'         => 'ids',
Nicolò Caruso's avatar
Nicolò Caruso committed
309
            'type'         => 'string',
Nicolò Caruso's avatar
Nicolò Caruso committed
310 311 312 313
            'optional'     => TRUE,
            'default value' => '',
            'description'  => t('IDs'),
            'source'       => array('param' => 'ids'),
Nicolò Caruso's avatar
Nicolò Caruso committed
314
          ),
Nicolò Caruso's avatar
Nicolò Caruso committed
315 316 317 318 319 320 321
          array(
            'name'         => 'event_type',
            'type'         => 'string',
            'description'  => t('Event Type'),
            'source'       => array('param' => 'event_type'),
            'optional'     => TRUE,
          ),
322 323 324 325 326 327 328 329
          array(
            'name'         => 'children',
            'type'         => 'boolean',
            'description'  => t('Return children'),
            'source'       => array('param' => 'children'),
            'optional'     => TRUE,
            'default value' => TRUE,
          ),
Nicolò Caruso's avatar
Nicolò Caruso committed
330
          array(
Nicolò Caruso's avatar
Nicolò Caruso committed
331
            'name'         => 'offset',
Nicolò Caruso's avatar
Nicolò Caruso committed
332
            'type'         => 'string',
Nicolò Caruso's avatar
Nicolò Caruso committed
333 334 335 336
            'optional'     => TRUE,
            'default value' => 0,
            'description'  => t('Result offset to start listing'),
            'source'       => array('param' => 'offset'),
Nicolò Caruso's avatar
Nicolò Caruso committed
337 338
          ),
          array(
Nicolò Caruso's avatar
Nicolò Caruso committed
339
            'name'         => 'limit',
Nicolò Caruso's avatar
Nicolò Caruso committed
340
            'type'         => 'string',
Nicolò Caruso's avatar
Nicolò Caruso committed
341 342 343 344
            'optional'     => TRUE,
            'default value' => 100,
            'description'  => t('Number of items to return'),
            'source'       => array('param' => 'limit'),
Nicolò Caruso's avatar
Nicolò Caruso committed
345 346 347 348 349 350
          ),
        ),
      ),
    ),
  );

Nicolò Caruso's avatar
Nicolò Caruso committed
351
  $resources['events'] = array(
352 353
    'operations' => array(
      'index' => array(
Nicolò Caruso's avatar
Nicolò Caruso committed
354
        'callback' => 'bat_api_services_events_index',
Nicolò Caruso's avatar
Nicolò Caruso committed
355 356
        'help' => t('Returns an object containing events information.'),
        'access arguments' => array('access events index service'),
357 358
        'args'     => array(
          array(
359
            'name'         => 'target_ids',
360
            'type'         => 'string',
361 362
            'description'  => t('Targe Entity IDs'),
            'source'       => array('param' => 'target_ids'),
363 364 365
            'optional'     => FALSE,
          ),
          array(
366
            'name'         => 'target_types',
367
            'type'         => 'string',
368 369
            'description'  => t('Target Types'),
            'source'       => array('param' => 'target_types'),
Nicolò Caruso's avatar
Nicolò Caruso committed
370
            'optional'     => TRUE,
371
          ),
372 373 374 375 376 377 378
          array(
            'name'         => 'target_entity_type',
            'type'         => 'string',
            'description'  => t('Target Entity Type'),
            'source'       => array('param' => 'target_entity_type'),
            'optional'     => FALSE,
          ),
379 380 381 382 383 384 385 386 387 388
          array(
            'name'         => 'start_date',
            'type'         => 'string',
            'description'  => t('Start date'),
            'source'       => array('param' => 'start_date'),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'end_date',
            'type'         => 'string',
Nicolò Caruso's avatar
Nicolò Caruso committed
389
            'description'  => t('End date'),
Nicolò Caruso's avatar
Nicolò Caruso committed
390
            'source'       => array('param' => 'end_date'),
391 392
            'optional'     => FALSE,
          ),
Nicolò Caruso's avatar
Nicolò Caruso committed
393
          array(
Nicolò Caruso's avatar
Nicolò Caruso committed
394
            'name'         => 'event_types',
Nicolò Caruso's avatar
Nicolò Caruso committed
395
            'type'         => 'string',
Nicolò Caruso's avatar
Nicolò Caruso committed
396 397
            'description'  => t('Type'),
            'source'       => array('param' => 'event_types'),
Nicolò Caruso's avatar
Nicolò Caruso committed
398 399
            'optional'     => FALSE,
          ),
400 401
        ),
      ),
acrollet's avatar
acrollet committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
      'update' => array(
        'callback' => 'bat_api_services_edit_event',
        'access arguments' => array('access edit event service'),
        'help'     => t('Edit an existing Event.'),
        'args'     => array(
          array(
            'name'         => 'event_id',
            'type'         => 'integer',
            'description'  => t('Event ID'),
            'source'       => array('path' => 0),
            'optional'     => FALSE,
          ),
          array(
            'name' => 'bat_event',
            'type' => 'array',
            'description' => t('The event data to update'),
            'source' => 'data',
            'optional' => FALSE,
          ),
        ),
      ),
423
    ),
istos's avatar
istos committed
424 425
  );

426
  $resources['events-calendar'] = array(
427 428 429
    'operations' => array(
      'index' => array(
        'callback' => 'bat_api_services_events_index_calendar',
Nicolò Caruso's avatar
Nicolò Caruso committed
430 431
        'help' => t('Returns an object containing events information.'),
        'access arguments' => array('access events calendar index service'),
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
        'args'     => array(
          array(
            'name'         => 'unit_ids',
            'type'         => 'string',
            'description'  => t('Unit IDs'),
            'source'       => array('param' => 'unit_ids'),
            'optional'     => TRUE,
          ),
          array(
            'name'         => 'unit_types',
            'type'         => 'string',
            'description'  => t('Unit Types'),
            'source'       => array('param' => 'unit_types'),
            'optional'     => TRUE,
          ),
          array(
            'name'         => 'start',
            'type'         => 'string',
            'description'  => t('Start date'),
            'source'       => array('param' => 'start'),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'end',
            'type'         => 'string',
            'description'  => t('End date'),
            'source'       => array('param' => 'end'),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'event_types',
            'type'         => 'string',
            'description'  => t('Type'),
            'source'       => array('param' => 'event_types'),
            'optional'     => FALSE,
          ),
468 469 470 471 472 473 474 475
          array(
            'name'         => 'background',
            'type'         => 'boolean',
            'description'  => t('Background'),
            'source'       => array('param' => 'background'),
            'optional'     => TRUE,
            'default value' => TRUE,
          ),
476 477 478 479 480
        ),
      ),
    ),
  );

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
  $resources['matching-units-calendar'] = array(
    'operations' => array(
      'index' => array(
        'callback' => 'bat_api_services_matching_units_calendar',
        'help' => t('Returns an object containing events information.'),
        'access arguments' => array('access events calendar index service'),
        'args'     => array(
          array(
            'name'         => 'unit_types',
            'type'         => 'string',
            'description'  => t('Unit Types'),
            'source'       => array('param' => 'unit_types'),
            'optional'     => TRUE,
          ),
          array(
            'name'         => 'start',
            'type'         => 'string',
            'description'  => t('Start date'),
            'source'       => array('param' => 'start'),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'end',
            'type'         => 'string',
            'description'  => t('End date'),
            'source'       => array('param' => 'end'),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'event_type',
            'type'         => 'string',
            'description'  => t('Type'),
            'source'       => array('param' => 'event_type'),
            'optional'     => FALSE,
          ),
          array(
            'name'         => 'event_states',
            'type'         => 'string',
            'description'  => t('Event States'),
            'source'       => array('param' => 'event_states'),
            'optional'     => FALSE,
          ),
        ),
      ),
    ),
  );

528 529 530
  return $resources;
}

Nicolò Caruso's avatar
Nicolò Caruso committed
531 532 533 534 535
/**
 * Retrieve a list of units to use with Fullcalendar scheduler.
 *
 * @param $unit_types
 * @param $unit_ids
Nicolò Caruso's avatar
Nicolò Caruso committed
536
 * @param $event_type
Nicolò Caruso's avatar
Nicolò Caruso committed
537 538 539
 * @param $offset
 * @param $limit
 */
540
function bat_api_services_units_index_calendar($unit_types, $unit_ids, $event_type, $return_children, $offset, $limit) {
541 542 543 544 545 546
  $create_event_access = FALSE;

  if (bat_event_access('create', bat_event_create(array('type' => $event_type)))) {
    $create_event_access = TRUE;
  }

Nicolò Caruso's avatar
Nicolò Caruso committed
547 548
  $ids = array_filter(explode(',', $unit_ids));

549 550
  if ($unit_types == 'all') {
    $types = array();
Nicolò Caruso's avatar
Nicolò Caruso committed
551 552 553 554 555 556 557 558 559

    foreach (bat_unit_get_types() as $type) {
      $type_bundle = bat_type_bundle_load($type->type);

      if (is_array($type_bundle->default_event_value_field_ids)) {
        if (isset($type_bundle->default_event_value_field_ids[$event_type]) && !empty($type_bundle->default_event_value_field_ids[$event_type])) {
          $types[] = $type->type_id;
        }
      }
560
    }
Nicolò Caruso's avatar
Nicolò Caruso committed
561
  }
562 563
  else {
    $types = array_filter(explode(',', $unit_types));
Nicolò Caruso's avatar
Nicolò Caruso committed
564 565
  }

566
  $bat_event_type = bat_event_type_load($event_type);
Nicolò Caruso's avatar
Nicolò Caruso committed
567

568
  $target_entity_type = $bat_event_type->target_entity_type;
569

570
  $controller = entity_get_controller($target_entity_type);
Nicolò Caruso's avatar
Nicolò Caruso committed
571

572
  $units = array();
Nicolò Caruso's avatar
Nicolò Caruso committed
573

574
  foreach ($types as $type) {
Nicolò Caruso's avatar
Nicolò Caruso committed
575
    $entities = $controller->getReferencedIds($type, $ids);
Nicolò Caruso's avatar
Nicolò Caruso committed
576

577
    $childrens = array();
Nicolò Caruso's avatar
Nicolò Caruso committed
578

579 580 581 582 583 584 585
    foreach ($entities as $entity) {
      $childrens[$entity['type_id']][] = array(
        'id' => 'S' . $entity['id'],
        'title' => $entity['name'],
        'create_event' => $create_event_access,
      );
    }
Nicolò Caruso's avatar
Nicolò Caruso committed
586

587 588 589
    foreach ($childrens as $type_id => $children) {
      $unit_type = bat_type_load($type_id);

590 591 592 593 594 595 596 597 598 599 600 601 602
      if ($return_children) {
        $units[] = array(
          'id' => $unit_type->type_id,
          'title' => $unit_type->name,
          'children' => $children,
        );
      }
      else {
        $units[] = array(
          'id' => $unit_type->type_id,
          'title' => $unit_type->name,
        );
      }
603
    }
604 605 606
  }

  return $units;
Nicolò Caruso's avatar
Nicolò Caruso committed
607 608
}

609
/**
610
 * Retrieve a list of units.
611
 *
Nicolò Caruso's avatar
Nicolò Caruso committed
612
 * @param $unit_ids
Nicolò Caruso's avatar
Nicolò Caruso committed
613 614
 * @param $offset
 * @param $limit
615
 */
istos's avatar
istos committed
616
function bat_api_services_units_index($unit_ids, $offset, $limit) {
617 618 619 620
  $return = new stdClass();
  $return->sessid = session_id();

  ctools_include('plugins');
istos's avatar
istos committed
621
  $field_handlers = ctools_get_plugins('bat_api', 'bat_unit_field_handler');
622

Nicolò Caruso's avatar
Nicolò Caruso committed
623
  $ids = array_filter(explode(',', $unit_ids));
624

istos's avatar
istos committed
625
  $query = db_select('bat_units', 'n')
Nicolò Caruso's avatar
Nicolò Caruso committed
626
            ->fields('n', array('unit_id', 'type', 'type_id', 'name', 'language', 'status', 'uid'));
627 628 629
  if (!empty($ids)) {
    $query->condition('unit_id', $ids, 'IN');
  }
Nicolò Caruso's avatar
Nicolò Caruso committed
630 631
  $query->orderBy('unit_id');
  $query->range($offset, $limit);
istos's avatar
istos committed
632
  $bat_units = $query->execute()->fetchAll();
633 634

  $units = array();
istos's avatar
istos committed
635
  foreach ($bat_units as $unit) {
636 637 638
    $units[$unit->unit_id] = array(
      'properties' => array(
        'name' => $unit->name,
Nicolò Caruso's avatar
Nicolò Caruso committed
639 640
        'type' => $unit->type,
        'type_id' => $unit->type_id,
Nicolò Caruso's avatar
Nicolò Caruso committed
641
        'language' => $unit->language,
642
        'status' => $unit->status,
Nicolò Caruso's avatar
Nicolò Caruso committed
643
        'uid' => $unit->uid,
644 645 646 647 648 649 650
      ),
      'fields' => array(
      ),
    );

    foreach ($field_handlers as $handler) {
      $class = ctools_plugin_get_class($handler, 'handler');
istos's avatar
istos committed
651
      $object_handler = new $class(bat_unit_load($unit->unit_id));
652 653

      $field_type = $object_handler->getFieldType();
654 655 656 657 658
      $field_info = $object_handler->getFieldInfo();

      if (!empty($field_info)) {
        $units[$unit->unit_id]['fields'][$field_type] = $field_info;
      }
659 660 661 662 663 664 665 666 667
    }
  }

  $return->units = $units;

  return $return;
}

/**
Nicolò Caruso's avatar
Nicolò Caruso committed
668
 * Create a new unit.
669
 *
Nicolò Caruso's avatar
Nicolò Caruso committed
670 671
 * @param $name
 * @param $type
Nicolò Caruso's avatar
Nicolò Caruso committed
672 673
 * @param $type_id
 * @param $uid
Nicolò Caruso's avatar
Nicolò Caruso committed
674
 * @param $status
675
 */
Nicolò Caruso's avatar
Nicolò Caruso committed
676
function bat_api_services_add_unit($name, $type, $type_id, $uid, $status) {
677 678 679
  $return = new stdClass();
  $return->sessid = session_id();

Nicolò Caruso's avatar
Nicolò Caruso committed
680
  if (bat_unit_bundle_load($type) !== FALSE && bat_type_load($type_id) !== FALSE && $name != '') {
istos's avatar
istos committed
681
    $unit = bat_unit_create(
Nicolò Caruso's avatar
Nicolò Caruso committed
682 683 684
      array(
        'name' => $name,
        'type' => $type,
Nicolò Caruso's avatar
Nicolò Caruso committed
685
        'type_id' => $type_id,
Nicolò Caruso's avatar
Nicolò Caruso committed
686
        'status' => $status,
Nicolò Caruso's avatar
Nicolò Caruso committed
687
        'uid' => $uid,
Nicolò Caruso's avatar
Nicolò Caruso committed
688 689 690 691
        'created' => REQUEST_TIME,
        'changed' => REQUEST_TIME,
      )
    );
692

istos's avatar
istos committed
693
    bat_unit_save($unit);
Nicolò Caruso's avatar
Nicolò Caruso committed
694 695 696 697 698 699 700

    $return->unit_id = $unit->unit_id;
    $return->status = 1;
  }
  else {
    $return->status = 0;
  }
701 702 703 704 705

  return $return;
}

/**
Nicolò Caruso's avatar
Nicolò Caruso committed
706
 * Update an existing unit.
707
 *
Nicolò Caruso's avatar
Nicolò Caruso committed
708
 * @param $unit_id
istos's avatar
istos committed
709
 * @param $bat_unit_data
710
 */
istos's avatar
istos committed
711
function bat_api_services_edit_unit($unit_id, $bat_unit_data) {
712 713 714
  $return = new stdClass();
  $return->sessid = session_id();

Nicolò Caruso's avatar
Nicolò Caruso committed
715
  if ($unit = bat_unit_load($unit_id)) {
istos's avatar
istos committed
716 717 718
    $unit->name = (isset($bat_unit_data['name'])) ? $bat_unit_data['name'] : $unit->name;
    $unit->status = (isset($bat_unit_data['status'])) ? $bat_unit_data['status'] : $unit->status;
    $unit->uid = (isset($bat_unit_data['uid'])) ? $bat_unit_data['uid'] : $unit->uid;
Nicolò Caruso's avatar
Nicolò Caruso committed
719
    $unit->data = (isset($bat_unit_data['data'])) ? $bat_unit_data['data'] : $unit->data;
Nicolò Caruso's avatar
Nicolò Caruso committed
720 721 722

    $unit->changed = REQUEST_TIME;

istos's avatar
istos committed
723
    bat_unit_save($unit);
Nicolò Caruso's avatar
Nicolò Caruso committed
724 725 726 727 728 729

    $return->status = 1;
  }
  else {
    $return->status = 0;
  }
730 731

  return $return;
istos's avatar
istos committed
732 733
}

Nicolò Caruso's avatar
Nicolò Caruso committed
734 735 736
/**
 * Retrieve a list of events.
 *
737 738 739
 * @param $target_ids
 * @param $target_types
 * @param $target_entity_type
Nicolò Caruso's avatar
Nicolò Caruso committed
740 741 742 743
 * @param $start_date
 * @param $end_date
 * @param $event_types
 */
744
function bat_api_services_events_index($target_ids, $target_types, $target_entity_type, $start_date, $end_date, $event_types) {
istos's avatar
istos committed
745 746
  global $databases;

Nicolò Caruso's avatar
Nicolò Caruso committed
747 748 749
  $return = new stdClass();
  $return->sessid = session_id();

750
  $target_types = array_filter(explode(',', $target_types));
Nicolò Caruso's avatar
Nicolò Caruso committed
751

Nicolò Caruso's avatar
Nicolò Caruso committed
752
  $types = array_filter(explode(',', $event_types));
Nicolò Caruso's avatar
Nicolò Caruso committed
753 754 755

  $events_json = array();

Nicolò Caruso's avatar
Nicolò Caruso committed
756
  foreach ($types as $type) {
Nicolò Caruso's avatar
Nicolò Caruso committed
757
    $prefix = (isset($databases['default']['default']['prefix'])) ? $databases['default']['default']['prefix'] : '';
istos's avatar
istos committed
758
    $event_store = new DrupalDBStore($type, DrupalDBStore::BAT_EVENT, $prefix);
Nicolò Caruso's avatar
Nicolò Caruso committed
759

Nicolò Caruso's avatar
Nicolò Caruso committed
760 761
    $start_date_object = new DateTime($start_date);
    $end_date_object = new DateTime($end_date);
762

Nicolò Caruso's avatar
Nicolò Caruso committed
763
    $today = new DateTime();
Nicolò Caruso's avatar
Nicolò Caruso committed
764
    if (!user_access('view past event information') && $today > $start_date_object) {
Nicolò Caruso's avatar
Nicolò Caruso committed
765 766 767
      if ($today > $end_date_object) {
        $return->events = array();
        return $return;
Nicolò Caruso's avatar
Nicolò Caruso committed
768
      }
769

Nicolò Caruso's avatar
Nicolò Caruso committed
770
      $start_date_object = $today;
771 772
    }

773
    $ids = explode(',', $target_ids);
774

Nicolò Caruso's avatar
Nicolò Caruso committed
775 776
    $units = array();
    foreach ($ids as $id) {
777 778
      if ($target_entity = entity_load_single($target_entity_type, $id)) {
        if (in_array($target_entity->type, $target_types) || empty($target_types)) {
779 780 781
          // Setting the default value to 0 since we are dealing with the events array
          // so getting event IDs.
          $units[] = new Unit($id, 0);
Nicolò Caruso's avatar
Nicolò Caruso committed
782
        }
783 784 785
      }
    }

Nicolò Caruso's avatar
Nicolò Caruso committed
786
    if (!empty($units)) {
787 788
      $event_calendar = new Calendar($units, $event_store);
      $event_ids = $event_calendar->getEvents($start_date_object, $end_date_object);
789

790
      $bat_event_type = bat_event_type_load($type);
acrollet's avatar
acrollet committed
791
      if ($bat_event_type->fixed_event_states) {
Nicolò Caruso's avatar
Nicolò Caruso committed
792
        $event_formatter = new FullCalendarFixedStateEventFormatter($bat_event_type);
acrollet's avatar
acrollet committed
793 794
      }
      else {
Nicolò Caruso's avatar
Nicolò Caruso committed
795
        $event_formatter = new FullCalendarOpenStateEventFormatter($bat_event_type);
acrollet's avatar
acrollet committed
796
      }
797

798
      foreach ($event_ids as $unit_id => $unit_events) {
Nicolò Caruso's avatar
Nicolò Caruso committed
799
        foreach ($unit_events as $key => $event) {
800 801 802
          $events_json[] = array(
            'id' => (string)$key . $unit_id,
            'bat_id' => $event->getValue(),
Nicolò Caruso's avatar
Nicolò Caruso committed
803
            'resourceId' => 'S' . $unit_id,
804
          ) + $event->toJson($event_formatter);
Nicolò Caruso's avatar
Nicolò Caruso committed
805 806
        }
      }
807
    }
istos's avatar
istos committed
808 809
  }

810
  $return->events = $events_json;
istos's avatar
istos committed
811

812
  return $return;
istos's avatar
istos committed
813
}
814

Nicolò Caruso's avatar
Nicolò Caruso committed
815 816 817 818 819 820 821 822 823
/**
 * Retrieve a list of events to use with Fullcalendar scheduler.
 *
 * @param $unit_ids
 * @param $unit_types
 * @param $start_date
 * @param $end_date
 * @param $event_types
 */
824
function bat_api_services_events_index_calendar($unit_ids, $unit_types, $start_date, $end_date, $event_types, $background) {
istos's avatar
istos committed
825 826
  global $databases;
  
827 828 829 830 831 832 833 834 835
  if ($unit_types == 'all') {
    $unit_types = array();
    foreach (bat_unit_get_types() as $type => $info) {
      $unit_types[] = $type;
    }
  }
  else {
    $unit_types = array_filter(explode(',', $unit_types));
  }
836

837 838 839 840 841 842 843 844 845
  if ($event_types == 'all') {
    $types = array();
    foreach (bat_event_get_types() as $type => $info) {
      $types[] = $type;
    }
  }
  else {
    $types = array_filter(explode(',', $event_types));
  }
846 847 848 849

  $events_json = array();

  foreach ($types as $type) {
850 851 852 853 854
    // Check if user has permission to view calendar data for this event type.
    if (!user_access('view calendar data for any ' . $type . ' event')) {
      continue;
    }

855 856
    // Get the event type definition from Drupal
    $bat_event_type = bat_event_type_load($type);
857

858 859 860 861
    $target_entity_type = $bat_event_type->target_entity_type;

    $controller = entity_get_controller($target_entity_type);

862
    // For each type of event create a state store and an event store
Nicolò Caruso's avatar
Nicolò Caruso committed
863
    $prefix = (isset($databases['default']['default']['prefix'])) ? $databases['default']['default']['prefix'] : '';
istos's avatar
istos committed
864
    $event_store = new DrupalDBStore($type, DrupalDBStore::BAT_EVENT, $prefix);
istos's avatar
istos committed
865

866 867 868 869
    $start_date_object = new DateTime($start_date);
    $end_date_object = new DateTime($end_date);

    $today = new DateTime();
istos's avatar
istos committed
870
    if (!user_access('view past event information') && $today > $start_date_object) {
871 872 873 874 875 876 877 878 879
      if ($today > $end_date_object) {
        $return->events = array();
        return $return;
      }
      $start_date_object = $today;
    }

    $ids = array_filter(explode(',', $unit_ids));

880
    foreach ($unit_types as $unit_type) {
Nicolò Caruso's avatar
Nicolò Caruso committed
881
      $entities = $controller->getReferencedIds($unit_type, $ids);
882

883
      $childrens = array();
884

885 886 887 888 889 890 891
      // Create an array of unit objects - the default value is set to 0 since we want
      // to know if the value in the database is actually 0. This will allow us to identify
      // which events are represented by events in the database (i.e. have a value different to 0)
      $units = array();
      foreach ($entities as $entity) {
        $units[] = new Unit($entity['id'], 0);
      }
istos's avatar
istos committed
892

893 894
      if (!empty($units)) {
        $event_calendar = new Calendar($units, $event_store);
895

896
        $event_ids = $event_calendar->getEvents($start_date_object, $end_date_object);
897

898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
        // @TODO - this is an undeclared dependency of bat_event_ui
        if ($bat_event_type->fixed_event_states) {
          $event_formatter = new FullCalendarFixedStateEventFormatter($bat_event_type, $background);
        }
        else {
          $event_formatter = new FullCalendarOpenStateEventFormatter($bat_event_type, $background);
        }

        foreach ($event_ids as $unit_id => $unit_events) {
          foreach ($unit_events as $key => $event) {
            $events_json[] = array(
              'id' => (string)$key . $unit_id,
              'bat_id' => $event->getValue(),
              'resourceId' => 'S' . $unit_id,
            ) + $event->toJson($event_formatter);
          }
914 915 916 917 918
        }
      }
    }
  }

Nicolò Caruso's avatar
Nicolò Caruso committed
919 920 921 922 923
  $events_json = _bat_api_merge_non_blocking_events($events_json);

  return array_values($events_json);
}

acrollet's avatar
acrollet committed
924 925 926 927 928 929 930 931
/**
 * Update an existing event.
 *
 * @param $event_id
 * @param $bat_event_data
 */
function bat_api_services_edit_event($event_id, $bat_event_data) {
  $return = new stdClass();
acrollet's avatar
acrollet committed
932
  $return->status = 0;
acrollet's avatar
acrollet committed
933 934
  $return->sessid = session_id();

acrollet's avatar
acrollet committed
935
  if ($event_id && $event = bat_event_load($event_id)) {
acrollet's avatar
acrollet committed
936 937 938
    $event->start_date = (isset($bat_event_data['start_date'])) ? $bat_event_data['start_date'] : $event->start_date;
    $event->end_date = (isset($bat_event_data['end_date'])) ? $bat_event_data['end_date'] : $event->end_date;
    $event->uid = (isset($bat_event_data['uid'])) ? $bat_event_data['uid'] : $event->uid;
acrollet's avatar
acrollet committed
939
    $event->data = (isset($bat_event_data['data'])) ? $bat_event_data['data'] : $event->data;
Nicolò Caruso's avatar
Nicolò Caruso committed
940

acrollet's avatar
acrollet committed
941 942
    $event->changed = REQUEST_TIME;

943 944 945 946 947 948
    // Set new target_id if present.
    if (isset($bat_event_data['target_id'])) {
      $event_type = bat_event_type_load($event->type);
      // Construct target entity reference field name using this event type's target entity type.
      $target_field_name = 'event_' . $event_type->target_entity_type . '_reference';
      $event->{$target_field_name} = array(
acrollet's avatar
acrollet committed
949 950
        LANGUAGE_NONE => array(
          0 => array(
951
            'target_id' => $bat_event_data['target_id'],
acrollet's avatar
acrollet committed
952 953 954 955 956
          )
        )
      );
    }

acrollet's avatar
acrollet committed
957 958 959 960 961 962 963 964 965 966
    // Set new state id if present.
    if (isset($bat_event_data['state_id'])) {
      $event->event_state_reference = array(
        LANGUAGE_NONE => array(
          0 => array(
            'state_id' => $bat_event_data['state_id'],
          )
        )
      );
    }
acrollet's avatar
acrollet committed
967

acrollet's avatar
acrollet committed
968
    $event->save();
Nicolò Caruso's avatar
Nicolò Caruso committed
969

acrollet's avatar
acrollet committed
970 971
    $return->status = 1;
  }
acrollet's avatar
acrollet committed
972 973 974 975 976 977
  elseif ($event_id == 'undefined') {
    // This is a new event, save it.
    $event = bat_event_create(array('type' => $bat_event_data['type']));
    $event->start_date = $bat_event_data['start_date'];
    $event->end_date = $bat_event_data['end_date'];
    $event->uid = (isset($bat_event_data['uid'])) ? $bat_event_data['uid'] : 0;
acrollet's avatar
acrollet committed
978
    $event->data = (isset($bat_event_data['data'])) ? $bat_event_data['data'] : NULL;
Nicolò Caruso's avatar
Nicolò Caruso committed
979 980

    $event->created = REQUEST_TIME;
acrollet's avatar
acrollet committed
981 982
    $event->changed = REQUEST_TIME;

983 984 985 986 987 988
    // Set new target_id if present.
    if (isset($bat_event_data['target_id'])) {
      $event_type = bat_event_type_load($event->type);
      // Construct target entity reference field name using this event type's target entity type.
      $target_field_name = 'event_' . $event_type->target_entity_type . '_reference';
      $event->{$target_field_name} = array(
acrollet's avatar
acrollet committed
989 990
        LANGUAGE_NONE => array(
          0 => array(
991
            'target_id' => $bat_event_data['target_id'],
acrollet's avatar
acrollet committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
          )
        )
      );
    }

    // Set new state id if present.
    if (isset($bat_event_data['state_id'])) {
      $event->event_state_reference = array(
        LANGUAGE_NONE => array(
          0 => array(
            'state_id' => $bat_event_data['state_id'],
          )
        )
      );
    }

    $event->save();
Nicolò Caruso's avatar
Nicolò Caruso committed
1009

acrollet's avatar
acrollet committed
1010
    $return->status = 1;
acrollet's avatar
acrollet committed
1011 1012 1013 1014
  }

  return $return;
}
1015

Nicolò Caruso's avatar
Nicolò Caruso committed
1016
/**
1017
 * Merge non blocking events so that we display them as a continuous single event.
Nicolò Caruso's avatar
Nicolò Caruso committed
1018 1019 1020 1021 1022 1023 1024 1025 1026
 *
 * @param $events array
 *
 * @return array
 */
function _bat_api_merge_non_blocking_events($events) {
  $prev_id = FALSE;

  foreach ($events as $id => $event) {
Nicolò Caruso's avatar
Nicolò Caruso committed
1027
    if ($prev_id !== FALSE && isset($event['rendering']) && $event['rendering'] == 'background' && !$event['blocking']) {
Nicolò Caruso's avatar
Nicolò Caruso committed
1028
      $last_event = $events[$prev_id];
Nicolò Caruso's avatar
Nicolò Caruso committed
1029
      if (isset($last_event['rendering']) && $last_event['rendering'] == 'background' && !$last_event['blocking']) {
Nicolò Caruso's avatar
Nicolò Caruso committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
        if ($last_event['resourceId'] == $event['resourceId'] &&
            $last_event['title'] == $event['title'] &&
            $last_event['color'] == $event['color']) {
          $events[$prev_id]['end'] = $event['end'];

          unset($events[$id]);
          continue;
        }
      }
    }

    $prev_id = $id;
  }

  return $events;
1045
}
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141