1: <?php
2: /**
3: * Document for class GeoHead
4: *
5: * PHP Version 5.6
6: *
7: * @category Class
8: * @package Geolib
9: * @author Peter Pitchford <peter@geotonics.com>
10: * @license GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
11: * @link http://geotonics.com/#geolib
12: */
13:
14: /**
15: * Geo - Class to create html head tags
16: *
17: * @category Html_Tag
18: * @package Geolib
19: * @author Peter Pitchford <peter@geotonics.com>
20: * @license GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
21: * @version Release: .1
22: * @link http://geotonics.com/#geolib
23: * @since Class available since Release .1
24: */
25: class GeoHead extends GeoTag
26: {
27: private $title;
28: private $stylesheets;
29: private $scripts;
30: private $headScripts;
31: private $styles;
32: private $media;
33: private $addlTags;
34: private $base;
35: private $tags;
36: private $addHtml;
37:
38: /**
39: * Constructor
40: *
41: * @param string $title Document title
42: * @param string|array $stylesheets Paths to document stylesheets. Can be 1 path or an array
43: * @param string|array $scripts Paths to document scripts. Can be either 1 path or an array
44: * @param string $styles Styles for style tag in head
45: * @param array $metas Content of metatags
46: * @param string|array $addlTags Any additional tags for head
47: * addlTags can be multi or non multi. Examples:
48: * array:$tags["script"][$script]=array("type"=>'text/script');
49: * $addlTags['link'][]=array("rel"=>'goback');
50: * $addlTags['link']=array("rel"=>'goback');
51: * @param string $media Media attribute
52: * @param string $addHtml Any additional html for head
53: */
54: public function __construct(
55: $title = null,
56: $stylesheets = null,
57: $scripts = null,
58: $styles = null,
59: $metas = null,
60: $addlTags = null,
61: $media = null,
62: $addHtml = null
63: ) {
64:
65: $this->setScripts($scripts, 'scripts');
66: $this->setStylesheets($stylesheets);
67: $this->setStyles($styles);
68: $this->setTitle($title);
69: $this->media = $media;
70: $this->setMetas(
71: array(
72: null,
73: "text/html; charset=utf-8",
74: "Content-Type",
75: null
76: )
77: );
78: $this->setMetas($metas);
79:
80: if (isset($addlTags)) {
81: $this->addTags($addlTags);
82: }
83:
84: if (isset($addHtml)) {
85: $this->addHtml($addHtml);
86: }
87:
88: //GeoDebug::db($this->tags, 'thistags');
89: parent::__construct("head");
90: }
91:
92: /**
93: * Set content of style tags inside head
94: *
95: * @param array $styles Each row gets its own style tag
96: *
97: * @return void
98: */
99: public function setStyles($styles)
100: {
101: $this->styles = $styles;
102: }
103:
104: /**
105: * Set content of title tag
106: *
107: * @param text $title document title
108: *
109: * @return void
110: */
111: public function setTitle($title)
112: {
113: if (!$title) {
114: $title = $_SERVER["PHP_SELF"];
115: }
116:
117: $this->title = $title;
118: }
119:
120: /**
121: * Set document metatags
122: *
123: * @param array $metas Content of metatags.
124: * @param bool $replace If true, replace metas. Default is to add $metas to existing metas.
125: *
126: * @return void
127: */
128: public function setMetas($metas, $replace = null)
129: {
130: if ($metas) {
131: $metas = geoMultiArr($metas);
132: foreach ($metas as $key => $meta) {
133: //initialize missing array values
134: for ($i = 0; $i < 4; $i++) {
135: if (!isset($meta[$i])) {
136: $meta[$i] = null;
137: }
138: }
139:
140: if ($meta[0]) {
141: $newMetas[$meta[0]] = $meta;
142: } else {
143: $newMetas[] = $meta;
144: }
145: }
146: $this->setTags($newMetas, "metas", $replace);
147: }
148: }
149:
150: /**
151: * Set links to stylesheets
152: *
153: * @param array $stylesheets hrefs of css files
154: * @param bool $replace If true, replace stylesheets. Default is to add $stylesheets to existing metas.
155: *
156: * @return void
157: */
158: public function setStylesheets($stylesheets, $replace = null)
159: {
160: $this->setTags($stylesheets, 'stylesheets', $replace);
161: }
162:
163: /**
164: * Set scripts
165: *
166: * @param array $scripts hrefs of script files
167: * @param bool $replace If true, replace script hrefs. Default is to add $scripts to existing scripts.
168: * @param bool $isForHead If true, script tags will be added to the head.
169: * Default is to add script tags to the end of the body
170: *
171: * @return void
172: */
173: public function setScripts($scripts, $replace = null, $isForHead = null)
174: {
175: if ($isForHead) {
176: $this->setTags($scripts, 'headScripts', $replace);
177: } else {
178: $this->setTags($scripts, 'scripts', $replace);
179: }
180: }
181:
182: /**
183: * Set head tags
184: *
185: * @param array $tags Content of head tags.
186: * @param string $name Tag to put content into.
187: * @param bool $replace If true, replace $name tags. Default is to add $name tags to existing $name tags.
188: *
189: * @return void
190: */
191: private function setTags($tags, $name = null, $replace = null)
192: {
193: if ($tags) {
194: $tags = Geo::arr($tags);
195: if ($replace || !isset($this->$name)) {
196: $this->$name = $tags;
197: } else {
198: $this->$name = array_merge($this->$name, $tags);
199: }
200: }
201: }
202:
203: /**
204: * Set Base
205: *
206: * @param array $base Set document base
207: *
208: * @return void
209: */
210: public function setBase($base)
211: {
212: $this->base = $base;
213: }
214:
215: /**
216: * Get scripts. Sets scripts for the head or the end of the body via geohtmlObj
217: *
218: * @param bool $getHeadScripts Indicates whether to get scripts indended for the head
219: * or the end of the body. Default is the end of the body.
220: *
221: * @return string $scripts Script tags
222: */
223: public function scripts($getHeadScripts = null)
224: {
225: if ($getHeadScripts) {
226: $scriptArr = $this->headScripts;
227: } else {
228: $scriptArr = $this->scripts;
229: }
230:
231: if ($scriptArr) {
232: $scripts='';
233:
234: foreach ($scriptArr as $URL) {
235: $scripts .= geoScript($URL);
236: }
237:
238: return $scripts;
239: }
240: }
241:
242: /**
243: * Add tags to the head
244: *
245: * @param array|string $addlTags Additional tags for the head.
246: * Can be a 2 D array in the form of $array($tagname=>$tagContentArray)
247: * Can be an 1 D array in the form of $array($tagname=>$tagContentString)
248: * Can be a string. String is assumed to be the content of a script tag
249: *
250: * @return string $scripts Script tags
251: */
252: public function addTags($addlTags)
253: {
254: if ($addlTags) {
255: if (is_array($addlTags)) {
256: foreach ($addlTags as $tagName => $tags) {
257: if (geoIsMultiArr($tags)) {
258: foreach ($tags as $text => $atts) {
259: $this->tags[$tagName][$text] = $atts;
260: }
261: } else {
262: $this->tags[$tagName][] = $tags;
263: }
264: }
265: } else {
266: // assume its javascript for a script tag
267: $this->tags['script'][$addlTags] = array(
268: "type" => "text/javascript"
269: );
270: }
271: }
272: }
273:
274: /**
275: * Add Html to head
276: *
277: * @param string $html Html to be added to head
278: *
279: * @return void
280: */
281: public function addHtml($html)
282: {
283: $this->addHtml = $html;
284: }
285:
286: /**
287: * HTML head tag
288: *
289: * @return string head tag
290: */
291: public function tag()
292: {
293: if ($this->title) {
294: $this->text = geoTag('title', $this->title);
295: }
296:
297: if ($this->metas) {
298: foreach ($this->metas as $meta) {
299: $this->tags['meta'][] = array(
300: "name" => $meta[0],
301: "content" => $meta[1],
302: "http-equiv" => $meta[2],
303: "scheme" => $meta[3]
304: );
305: }
306: }
307:
308: if ($this->stylesheets) {
309: foreach ($this->stylesheets as $cssUrl) {
310: $this->tags['link'][] = array(
311: 'rel' => 'stylesheet',
312: 'type' => 'text/css',
313: 'href' => $cssUrl
314: );
315: }
316: }
317:
318: if ($this->styles) {
319: $this->tags['style'][$this->styles] = array(
320: 'type' => 'text/css',
321: "media" => $this->media
322: );
323: }
324:
325: $this->text .= $this->scripts(true);
326:
327: if ($this->base) {
328: $this->tags['base'][] = array(
329: 'target' => $this->base
330: );
331: }
332:
333: $this->tags["link"][] = array(
334: "rel" => "SHORTCUT ICON",
335: "href" => "/images/favicon.ico"
336: );
337:
338: foreach ($this->tags as $name => $arr) {
339: foreach ($arr as $text => $atts) {
340: $this->text .= geoTag($name, $text, null, null, null, $atts);
341: }
342: }
343:
344: if ($this->addHtml) {
345: $this->text .= $this->addHtml;
346: }
347:
348: return parent::baseTag();
349: }
350: }
351: