ElementImageOutputStrategy.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\model\output;
12 
17 
18 /**
19  * ElementImageOutputStrategy outputs a tree of objects into an image file.
20  * It must be configured with a map that was calculated by a LayoutVisitor.
21  *
22  * @author ingo herwig <ingo@wemove.com>
23  */
25 
26  /**
27  * Constructor.
28  * @param $format Image format name [IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP].
29  * @param $file The output file name.
30  * @param $map The position map provided by LayoutVisitor.
31  * @param $lineType The linetype to use [LINETYPE_DIRECT|LINETYPE_ROUTED] DEFAULT LINETYPE_DIRECT.
32  * @param $scale The image scale (will be xscale) DEFAULT 100.
33  * @param $aspect The image aspect (aspect = xscale/yscale) DEFAULT 0.5.
34  * @param $border The image border [px] DEFAULT 50.
35  * @param $usemap Name of the HTML ImageMap to write to stdout ['' means no map] DEFAULT ''.
36  */
37  public function __construct($format, $file, $map, $lineType=self::LINETYPE_DIRECT, $scale=100,
38  $aspect=0.5, $border=50, $usemap='') {
39 
40  parent::__construct($format, $file, $map, $lineType, $scale, $aspect, $border, $usemap);
41  // define label dimensions relative to node connector position
42  $this->labelDim['left'] = -10;
43  $this->labelDim['top'] = -10;
44  $this->labelDim['right'] = 80;
45  $this->labelDim['bottom'] = 45;
46  // define text position relative to node connector position
47  $this->textPos['left'] = -5;
48  $this->textPos['top'] = -8;
49  }
50 
51  /**
52  * @see OutputStrategy::writeHeader
53  */
54  public function writeHeader() {
55  parent::writeHeader();
56  // print legend
57  $color = ImageColorAllocate($this->img, 0, 150, 0);
58  $this->writeFilledBorderedRect(new Position($this->border, $this->border, 0),
59  new Position($this->border+10, $this->border+10, 0), $this->bgColor, $color);
60  ImageString($this->img, 1, $this->border+20, $this->border+2, "optional", $color);
61  $color = $this->txtColor;
62  $this->writeFilledBorderedRect(new Position($this->border, $this->border+20, 0),
63  new Position($this->border+10, $this->border+30, 0), $this->bgColor, $color);
64  ImageString($this->img, 1, $this->border+20, $this->border+22, "required", $color);
65  $color = ImageColorAllocate($this->img, 150, 150, 150);
66  for($i=2; $i>=0; $i--) {
67  $this->writeFilledBorderedRect(new Position($this->border+2*$i, $this->border+40+2*$i, 0),
68  new Position($this->border+10+2*$i, $this->border+50+2*$i, 0), $this->bgColor, $color);
69  }
70  ImageString($this->img, 1, $this->border+20, $this->border+42, "repetitive", $color);
71  }
72 
73  /**
74  * @see OutputStrategy::writeObject
75  */
76  public function writeObject(PersistentObject $obj) {
77  $properties = $obj->getValueProperties($obj->getType());
78  if (!strstr($obj->getType(), '->')) {
79  $smallText = $obj->getType();
80  $bigText = $properties['oid'];
81  $color = $this->txtColor;
82  }
83  else {
84  $smallText = substr ($obj->getType(), 0, strrpos ($obj->getType(), ":"));
85  $bigText = substr (strrchr ($obj->getType(), ":"), 1);
86  $color = ImageColorAllocate($this->img, 150, 150, 150);
87  }
88 
89  $oid = $obj->getOID();
90  $x = $this->map[$oid]->x * $this->xscale - $this->labelDim['left'] + $this->border;
91  $y = $this->map[$oid]->y * $this->yscale - $this->labelDim['top'] + $this->border;
92 
93  if ($obj->getProperty('minOccurs') == 0) { // optional
94  $color = ImageColorAllocate($this->img, 0, 150, 0);
95  }
96 
97  // print box
98  if ($obj->getProperty('maxOccurs') == 'unbounded' || $obj->getProperty('maxOccurs') > 1) {
99  for($i=3; $i>=1; $i--) {
100  $this->writeFilledBorderedRect(new Position($x + $this->labelDim['left']+5*$i, $y + $this->labelDim['top']+5*$i, 0),
101  new Position($x + $this->labelDim['right']+5*$i, $y + $this->labelDim['bottom']+5*$i, 0),
102  $this->bgColor, $color);
103  }
104  }
105  // print label
106  $this->writeFilledBorderedRect(new Position($x + $this->labelDim['left'], $y + $this->labelDim['top'], 0),
107  new Position($x + $this->labelDim['right'], $y + $this->labelDim['bottom'], 0),
108  $this->bgColor, $color);
109  // write text
110  ImageString($this->img, 2,
111  $x + $this->textPos['left'],
112  $y + $this->textPos['top'],
113  $smallText,
114  $color);
115  ImageString($this->img, 1,
116  $x + $this->textPos['left'],
117  $y + $this->textPos['top']+15,
118  "E: ".$properties['data_type'],
119  $color);
120  // write attribs
121  $attribs = $obj->getValueNames(true);
122  $i = 0;
123  if (is_array($attribs)) {
124  foreach ($attribs as $attrib) {
125  ImageString($this->img, 1,
126  $x + $this->textPos['left'],
127  $y + $this->textPos['top']+25+10*$i,
128  "A: ".$attrib,
129  $color);
130  $i++;
131  }
132  }
133  ImageString($this->img, 45,
134  $x + $this->textPos['left']+65,
135  $y + $this->textPos['top']+37,
136  $bigText,
137  $color);
138 
139  // draw line
140  $parent = $obj->getParent();
141  if ($parent) {
142  $this->drawConnectionLine($parent->getOID(), $oid);
143  }
144  // print map
145  if ($this->usemap != '') {
146  echo '<area shape="rect" coords="'.
147  ($x + $this->labelDim['left']).','.
148  ($y + $this->labelDim['top']).','.
149  ($x + $this->labelDim['right']).','.
150  ($y + $this->labelDim['bottom'] + 8*$this->map[$oid]->z).
151  '" onclick="javascript:alert(\'Node OID: '.$obj->getOID().'\')" alt="'.$obj->getOID().'">'."\n";
152  }
153  }
154 
155  private function writeFilledBorderedRect($topleft, $bottomright, $bgcolor, $bordercolor) {
156  ImageFilledRectangle($this->img, $topleft->x, $topleft->y, $bottomright->x, $bottomright->y, $bgcolor);
157  ImageRectangle($this->img, $topleft->x, $topleft->y, $bottomright->x, $bottomright->y, $bordercolor);
158  }
159 }
160 ?>
OutputStrategy defines the interface for classes that write an object's content to a destination (cal...
getType()
Get the type of the object.
The Position class stores a coordinate tuple for use with the LayoutVisitor.
Definition: Position.php:19
__construct($format, $file, $map, $lineType=self::LINETYPE_DIRECT, $scale=100, $aspect=0.5, $border=50, $usemap='')
Constructor.
getValueNames($excludeTransient=false)
Get the names of all attributes.
getOID()
Get the object id of the PersistentObject.
PersistentObject defines the interface of all persistent objects.
drawConnectionLine($poid, $oid)
Draw connection line.
ImageOutputStrategy outputs a tree of objects into an image file.
getProperty($name)
Get the value of a named property in the object.
ElementImageOutputStrategy outputs a tree of objects into an image file.