-
Notifications
You must be signed in to change notification settings - Fork 0
/
motionsplan_exercise_pdf.module
628 lines (562 loc) · 17.5 KB
/
motionsplan_exercise_pdf.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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
468
469
470
471
472
473
474
475
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
<?php
/**
* @file
* Code for the PDF for the exercises and exercise programs
*/
include_once 'motionsplan_exercise_pdf.features.inc';
function motionsplan_exercise_pdf_libraries_info() {
$libraries['tcpdf'] = array(
'name' => 'TCPDF',
'vendor url' => 'http://www.tcpdf.org',
'download url' => 'http://sourceforge.net/projects/tcpdf/files/',
'version arguments' => array(
'file' => 'tcpdf.php',
'pattern' => '/\Version : ((\d+)\.(\d+)\.(\d+))/',
),
'files' => array(
'php' => array(
'tcpdf.php',
),
),
);
$libraries['fpdi'] = array(
'name' => 'FPDI',
'vendor url' => 'http://www.setasign.com/products/fpdi',
'download url' => 'http://www.setasign.com/products/fpdi/downloads',
'files' => array(
'php' => array(
'fpdi.php',
),
),
'integration files' => array(
'vih_lectures_pdf' => array(
'php' => array('fpdi.php'),
),
),
);
return $libraries;
}
/**
* Implementation of hook_menu()
*/
function motionsplan_exercise_pdf_menu() {
/*
$items['node/%node/pdf'] = array(
'title' => 'PDF',
'page callback' => 'exercise_pdf_landscape',
'page arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'access callback' => 'exercise_pdf_access',
'access arguments' => array(1)
);
*/
$items['node/%node/pdf2'] = array(
'title' => 'PDF2',
'page callback' => 'exercise_pdf_portrait',
'page arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'access callback' => 'exercise_pdf_access',
'access arguments' => array(1)
);
$items['node/%node/pdf3'] = array(
'title' => 'PDF3',
'page callback' => 'exercise_pdf_advanced',
'page arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'access callback' => 'exercise_pdf_access',
'access arguments' => array(1)
);
$items['node/%node/print'] = array(
'title' => 'PDF',
'page callback' => 'article_pdf',
'page arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'access callback' => 'article_pdf_access',
'access arguments' => array(1)
);
$items['node/%node/pdf-compact'] = array(
'title' => t('PDF'),
'page callback' => 'exerciseprogram_print_compact_pdf',
'page arguments' => array(1),
'access callback' => 'exerciseprogram_pdf_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/pdf-all-cards'] = array(
'title' => t('Exercise PDF cards'),
'page callback' => 'exerciseprogram_print_pdf_cards_exercises',
'page arguments' => array(1),
'access callback' => 'exerciseprogram_pdf_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
$items['shuffle-pdf'] = array(
'title' => t('PDF with categories'),
'page callback' => 'workout_shuffle_categories',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
$items['shuffle-pdf-compact'] = array(
'title' => t('PDF with categories'),
'page callback' => 'workout_shuffle_categories_compact',
'page arguments' => array(1),
'access callback' => array(1),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_theme().
*/
function motionsplan_exercise_pdf_theme($existing, $type, $theme, $path) {
return array(
'entity_print__motionsplan_exercise_program' => array(
'base hook' => 'entity_print',
'template' => 'entity-print--motionsplan-exercise-program',
),
);
}
/**
* Checks for access for exercises
*/
function workout_shuffle_pdf_access($term) {
return true;
if ($term->type != 'motionsplan_activity_category') {
return FALSE;
}
return TRUE;
}
/**
* Checks for access for exercises
*/
function exercise_pdf_access($node) {
if ($node->type != 'exercise') {
return FALSE;
}
return TRUE;
}
/**
* Checks for access for exercise programs
*/
function exerciseprogram_pdf_access($node) {
if ($node->type != 'trainingprogram') {
return FALSE;
}
return TRUE;
}
/**
* Checks for access for articles
*/
function article_pdf_access($node) {
if ($node->type != 'story') {
return FALSE;
}
return TRUE;
}
/**
* Plugin for the PDF printouts.
*
* Implements hook_ctools_plugin_type().
*
* Has plenty options. See ctools/help/plugins-creating.html
*/
function motionsplan_exercise_pdf_ctools_plugin_type() {
$plugins['layouts'] = array(
'use hooks' => TRUE,
);
return $plugins;
}
/**
* Implements hook_ctools_plugin_directory().
*/
function motionsplan_exercise_pdf_ctools_plugin_directory($owner, $plugin) {
if (($owner == 'motionsplan_exercise_pdf') && ($plugin == 'layouts')) {
return 'plugins/' . $plugin;
}
}
/**
* Fetch metadata on a specific layout plugin.
*
* @param string $layout
* Name of a panel layout. If the layout name contains a ':' this
* indicates that we need to separate the sublayout out and
* load it individually.
*
* @return array
* An array with information about the requested panel layout.
*/
function motionsplan_exercise_pdf_get_layout($layout) {
ctools_include('plugins');
return ctools_get_plugins('motionsplan_exercise_pdf', 'layouts', $layout);
}
/**
* Fetch metadata for all layout plugins.
*
* @param
* string $id Optional plugin ID
*
* @return array
* An array of arrays with information about all available layouts.
*/
function motionsplan_exercise_pdf_get_layouts($id = NULL) {
ctools_include('plugins');
return ctools_get_plugins('motionsplan_exercise_pdf', 'layouts', $id);
}
/**
* Returns pdf layouts plugin.
*/
function motionsplan_exercise_pdf_layouts() {
return variable_get('motionsplan_exercise_pdf_layout', 'landscape');
}
/**
* Implements hook_admin_settings().
*/
function motionsplan_exercise_pdf_admin_settings() {
global $base_url;
$options = array();
foreach (motionsplan_exercise_pdf_get_layouts() as $key => $layout) {
$options[$key] = $key;
}
$form = array();
$form['motionsplan_exercise_pdf']['layout'] = array(
'#type' => 'select',
'#title' => t('Layout'),
'#default_value' => variable_get('motionsplan_exercise_pdf_layout', 'portrait'),
'#options' => $options,
'#description' => t("Which layout do you wish to use?"),
'#required' => TRUE,
);
// Use the #managed_file FAPI element to upload an image file.
$form['motionsplan_exercise_pdf']['logo'] = array(
'#title' => t('Logo'),
'#type' => 'managed_file',
'#description' => t('The uploaded image will be displayed on this page using the image style choosen below.'),
'#default_value' => variable_get('motionsplan_exercise_pdf_logo', ''),
'#upload_location' => 'public://logo/',
);
$form['motionsplan_exercise_pdf']['link'] = array(
'#type' => 'textfield',
'#title' => t('Link'),
'#default_value' => variable_get('motionsplan_exercise_pdf_link', $base_url),
'#description' => t("Link to use for the logo"),
'#required' => TRUE,
);
$form['#submit'][] = 'motionsplan_exercise_pdf_admin_settings_submit';
return system_settings_form($form);
}
/**
* Implements hook_admin_settings_validate().
*/
function motionsplan_exercise_pdf_admin_settings_validate($form, &$form_state) {
return TRUE;
}
/**
* Implements hook_admin_settings_submit().
*/
function motionsplan_exercise_pdf_admin_settings_submit($form, &$form_state) {
variable_set('motionsplan_exercise_pdf_layout', $form_state['values']['layout']);
variable_set('motionsplan_exercise_pdf_link', $form_state['values']['link']);
// @see http://drupal.org/node/1090198.
global $user;
if (is_numeric($form_state['values']['logo']) && $form_state['values']['logo'] > 0) {
// Load the file via file.fid.
$file = file_load($form_state['values']['logo']);
if ($file) {
// Change status to permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save.
file_save($file);
// Save file to variable.
variable_set('motionsplan_exercise_pdf_logo', $file->fid);
// Record that the module (in this example, user module) is using the file.
file_usage_add($file, 'motionsplan_exercise_pdf', 'user', $user->uid);
// Unset formstate value - make sure it is unset before system submit.
unset($form_state['values']['logo']);
}
}
else {
// Load the file via file.fid.
$file = file_load(variable_get('motionsplan_exercise_pdf_logo', ''));
if ($file->fid) {
// Delete the file and the usage record.
file_delete($file, TRUE);
}
variable_set('motionsplan_exercise_pdf_logo', '');
}
}
/**
* Creates a pdf of an article node
*
* @param $nid Node id
*
* @return void
*/
function article_pdf($node) {
global $base_url;
require_once libraries_get_path('tcpdf') . '/tcpdf.php';
ctools_include('motionsplan_exercise_pdf.pdf', 'motionsplan_exercise_pdf', '.');
$title = $node->title;
$pdf = new ArticlePdf($node);
$pdf->SetTitle($title);
$pdf->SetSubject('');
$pdf->SetAuthor('Motionsplan.dk');
$pdf->AddContent();
$pdf->Output($title . '.pdf', 'I');
}
function hex2rgb($hex) {
$hex = str_replace("#", "", $hex);
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = array($r, $g, $b);
//return implode(",", $rgb); // returns the rgb values separated by commas
return $rgb; // returns an array with the rgb values
}
function workout_shuffle_categories() {
global $base_url;
require_once libraries_get_path('fpdf') . '/fpdf.php';
require_once libraries_get_path('tcpdf') . '/tcpdf.php';
ctools_include('motionsplan_exercise_pdf.pdf', 'motionsplan_exercise_pdf', '.');
$vocabulary = taxonomy_vocabulary_machine_name_load('motionsplan_activity_category');
$taxonomy = taxonomy_get_tree($vocabulary->vid, 0, NULL, TRUE);
$pdf = new ShuffleWorkoutPdf();
foreach ($taxonomy as $term) {
$pdf->addTerm($term);
}
$pdf->Output('workoutshuffle.pdf', 'I');
}
function workout_shuffle_categories_compact() {
global $base_url;
require_once libraries_get_path('fpdf') . '/fpdf.php';
require_once libraries_get_path('tcpdf') . '/tcpdf.php';
ctools_include('motionsplan_exercise_pdf.pdf', 'motionsplan_exercise_pdf', '.');
$vocabulary = taxonomy_vocabulary_machine_name_load('motionsplan_activity_category');
$taxonomy = taxonomy_get_tree($vocabulary->vid, 0, NULL, TRUE);
$pdf = new ShuffleWorkoutPdfCompact();
foreach ($taxonomy as $term) {
$pdf->addTerm($term);
}
$pdf->Output('workoutshuffle.pdf', 'I');
}
/**
* Creates a pdf of an exercise node
*
* @param $nid Node id
*
* @return void
*/
function exercise_pdf_portrait($node) {
global $base_url;
if (!($library = libraries_load('tcpdf')) && empty($library['loaded'])) {
die('Library is not loaded');
}
ctools_include('Layout', 'motionsplan_exercise_pdf', 'plugins/layouts/portrait');
$term = taxonomy_term_load($node->field_exercise_category[LANGUAGE_NONE][0]['tid']);
$hex = $term->field_category_color[LANGUAGE_NONE][0]['rgb'];
$rgb = hex2rgb($hex);
$title = utf8_decode($node->title);
$pdf = new ExercisePdfPortrait();
$pdf->setTitleBackgroundColor($rgb);
$pdf->SetTitle($title);
$pdf->SetSubject('');
$pdf->SetAuthor('Motionsplan.dk');
$pdf->AddNewPage($node);
$pdf->Output($title . '.pdf', 'I');
}
/**
* Creates a pdf of an exercise node
*
* @param $nid Node id
*
* @return void
*/
function exercise_pdf_landscape($node) {
global $base_url;
require_once libraries_get_path('tcpdf') . '/tcpdf.php';
ctools_include('Layout', 'motionsplan_exercise_pdf', 'plugins/layouts/landscape');
$title = utf8_decode($node->title);
$pdf = new ExercisePdfLandscape();
$pdf->SetTitle($title);
$pdf->SetSubject('');
$pdf->SetAuthor('Motionsplan.dk');
$pdf->AddNewPage($node);
$pdf->Output($title . '.pdf', 'I');
}
/**
* Creates a pdf of an exercise node
*
* @param $nid Node id
*
* @return void
*/
function exercise_pdf_advanced($node) {
global $base_url;
require_once libraries_get_path('tcpdf') . '/tcpdf.php';
ctools_include('Layout', 'motionsplan_exercise_pdf', 'plugins/layouts/portrait_extended');
$title = utf8_decode($node->title);
$pdf = new ExerciseAdvancedPdfPortrait();
$pdf->SetTitle($title);
$pdf->SetSubject('');
$pdf->SetAuthor('Motionsplan.dk');
$pdf->AddNewPage($node);
$pdf->Output($title . '.pdf', 'I');
}
/**
* Prints A4 of the exercises in a program.
*/
function exerciseprogram_print_pdf_cards_exercises() {
global $base_path;
require_once libraries_get_path('fpdf') . '/fpdf.php';
require_once libraries_get_path('tcpdf') . '/tcpdf.php';
ctools_include('motionsplan_exercise_pdf.pdf', 'motionsplan_exercise_pdf', '.');
ctools_include('Layout', 'motionsplan_exercise_pdf', 'plugins/layouts/portrait');
$node = node_load(arg(1));
$exercises = $node->field_program_exercises[LANGUAGE_NONE];
$nids = array();
foreach ($exercises as $key => $exercise) {
$nids[] = $exercise['target_id'];
}
if (sizeof($nids)<=0) {
return '<strong>Du skal tilføje øvelser for at kunne udskrive</strong>';
}
$pdf = new ExercisePdfPortrait();
$pdf->SetTitle(utf8_decode('Træningsøvelser'));
$pdf->SetSubject('');
$pdf->SetAuthor('Motionsplan.dk');
foreach ($nids as $nid) {
$node = node_load($nid);
$term = taxonomy_term_load($node->field_exercise_category[LANGUAGE_NONE][0]['tid']);
$hex = $term->field_category_color[LANGUAGE_NONE][0]['rgb'];
$rgb = hex2rgb($hex);
$pdf->setTitleBackgroundColor($rgb);
$pdf->AddNewPage($node);
}
$pdf->Output();
}
/**
* Outputs pdf of a compact training program.
*/
function exerciseprogram_print_compact_pdf($node) {
global $base_path;
require_once libraries_get_path('fpdf') . '/fpdf.php';
// HACK START included because Article PDF uses TCPDF
require_once libraries_get_path('tcpdf') . '/tcpdf.php';
// HACK END
ctools_include('motionsplan_exercise_pdf.pdf', 'motionsplan_exercise_pdf', '.');
$title = utf8_decode($node->title);
$description = utf8_decode(strip_tags($node->body[LANGUAGE_NONE][0]['safe_value']));
// $description = check_markup($description, 'filtered_html');
$pdf = new ExerciseProgramCompactPdf();
$pdf->SetTitle($title);
$pdf->SetSubject('');
$pdf->SetAuthor('Motionsplan.dk');
$pdf->SetAutoPageBreak(FALSE);
$pdf->AddPage();
$pdf->SetFont('Helvetica', 'B', 20);
$pdf->Cell(0, 10, $title, null, 2, 'L', FALSE);
$pdf->Image(dirname(__FILE__) . '/mp-logo.png', 150, 8, 50, 0, '', 'http://motionsplan.dk/');
$pdf->setTextColor(0, 0, 0);
$pdf->SetFont('Helvetica', null, 10);
$pdf->MultiCell(180, 6, $description, 0);
// $pdf->writeHTMLCell(180, 6, $pdf->GetX(), $pdf->GetY(), $description, 0);
$pdf->SetY($pdf->GetY()+2);
// Table starts.
$pdf->SetWidths(array(50, 70, 70));
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->Row(array(utf8_decode('Øvelse'), utf8_decode('Beskrivelse'), utf8_decode('Kommentar')));
$pdf->SetFont('Helvetica', null, 10);
$style = 'square_thumbnail';
$style_array = image_style_load($style);
foreach ($node->field_program_exercises[LANGUAGE_NONE] as $entity) {
$e = node_load($entity['target_id']);
$imgs = array();
foreach ($e->field_exercise_images[LANGUAGE_NONE] as $image) {
$dst = image_style_path($style, $image['uri']);
if (file_exists($dst) || image_style_create_derivative($style_array, $image['uri'], $dst)) {
$picture_filename = drupal_realpath(image_style_path($style, $image['uri']));
if (is_dir($picture_filename)) {
continue;
}
$imgs[] = $picture_filename;
}
}
if (sizeof($imgs) > 3) {
$tmp_imgs = array();
$tmp_imgs[] = $imgs[0];
$tmp_imgs[] = $imgs[floor(sizeof($imgs) / 2)];
$tmp_imgs[] = $imgs[sizeof($imgs) - 1];
$imgs = $tmp_imgs;
}
$pdf->Row(array(utf8_decode($e->title), utf8_decode($e->field_exercise_intro[LANGUAGE_NONE][0]['value']), ''), $imgs);
}
$pdf->Output($title . '.pdf', 'I');
}
/**
* Choose your own pdf's.
*
* Implements hook_node_operations().
*/
function motionsplan_exercise_pdf_node_operations() {
$operations = array(
'pdf' => array(
'label' => t('Print PDF (max 10)'),
'callback' => 'node_operations_pdf',
));
return $operations;
}
function motionsplan_exercise_pdf_action_info() {
return array(
'motionsplan_exercise_pdf_print_action' => array(
'type' => 'node',
'label' => t('Generate PDF'),
'behavior' => array('generate_pdf'),
'configurable' => FALSE,
'vbo_configurable' => TRUE,
'triggers' => array('any'),
),
);
}
function motionsplan_exercise_pdf_print_action(&$entity, $context) {
$message = t('Node title is %title. Sincerely, %hero', array(
'%title' => $node->title,
'%hero' => $context['hero'],
));
drupal_set_message($message);
}
/**
* Make it possible to print exercises you choose.
*/
function node_operations_pdf($nodes) {
global $base_url;
$title = 'Træningsøvelser';
$pdf = new ExerciseAdvancedPdfPortrait(FALSE);
$pdf->SetTitle($title);
$pdf->SetSubject('');
$pdf->SetAuthor('Motionsplan.dk');
$i = 0;
$number = 10;
foreach ($nodes as $node_id) {
if ($i >= $number) {
$error = TRUE;
break;
}
$node = node_load($node_id);
if ($node->type != 'exercise') {
continue;
}
$i++;
$pdf->AddNewPage($node);
}
if ($error !== TRUE) {
$pdf->Output($title . '.pdf', 'D');
} else {
drupal_set_message(t('Can only print %number pages at a time', array('%number' => $number)), 'error');
}
}