Simple ActionApps code examples

From ActionApps Documentation
Jump to: navigation, search

Copy an existing item - basic version

<?
/* Copied config.php3 from AA's include dirC
   tweaked to needs */
require_once "config.php3";
require_once $GLOBALS['AA_INC_PATH']."locsess.php3";
require_once $GLOBALS['AA_INC_PATH']."itemfunc.php3";

function fieldname($name) {
  //Add some extra dots to fieldname ...
  $len = strlen($name);
  $result = $name;
  if ($len < 16)
    for ($count = 0; $count < 16 - $len; $count++)
      $result .= ".";
  return $result;
}

function get_value($item_array, $field) {
  //return value of field from item_array
  return $item_array[fieldname($field)][0]["value"];
}

function test_get_and_store() {
  $item_id = "ac18cc62bd06ca592b6c32116e3e5159";
  $existing_content_array = GetItemContent($item_id);
  echo get_value($existing_content_array[$item_id], "headline");
  //var_dump($existing_content_array);
  var_dump($item_id);
  $new_item_id = new_id();
  $existing_content_array[$item_id][fieldname("headline")][0]["value"] =
    "Now finetune these thingies";
  $new_content_array = array($new_item_id => $existing_content_array[$item_id]);
  $content4id = new ItemContent($new_content_array[$new_item_id]);
  $content4id->setItemID($new_item_id);
  $added_to_db = $content4id->storeItem('insert',
                                        $invalidatecache = false,
                                        $feed = false);
}

test_get_and_store();
?>

Copy an existing item - classy version

<?
/* Copied config.php3 from AA's include dirC
   tweaked to needs */
require_once "config.php3";
require_once $GLOBALS['AA_INC_PATH']."locsess.php3";
require_once $GLOBALS['AA_INC_PATH']."itemfunc.php3";

class aa_item {
  /* Kasper's own class to deal with Action Apps Items */

  function aa_item($item_id = null) {
    if ($item_id) {
      $this->fetch_item($item_id);
    }
  }

  function fetch_item($item_id) {
    $this->item_id = $item_id;
    $this->content_array = GetItemContent($item_id);
  }

  function fieldname($name) {
    /* Add some extra dots to fieldname ... */
    $len = strlen($name);
    $result = $name;
    if ($len < 16)
      for ($count = 0; $count < 16 - $len; $count++)
        $result .= ".";
    return $result;
  }

  function value($field) {
    /* Return value of field */

    return $this->content_array[$this->item_id]
      [$this->fieldname($field)][0]["value"];
  }

  function set_value($field, $value) {
    /* Set value of field */

    $this->content_array[$this->item_id]
      [$this->fieldname($field)][0]["value"] = $value;
  }

  function save_as_new() {
    /* Save item with new ID */

    $new_item_id = new_id();
    $new_content_array = array($new_item_id =>
                               $this->content_array[$this->item_id]);
    $content4id = new ItemContent($new_content_array[$new_item_id]);
    $content4id->setItemID($new_item_id);
    $added_to_db = $content4id->storeItem('insert',
                                          $invalidatecache = false,
                                          $feed = false);
    $this->fetch_item($new_item_id);  }
}


function test_get_and_store() {
  $existing_item = new aa_item("ac18cc62bd06ca592b6c32116e3e5159");
  echo $existing_item->value("headline");
  $existing_item->set_value("headline", "New item test");
  $existing_item->save_as_new();
}

test_get_and_store();
?>