Skip to content

Instantly share code, notes, and snippets.

@napak
Forked from manviny/PwAngular.module
Last active August 29, 2015 14:10
Show Gist options
  • Save napak/7fcf40f2644cb1d2a2b9 to your computer and use it in GitHub Desktop.
Save napak/7fcf40f2644cb1d2a2b9 to your computer and use it in GitHub Desktop.
<?php
/**
* ProcessWire 'Hello world' demonstration module
*
* Demonstrates the Module interface and how to add hooks.
*
* See README file for further links regarding module development.
*
* ProcessWire 2.x
* Copyright (C) 2014 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
class PwAngular extends WireData implements Module {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
// The module'ss title, typically a little more descriptive than the class name
'title' => 'PW and AngularJS',
// version number
'version' => 3,
// summary is brief description of what this module is
'summary' => '<script> app.controller("controllerCtrl", function ($scope) { }); </script> <div ng-controller="controllerCtrl"> ... </div>',
// Optional URL to more information about the module
'href' => 'http://processwire.com',
// singular=true: indicates that only one instance of the module is allowed.
// This is usually what you want for modules that attach hooks.
'singular' => true,
// autoload=true: indicates the module should be started with ProcessWire.
// This is necessary for any modules that attach runtime hooks, otherwise those
// hooks won't get attached unless some other code calls the module on it's own.
// Note that autoload modules are almost always also 'singular' (seen above).
'autoload' => true,
// Optional font-awesome icon name, minus the 'fa-' part
'icon' => 'smile-o',
);
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init() {
// add a hook after each page is rendered and modify the output
$this->addHookAfter('Page::render', $this, 'addAngular');
$this->addHook('Page::getPage', $this, 'getPage');
$this->addHook('Page::getChildren', $this, 'getChildren');
}
/**
* addAngular
*
*/
public function addAngular($event) {
$page = $event->object;
// don't add this to the admin pages
if($page->template == 'admin') return;
// 1.- add angular
$angular = '<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>';
$angular .= '<script src="https://code.angularjs.org/1.3.2/angular-sanitize.min.js"></script>';
$angular .= "<script>var app = angular.module('myApp', ['ngSanitize'])</script>";
$angular .= "</head>";
$event->return = str_replace("</head>", $angular, $event->return);
// 2.- add ng-app="myApp" to body tag
$body = "<body";
$bodypos = strpos($body, "<body");
$bodypos_end = $bodypos + strlen("<body");
$newbody = substr_replace($body, ' ng-app="myApp" ', $bodypos_end, 0);
$event->return = str_replace("<body", $newbody, $event->return);
// 3.- add $page data to var = page (php -> javascript)
// $script =
// "<script>".
// "app.controller('especialidadesCtrl', function (\$scope) {".
// "\$scope.children = [];".
// // "$scope.children = ". wire('page')->children.
// "\$scope.children = 'hi there' ".
// "});".
// "</script>".
// "</body>";
// $event->return = str_replace("</body>", $script, $event->return);
}
public function getPage($event) {
$pagina = $event->object;
$array = array();
// fields to be avoided
$avoid = array("FieldtypeFieldsetOpen", "FieldtypeFieldsetClose","FieldtypeFieldsetTabOpen","FieldtypeFieldsetTabClose");
$array['id'] = $pagina->id;
foreach($pagina->fields as $field) {
if (!in_array($field->type, $avoid)) {
// si el campo no esta vacio
if( htmlspecialchars($pagina->get($field->name)) )
if($field->type == "FieldtypePage"){
$buscar = htmlspecialchars($pagina->get($field->name));
$paginas = $pages->get($buscar);
$array[$field->name] = $paginas->title;
}
else {
$array[$field->name] = htmlspecialchars($pagina->get($field->name));
}
}
}
$event->return = json_encode($array);
}
public function getChildren($event) {
$pagina = $event->object;
// fields to be avoided
$avoid = array("FieldtypeFieldsetOpen", "FieldtypeFieldsetClose","FieldtypeFieldsetTabOpen","FieldtypeFieldsetTabClose");
// fields that must be returned
$wanted = $fields;
// selector
$paginas = $pagina->children;
$arr = array();
foreach ($paginas as $child) {
$array = array();
foreach($child->fields as $field) {
$array['id'] = $child->id;
// if we dont' want all fields back
if ( !in_array($field->type, $avoid) && in_array($field->name, $wanted) && (count($wanted)>0) ) {
$array[$field->name] = htmlspecialchars($child->get($field->name));
}
// we want all fields back
if ( !in_array($field->type, $avoid) && (count($wanted)==0) ) {
$array[$field->name] = htmlspecialchars($child->get($field->name));
}
}
array_push($arr, $array);
}
$event->return = json_encode($arr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment