ElementImageOutputStrategy.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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();
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 ?>
getType()
Get the type of the object.
getOID()
Get the object id of the PersistentObject.
getValueNames($excludeTransient=false)
Get the names of all items.
getProperty($name)
Get the value of a named property in the object.
drawConnectionLine($poid, $oid)
Draw connection line.
__construct($format, $file, $map, $lineType=self::LINETYPE_DIRECT, $scale=100, $aspect=0.5, $border=50, $usemap='')
Constructor.
ElementImageOutputStrategy outputs a tree of objects into an image file.
The Position class stores a coordinate tuple for use with the LayoutVisitor.
Definition: Position.php:19
ImageOutputStrategy outputs a tree of objects into an image file.
PersistentObject defines the interface of all persistent objects.