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

Constructor & Destructor Documentation

◆ __construct()

GrimaTask::__construct ( )

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

1554 {
1555 $webroot = __DIR__;
1556 $base = get_class($this); //basename($_SERVER['PHP_SELF'],'.php');
1557 if (file_exists(join_paths($webroot, $base, "$base.xml")) and (!isset($this->formxml))) {
1558 $this->formxml = file_get_contents(join_paths($webroot, $base, "$base.xml"));
1559 }
1560 if (isset($this->formxml)) {
1561 $this->form = new GrimaForm();
1562 $this->form->fromXML($this->formxml);
1563 }
1564 }
A holder for the user inputs to a grima, base on the XML file.
Definition: grima-lib.php:1837
join_paths(... $paths)
Definition: grima-util.php:156

References join_paths().

Member Function Documentation

◆ addMessage()

GrimaTask::addMessage (   $type,
  $message 
)

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

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

Referenced by run().

◆ call()

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

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

1802 {
1803 $url = rtrim("../$grimaname/$grimaname.php?" . http_build_query($args), "?");
1804 do_redirect($url);
1805 }
do_redirect($url)
Definition: grima-util.php:45

References $args, and do_redirect().

Referenced by check_login().

◆ check_input()

GrimaTask::check_input ( )

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

1708 {
1709 if (isset($this->form)) {
1710 $input_good = true;
1711 foreach ($this->form->fields as $field) {
1712 if ($field->required) {
1713 if (!isset($this[$field->name])
1714 or !($this[$field->name])
1715 ) {
1716 $field->error_condition = "error";
1717 $field->error_message = "Field is required\n";
1718 $input_good = false;
1719 }
1720 }
1721 }
1722 return $input_good;
1723 } else {
1724
1725 foreach ($this->auto_args as $k => $v) {
1726 if (preg_match('/[^:]:$/', $k)) {
1727 if (!isset($this->args[$v]) or !($this->args[$v])) {
1728 return false;
1729 }
1730 }
1731 }
1732 return true;
1733 }
1734 }

Referenced by get_input(), get_input_param(), and run().

◆ check_login()

GrimaTask::check_login ( )

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

1613 {
1614 global $grima;
1615 if (isset($grima->apikey) and isset($grima->server)
1616 and ($grima->apikey) and ($grima->server)
1617 ) {
1618 return true;
1619 } else {
1620 GrimaTask::call('Login', array("redirect_url"=>$_SERVER['PHP_SELF']));
1621 exit;
1622 return false;
1623 }
1624 }
static call($grimaname, $args=array())
Definition: grima-lib.php:1801
$grima
Definition: grima-lib.php:5143

References $grima, and call().

Referenced by run().

◆ do_task()

GrimaTask::do_task ( )
abstract

Referenced by run().

◆ get_input()

GrimaTask::get_input ( )

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

1662 {
1663 if (isset($this->form)) {
1664 if (php_sapi_name() == "cli") { // command line
1665
1666 $param = "";
1667 foreach ($this->form->fields as $field) {
1668 $short = $field->shortname;
1669 if ($field->required) {
1670 $param .= "$short:";
1671 } else {
1672 $param .= "$short::";
1673 }
1674 }
1675
1676 if ($option = getopt($param)) {
1677 foreach ($this->form->fields as $field) {
1678 if (isset($option[$field->shortname])) {
1679 $this[$field->name] = $option[$field->shortname];
1680 }
1681 }
1682 /*
1683 } else {
1684 $this->usage(); exit;
1685 */
1686 }
1687
1688 if (!$this->check_input()) {
1689 $this->usage(); exit;
1690 }
1691
1692 } else { // web
1693 foreach ($this->form->fields as $field) {
1694 if (isset($_REQUEST[$field->name])) {
1695 $this[$field->name] = $_REQUEST[$field->name];
1696 /* sanitize */
1697 }
1698 }
1699 }
1700
1701 } else {
1702 // ??
1703 $this->get_input_param($this->auto_args);
1704 }
1705 }
usage()
Definition: grima-lib.php:1763
get_input_param($param)
Definition: grima-lib.php:1736
check_input()
Definition: grima-lib.php:1707

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

Referenced by run().

◆ get_input_param()

GrimaTask::get_input_param (   $param )

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

1737 {
1738 if (php_sapi_name() == "cli") { // command line
1739 if ($options = getopt(implode(array_keys($param)))) {
1740 foreach ($param as $k => $v) {
1741 $this->args[$v] = $options[$k[0]];
1742 }
1743 if (!$this->check_input()) {
1744 $this->usage(); exit;
1745 }
1746 } else {
1747 $this->usage(); exit;
1748 }
1749 } else { // web
1750 foreach ($param as $k => $v) {
1751 if (isset($_REQUEST[$v])) {
1752 $this->args[$v] = $_REQUEST[$v];
1753 }
1754 }
1755 if (!$this->check_input()) {
1756 $this->print_form(); exit;
1757 }
1758 }
1759 }
print_form()
Definition: grima-lib.php:1594

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

Referenced by get_input().

◆ offsetExists()

GrimaTask::offsetExists (   $offset )

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

1534 {
1535 return isset($this->args[$offset]);
1536 }

◆ offsetGet()

GrimaTask::offsetGet (   $offset )

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

1539 {
1540 return $this->args[$offset];
1541 }

◆ offsetSet()

GrimaTask::offsetSet (   $offset,
  $value 
)

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

1544 {
1545 $this->args[$offset] = $value;
1546 }

◆ offsetUnset()

GrimaTask::offsetUnset (   $offset )

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

1549 {
1550 unset($this->args[$offset]);
1551 }

◆ print_failure()

GrimaTask::print_failure ( )

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

1607 {
1608 $this->form->loadValues($this);
1609 $this->splat->splat('print_failure', $this->splatVars);
1610 }

Referenced by run().

◆ print_form()

GrimaTask::print_form ( )

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

1595 {
1596 $this->form->loadValues($this);
1597 $this->splat->splat('print_form', $this->splatVars);
1598 }

Referenced by get_input_param(), and run().

◆ print_success()

GrimaTask::print_success ( )

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

1601 {
1602 $this->form->loadPersistentValues($this);
1603 $this->splat->splat('print_success', $this->splatVars);
1604 }

Referenced by run().

◆ run()

GrimaTask::run ( )

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

1632 {
1633 $this->check_login(); // if not logged in, print login form
1634 $this->error = false;
1635 $this->get_input();
1636 $this->setup_splat(); // should happen after form xml read in get_input
1637 if ($this->check_input()) {
1638 try {
1639 $this->do_task();
1640 } catch (Exception $e) {
1641 $this->addMessage('error', $e->getMessage());
1642 $this->error = true;
1643 }
1644 } else {
1645 $this->print_form();
1646 exit;
1647 }
1648 if ($this->error) {
1649 $this->print_failure();
1650 } else {
1651 $this->print_success();
1652 }
1653 }
addMessage($type, $message)
Definition: grima-lib.php:1626
setup_splat()
Definition: grima-lib.php:1566
do_task()
check_login()
Definition: grima-lib.php:1612
get_input()
Definition: grima-lib.php:1661
print_failure()
Definition: grima-lib.php:1606
print_success()
Definition: grima-lib.php:1600

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

◆ RunIt()

static GrimaTask::RunIt ( )
static

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

1656 {
1657 $task = new static();
1658 $task->run();
1659 }

◆ setup_splat()

GrimaTask::setup_splat ( )

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

1567 {
1568 $webroot = __DIR__;
1569 $basename = get_class($this); //basename($_SERVER['PHP_SELF'],'.php');
1570
1571 $this->splat = new zemowsplat\Splat();
1572
1573 $this->splat->addBases(
1574 array(
1575 "$webroot/$basename/splats", // per-task overrides
1576 "$webroot/splats", // default
1577 )
1578 );
1579
1580 $this->splatVars = array(
1581 'title' => $this->form->title,
1582 'basename' => $basename,
1583 'webroot' => $webroot,
1584 'local_stylesheets' => array('Grima.css'),
1585 'form' => &$this->form,
1586 'messages' => &$this->messages,
1587 'width' => 9,
1588 );
1589 if (isset($this['redirect_url'])) {
1590 $this->splatVars['redirect_url'] = $this['redirect_url'];
1591 }
1592 }
Simple template engine.
Definition: grima-splats.php:12

Referenced by run().

◆ usage()

GrimaTask::usage ( )

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

1764 {
1765 // XXX rewrite for grima form
1766 global $argv;
1767 print "Usage: php ${argv[0]} ";
1768
1769 foreach ($this->form->fields as $field) {
1770 $k = $field->shortname;
1771 $n = $field->name;
1772 if ($field->required) {
1773 print "-$k <$n> ";
1774 } else {
1775 print "[-$k <$n>] ";
1776 }
1777 }
1778 print "\n"; exit;
1779
1780
1781 // old format
1782 /*
1783 foreach ($this->auto_args as $k => $v) {
1784 if (preg_match('/^(.):$/', $k, $m)) {
1785 print "-${m[1]} <$v> ";
1786 } else {
1787 if (preg_match('/^(.)::$/', $k, $m)) {
1788 print "[ -${m[1]} <$v> ] ";
1789 } else {
1790 if (preg_match('/^.$/', $k)) {
1791 print "[ -$k ] ";
1792 }
1793 }
1794 }
1795 }
1796 print "\n";
1797 exit;
1798 */
1799 }

Referenced by get_input(), and get_input_param().

Member Data Documentation

◆ $args

GrimaTask::$args = array()

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

Referenced by call().

◆ $auto_args

GrimaTask::$auto_args = array()

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

◆ $el_override

GrimaTask::$el_override = array()

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

◆ $error

GrimaTask::$error = false

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

◆ $messages

GrimaTask::$messages = array()

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


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