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 1306 of file grima-lib.php.

Constructor & Destructor Documentation

◆ __construct()

GrimaTask::__construct ( )

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

References join_paths().

1332  {
1333  $webroot = __DIR__;
1334  $base = get_class($this); //basename($_SERVER['PHP_SELF'],'.php');
1335  if (file_exists(join_paths($webroot,$base,"$base.xml")) and (!isset($this->formxml))) {
1336  $this->formxml = file_get_contents(join_paths($webroot,$base,"$base.xml"));
1337  }
1338  if (isset($this->formxml)) {
1339  $this->form = new GrimaForm();
1340  $this->form->fromXML($this->formxml);
1341  }
1342  }
A holder for the user inputs to a grima, base on the XML file.
Definition: grima-lib.php:1560
join_paths(... $paths)
Definition: grima-util.php:156

Member Function Documentation

◆ addMessage()

GrimaTask::addMessage (   $type,
  $message 
)

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

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

◆ call()

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

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

References do_redirect().

Referenced by check_login().

1530  {
1531  $url = rtrim("../$grimaname/$grimaname.php?" . http_build_query($args),"?");
1532  do_redirect($url);
1533  }
do_redirect($url)
Definition: grima-util.php:45

◆ check_input()

GrimaTask::check_input ( )

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

1457  {
1458  if (isset($this->form)) {
1459  $input_good = true;
1460  foreach ($this->form->fields as $field) {
1461  if ($field->required) {
1462  if (!isset($this[$field->name]) or
1463  !($this[$field->name])) {
1464  $field->error_condition = "error";
1465  $field->error_message = "Field is required\n";
1466  $input_good = false;
1467  }
1468  }
1469  }
1470  return $input_good;
1471  } else {
1472 
1473  foreach ($this->auto_args as $k => $v) {
1474  if (preg_match('/[^:]:$/',$k)) {
1475  if (!isset($this->args[$v]) or !($this->args[$v])) {
1476  return false;
1477  }
1478  }
1479  }
1480  return true;
1481  }
1482  }

◆ check_login()

GrimaTask::check_login ( )

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

References $grima, and call().

1384  {
1385  global $grima;
1386  if (isset($grima->apikey) and isset($grima->server) and
1387  ($grima->apikey) and ($grima->server)) {
1388  return true;
1389  } else {
1390  GrimaTask::call('Login',array("redirect_url"=>$_SERVER['PHP_SELF']));
1391  exit;
1392  return false;
1393  }
1394  }
$grima
Definition: grima-lib.php:4293
static call($grimaname, $args=array())
Definition: grima-lib.php:1530

◆ do_task()

GrimaTask::do_task ( )
abstract

◆ get_input()

GrimaTask::get_input ( )

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

1428  {
1429  if (isset($this->form)) {
1430  if (php_sapi_name() == "cli") { # command line
1431  /*
1432  if ($options = getopt(implode(array_keys($param)))) {
1433  foreach ($param as $k => $v) {
1434  $this->args[$v] = $options[$k[0]];
1435  }
1436  if (!$this->check_input()) {
1437  $this->usage(); exit;
1438  }
1439  } else {
1440  $this->usage(); exit;
1441  }
1442  */
1443  } else { # web
1444  foreach ($this->form->fields as $field) {
1445  if (isset($_REQUEST[$field->name])) {
1446  $this[$field->name] = $_REQUEST[$field->name];
1447  /* sanitize */
1448  }
1449  }
1450  }
1451 
1452  } else {
1453  $this->get_input_param($this->auto_args);
1454  }
1455  }
get_input_param($param)
Definition: grima-lib.php:1484

◆ get_input_param()

GrimaTask::get_input_param (   $param )

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

1484  {
1485  if (php_sapi_name() == "cli") { # command line
1486  if ($options = getopt(implode(array_keys($param)))) {
1487  foreach ($param as $k => $v) {
1488  $this->args[$v] = $options[$k[0]];
1489  }
1490  if (!$this->check_input()) {
1491  $this->usage(); exit;
1492  }
1493  } else {
1494  $this->usage(); exit;
1495  }
1496  } else { # web
1497  foreach ($param as $k => $v) {
1498  if (isset($_REQUEST[$v])) {
1499  $this->args[$v] = $_REQUEST[$v];
1500  }
1501  }
1502  if (!$this->check_input()) {
1503  $this->print_form(); exit;
1504  }
1505  }
1506  }
check_input()
Definition: grima-lib.php:1457
usage()
Definition: grima-lib.php:1510
print_form()
Definition: grima-lib.php:1369

◆ offsetExists()

GrimaTask::offsetExists (   $offset )

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

1316  {
1317  return isset($this->args[$offset]);
1318  }

◆ offsetGet()

GrimaTask::offsetGet (   $offset )

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

1320  {
1321  return $this->args[$offset];
1322  }

◆ offsetSet()

GrimaTask::offsetSet (   $offset,
  $value 
)

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

1324  {
1325  $this->args[$offset] = $value;
1326  }

◆ offsetUnset()

GrimaTask::offsetUnset (   $offset )

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

1328  {
1329  unset($this->args[$offset]);
1330  }

◆ print_failure()

GrimaTask::print_failure ( )

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

1379  {
1380  $this->form->loadValues($this);
1381  $this->splat->splat('print_failure', $this->splatVars );
1382  }

◆ print_form()

GrimaTask::print_form ( )

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

1369  {
1370  $this->form->loadValues($this);
1371  $this->splat->splat('print_form', $this->splatVars );
1372  }

◆ print_success()

GrimaTask::print_success ( )

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

1374  {
1375  $this->form->loadPersistentValues($this);
1376  $this->splat->splat('print_success', $this->splatVars );
1377  }

◆ run()

GrimaTask::run ( )

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

1400  {
1401  $this->check_login(); # if not logged in, print login form
1402  $this->error = false;
1403  $this->get_input();
1404  $this->setup_splat(); # should happen after form xml read in get_input
1405  if ($this->check_input()) {
1406  try {
1407  $this->do_task();
1408  } catch (Exception $e) {
1409  $this->addMessage('error',$e->getMessage());
1410  $this->error = true;
1411  }
1412  } else {
1413  $this->print_form();
1414  exit;
1415  }
1416  if ($this->error) {
1417  $this->print_failure();
1418  } else {
1419  $this->print_success();
1420  }
1421  }
get_input()
Definition: grima-lib.php:1428
check_input()
Definition: grima-lib.php:1457
setup_splat()
Definition: grima-lib.php:1344
do_task()
check_login()
Definition: grima-lib.php:1384
print_success()
Definition: grima-lib.php:1374
print_form()
Definition: grima-lib.php:1369
addMessage($type, $message)
Definition: grima-lib.php:1396
print_failure()
Definition: grima-lib.php:1379

◆ RunIt()

static GrimaTask::RunIt ( )
static

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

1423  {
1424  $task = new static();
1425  $task->run();
1426  }

◆ setup_splat()

GrimaTask::setup_splat ( )

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

1344  {
1345  $webroot = __DIR__;
1346  $basename = get_class($this); //basename($_SERVER['PHP_SELF'],'.php');
1347 
1348  $this->splat = new zemowsplat\Splat();
1349 
1350  $this->splat->addBases(array(
1351  "$webroot/$basename/splats", // per-task overrides
1352  "$webroot/splats", // default
1353  ));
1354 
1355  $this->splatVars = array(
1356  'title' => $this->form->title,
1357  'basename' => $basename,
1358  'webroot' => $webroot,
1359  'local_stylesheets' => array('Grima.css'),
1360  'form' => &$this->form,
1361  'messages' => &$this->messages,
1362  'width' => 9,
1363  );
1364  if (isset($this['redirect_url'])) {
1365  $this->splatVars['redirect_url'] = $this['redirect_url'];
1366  }
1367  }
Simple template engine.
Definition: grima-splats.php:12

◆ usage()

GrimaTask::usage ( )

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

1510  { # XXX rewrite for grima form
1511  global $argv;
1512  print "Usage: php ${argv[0]} ";
1513  foreach ($this->auto_args as $k => $v) {
1514  if (preg_match('/^(.):$/',$k,$m)) {
1515  print "-${m[1]} <$v> ";
1516  } else {
1517  if (preg_match('/^(.)::$/',$k,$m)) {
1518  print "[ -${m[1]} <$v> ] ";
1519  } else {
1520  if (preg_match('/^.$/',$k)) {
1521  print "[ -$k ] ";
1522  }
1523  }
1524  }
1525  }
1526  print "\n";
1527  exit;
1528  }

Member Data Documentation

◆ $args

GrimaTask::$args = array()

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

◆ $auto_args

GrimaTask::$auto_args = array()

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

◆ $el_override

GrimaTask::$el_override = array()

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

◆ $error

GrimaTask::$error = false

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

◆ $messages

GrimaTask::$messages = array()

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


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