Grima  2018-08
Whispering into Alma's ear
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
GrimaTask Class Reference

The base class for most "grimas". More...

Inheritance diagram for GrimaTask:

Public Member Functions

 offsetExists ($offset)
 
 offsetGet ($offset)
 
 offsetSet ($offset, $value)
 
 offsetUnset ($offset)
 
 __construct ()
 
 setup_splat ()
 
 print_form ()
 
 print_success ()
 
 print_failure ()
 
 check_login ()
 
 addMessage ($type, $message)
 
 run ()
 
 get_input ()
 
 check_input ()
 
 get_input_param ($param)
 
 do_task ()
 
 usage ()
 

Static Public Member Functions

static RunIt ()
 
static call ($grimaname, $args=array())
 

Public Attributes

 $error = false
 
 $args = array()
 
 $el_override = array()
 
 $auto_args = array()
 
 $messages = array()
 

Detailed Description

The base class for most "grimas".

Definition at line 1174 of file grima-lib.php.

Constructor & Destructor Documentation

◆ __construct()

GrimaTask::__construct ( )

Definition at line 1200 of file grima-lib.php.

References join_paths().

1200  {
1201  $webroot = __DIR__;
1202  $base = get_class($this); //basename($_SERVER['PHP_SELF'],'.php');
1203  if (file_exists(join_paths($webroot,$base,"$base.xml")) and (!isset($this->formxml))) {
1204  $this->formxml = file_get_contents(join_paths($webroot,$base,"$base.xml"));
1205  }
1206  if (isset($this->formxml)) {
1207  $this->form = new GrimaForm();
1208  $this->form->fromXML($this->formxml);
1209  }
1210  }
A holder for the user inputs to a grima, base on the XML file.
Definition: grima-lib.php:1428
join_paths(... $paths)
Definition: grima-util.php:114

Member Function Documentation

◆ addMessage()

GrimaTask::addMessage (   $type,
  $message 
)

Definition at line 1264 of file grima-lib.php.

Referenced by run().

1264  {
1265  $this->messages[] = new GrimaTaskMessage($type,$message);
1266  }
A thin wrapper around a message and urgency level.
Definition: grima-lib.php:1411

◆ call()

static GrimaTask::call (   $grimaname,
  $args = array() 
)
static

Definition at line 1398 of file grima-lib.php.

References $args, and do_redirect().

1398  {
1399  $url = rtrim("../$grimaname/$grimaname.php?" . http_build_query($args),"?");
1400  do_redirect($url);
1401  }
do_redirect($url)
Definition: grima-util.php:45

◆ check_input()

GrimaTask::check_input ( )

Definition at line 1325 of file grima-lib.php.

Referenced by get_input_param(), and run().

1325  {
1326  if (isset($this->form)) {
1327  $input_good = true;
1328  foreach ($this->form->fields as $field) {
1329  if ($field->required) {
1330  if (!isset($this[$field->name]) or
1331  !($this[$field->name])) {
1332  $field->error_condition = "error";
1333  $field->error_message = "Field is required\n";
1334  $input_good = false;
1335  }
1336  }
1337  }
1338  return $input_good;
1339  } else {
1340 
1341  foreach ($this->auto_args as $k => $v) {
1342  if (preg_match('/[^:]:$/',$k)) {
1343  if (!isset($this->args[$v]) or !($this->args[$v])) {
1344  return false;
1345  }
1346  }
1347  }
1348  return true;
1349  }
1350  }

◆ check_login()

GrimaTask::check_login ( )

Definition at line 1252 of file grima-lib.php.

References $grima, and do_redirect().

Referenced by run().

1252  {
1253  global $grima;
1254  if (isset($grima->apikey) and isset($grima->server) and
1255  ($grima->apikey) and ($grima->server)) {
1256  return true;
1257  } else {
1258  do_redirect('../Login/Login.php?redirect_url=' . urlencode($_SERVER['PHP_SELF']));
1259  exit;
1260  return false;
1261  }
1262  }
do_redirect($url)
Definition: grima-util.php:45
$grima
Definition: grima-lib.php:1166

◆ do_task()

GrimaTask::do_task ( )
abstract

Referenced by run().

◆ get_input()

GrimaTask::get_input ( )

Definition at line 1296 of file grima-lib.php.

References get_input_param().

Referenced by run().

1296  {
1297  if (isset($this->form)) {
1298  if (php_sapi_name() == "cli") { # command line
1299  /*
1300  if ($options = getopt(implode(array_keys($param)))) {
1301  foreach ($param as $k => $v) {
1302  $this->args[$v] = $options[$k[0]];
1303  }
1304  if (!$this->check_input()) {
1305  $this->usage(); exit;
1306  }
1307  } else {
1308  $this->usage(); exit;
1309  }
1310  */
1311  } else { # web
1312  foreach ($this->form->fields as $field) {
1313  if (isset($_REQUEST[$field->name])) {
1314  $this[$field->name] = $_REQUEST[$field->name];
1315  /* sanitize */
1316  }
1317  }
1318  }
1319 
1320  } else {
1321  $this->get_input_param($this->auto_args);
1322  }
1323  }
get_input_param($param)
Definition: grima-lib.php:1352

◆ get_input_param()

GrimaTask::get_input_param (   $param)

Definition at line 1352 of file grima-lib.php.

References check_input(), print_form(), and usage().

Referenced by get_input().

1352  {
1353  if (php_sapi_name() == "cli") { # command line
1354  if ($options = getopt(implode(array_keys($param)))) {
1355  foreach ($param as $k => $v) {
1356  $this->args[$v] = $options[$k[0]];
1357  }
1358  if (!$this->check_input()) {
1359  $this->usage(); exit;
1360  }
1361  } else {
1362  $this->usage(); exit;
1363  }
1364  } else { # web
1365  foreach ($param as $k => $v) {
1366  if (isset($_REQUEST[$v])) {
1367  $this->args[$v] = $_REQUEST[$v];
1368  }
1369  }
1370  if (!$this->check_input()) {
1371  $this->print_form(); exit;
1372  }
1373  }
1374  }

◆ offsetExists()

GrimaTask::offsetExists (   $offset)

Definition at line 1184 of file grima-lib.php.

1184  {
1185  return isset($this->args[$offset]);
1186  }

◆ offsetGet()

GrimaTask::offsetGet (   $offset)

Definition at line 1188 of file grima-lib.php.

1188  {
1189  return $this->args[$offset];
1190  }

◆ offsetSet()

GrimaTask::offsetSet (   $offset,
  $value 
)

Definition at line 1192 of file grima-lib.php.

1192  {
1193  $this->args[$offset] = $value;
1194  }

◆ offsetUnset()

GrimaTask::offsetUnset (   $offset)

Definition at line 1196 of file grima-lib.php.

1196  {
1197  unset($this->args[$offset]);
1198  }

◆ print_failure()

GrimaTask::print_failure ( )

Definition at line 1247 of file grima-lib.php.

Referenced by run().

1247  {
1248  $this->form->loadValues($this);
1249  $this->splat->splat('print_failure', $this->splatVars );
1250  }

◆ print_form()

GrimaTask::print_form ( )

Definition at line 1237 of file grima-lib.php.

Referenced by get_input_param(), and run().

1237  {
1238  $this->form->loadValues($this);
1239  $this->splat->splat('print_form', $this->splatVars );
1240  }

◆ print_success()

GrimaTask::print_success ( )

Definition at line 1242 of file grima-lib.php.

Referenced by run().

1242  {
1243  $this->form->loadPersistentValues($this);
1244  $this->splat->splat('print_success', $this->splatVars );
1245  }

◆ run()

GrimaTask::run ( )

Definition at line 1268 of file grima-lib.php.

References addMessage(), check_input(), check_login(), do_task(), get_input(), print_failure(), print_form(), print_success(), and setup_splat().

1268  {
1269  $this->check_login(); # if not logged in, print login form
1270  $this->error = false;
1271  $this->get_input();
1272  $this->setup_splat(); # should happen after form xml read in get_input
1273  if ($this->check_input()) {
1274  try {
1275  $this->do_task();
1276  } catch (Exception $e) {
1277  $this->addMessage('error',$e->getMessage());
1278  $this->error = true;
1279  }
1280  } else {
1281  $this->print_form();
1282  exit;
1283  }
1284  if ($this->error) {
1285  $this->print_failure();
1286  } else {
1287  $this->print_success();
1288  }
1289  }
addMessage($type, $message)
Definition: grima-lib.php:1264

◆ RunIt()

static GrimaTask::RunIt ( )
static

Definition at line 1291 of file grima-lib.php.

1291  {
1292  $task = new static();
1293  $task->run();
1294  }

◆ setup_splat()

GrimaTask::setup_splat ( )

Definition at line 1212 of file grima-lib.php.

Referenced by run().

1212  {
1213  $webroot = __DIR__;
1214  $basename = get_class($this); //basename($_SERVER['PHP_SELF'],'.php');
1215 
1216  $this->splat = new zemowsplat\Splat();
1217 
1218  $this->splat->addBases(array(
1219  "$webroot/$basename/splats", // per-task overrides
1220  "$webroot/splats", // default
1221  ));
1222 
1223  $this->splatVars = array(
1224  'title' => $this->form->title,
1225  'basename' => $basename,
1226  'webroot' => $webroot,
1227  'local_stylesheets' => array('Grima.css'),
1228  'form' => &$this->form,
1229  'messages' => &$this->messages,
1230  'width' => 9,
1231  );
1232  if (isset($this['redirect_url'])) {
1233  $this->splatVars['redirect_url'] = $this['redirect_url'];
1234  }
1235  }
Simple template engine.

◆ usage()

GrimaTask::usage ( )

Definition at line 1378 of file grima-lib.php.

Referenced by get_input_param().

1378  { # XXX rewrite for grima form
1379  global $argv;
1380  print "Usage: php ${argv[0]} ";
1381  foreach ($this->auto_args as $k => $v) {
1382  if (preg_match('/^(.):$/',$k,$m)) {
1383  print "-${m[1]} <$v> ";
1384  } else {
1385  if (preg_match('/^(.)::$/',$k,$m)) {
1386  print "[ -${m[1]} <$v> ] ";
1387  } else {
1388  if (preg_match('/^.$/',$k)) {
1389  print "[ -$k ] ";
1390  }
1391  }
1392  }
1393  }
1394  print "\n";
1395  exit;
1396  }

Member Data Documentation

◆ $args

GrimaTask::$args = array()

Definition at line 1177 of file grima-lib.php.

Referenced by call().

◆ $auto_args

GrimaTask::$auto_args = array()

Definition at line 1180 of file grima-lib.php.

◆ $el_override

GrimaTask::$el_override = array()

Definition at line 1178 of file grima-lib.php.

◆ $error

GrimaTask::$error = false

Definition at line 1176 of file grima-lib.php.

◆ $messages

GrimaTask::$messages = array()

Definition at line 1182 of file grima-lib.php.


The documentation for this class was generated from the following file: