Source for file globals.php

Documentation is available at globals.php

  1. <?php
  2. /**
  3.  * Helper functions that are always available in Omeka.  As global functions,
  4.  * these should be used as little as possible in the application code
  5.  * to reduce coupling.
  6.  *
  7.  * @package Omeka
  8.  ***/
  9.  
  10. /**
  11.  * Retrieve an option from the Omeka database.
  12.  * 
  13.  * If the returned value represents an object or array, it must be unserialized
  14.  * by the caller before use.  For example,
  15.  * <code>$object = unserialize(get_option('plugin_object'))</code>.
  16.  * 
  17.  * @param string $name 
  18.  * @return string 
  19.  ***/ 
  20. function get_option($name{
  21.     $options Omeka_Context::getInstance()->getOptions();
  22.     return $options[$name];
  23. }
  24.  
  25. /**
  26.  * Set an option in the Omeka database.
  27.  * 
  28.  * Note that objects and arrays must be serialized before being saved.
  29.  * 
  30.  * @see get_option()
  31.  * @param string $name 
  32.  * @param string $value 
  33.  * @return void 
  34.  ***/
  35. function set_option($name$value)
  36. {
  37.     $db get_db();
  38.     $db->exec("REPLACE INTO $db->Option (name, value) VALUES (?, ?)"array($name$value));
  39.     
  40.     //Now update the options hash so that any subsequent requests have it available
  41.     $options Omeka_Context::getInstance()->getOptions();
  42.     $options[$name$value;
  43.     
  44.     Omeka_Context::getInstance()->setOptions($options);
  45. }
  46.  
  47. /**
  48.  * Delete an option from the database.
  49.  * 
  50.  * @param string $name 
  51.  * @return void 
  52.  ***/
  53. function delete_option($name)
  54. {
  55.     $db get_db();
  56.     $sql "
  57.     DELETE 
  58.     FROM $db->Option 
  59.     WHERE `name` = ?";
  60.     $db->query($sqlarray($name));
  61.     
  62.     $options Omeka_Context::getInstance()->getOptions();
  63.     if (isset($options[$name])) {
  64.         unset($options[$name]);
  65.     }
  66.     Omeka_Context::getInstance()->setOptions($options);
  67. }
  68.  
  69. /**
  70.  * Generate a URL slug from a piece of text.
  71.  * 
  72.  * Trims whitespace, replaces some prohibited characters with hyphens, and
  73.  * converts the resulting string to lowercase.
  74.  * 
  75.  * @param string $text 
  76.  * @return string 
  77.  ***/
  78. function generate_slug($text)
  79. {
  80.     $slug trim($text);
  81.     
  82.     //Replace prohibited characters in the title with - 's
  83.     $prohibited array(':''/'' ''.''#');
  84.     $replace array_fill(0count($prohibited)'-');
  85.     $slug str_replace($prohibited$replacestrtolower($slug) );
  86.     return $slug;
  87. }
  88.  
  89. /**
  90.  * Retrieve one column of a multidimensional array as an array.
  91.  * 
  92.  * @param string|integer$col 
  93.  * @param array 
  94.  * @return array 
  95.  ***/
  96. function pluck($col$array)
  97. {
  98.     $res array();
  99.     foreach ($array as $k => $row{
  100.         $res[$k$row[$col];
  101.     }
  102.     return $res;    
  103.  
  104. /**
  105.  * Retrieve the User record associated with the currently logged in user.
  106.  * 
  107.  * @return User|nullNull if no user is logged in.
  108.  ***/
  109. function current_user()
  110. {
  111.     return Omeka_Context::getInstance()->getCurrentUser();
  112. }
  113.  
  114. /**
  115.  * Retrieve the database object.
  116.  * 
  117.  * @return Omeka_Db 
  118.  ***/
  119. function get_db()
  120. {
  121.     return Omeka_Context::getInstance()->getDb();
  122. }
  123.  
  124. /**
  125.  * Log a message with 'DEBUG' priority.
  126.  * 
  127.  * This will do nothing if logging is not enabled via config.ini's log.errors
  128.  * setting.
  129.  * 
  130.  * @param string 
  131.  * @return void 
  132.  ***/
  133. function debug($msg)
  134. {
  135.     $context Omeka_Context::getInstance();
  136.     $logger $context->getLogger();
  137.     if ($logger{
  138.         $logger->debug($msg);
  139.     }
  140. }
  141.  
  142. /**
  143.  * Called during startup to strip out slashes from the request superglobals in
  144.  * order to avoid problems with PHP's magic_quotes setting.
  145.  * 
  146.  * Does not need to be called elsewhere in the application.
  147.  * 
  148.  * @access private
  149.  * @return mixed 
  150.  ***/
  151. function stripslashes_deep($value)
  152. {
  153.      $value is_array($valuearray_map('stripslashes_deep'$valuestripslashes($value);
  154.  
  155.      return $value;
  156. }
  157.  
  158. /**
  159.  * Declare a plugin hook implementation within a plugin.
  160.  * 
  161.  * @param string 
  162.  * @param mixed $callback Any valid PHP callback.
  163.  * @return void 
  164.  ***/
  165. function add_plugin_hook($hook$callback)
  166. {
  167.     get_plugin_broker()->addHook($hook$callback);
  168.  
  169. /**
  170.  * Declare the point of execution for a specific plugin hook.
  171.  * 
  172.  * All plugin implementations of a given hook will be executed when this is called.
  173.  * 
  174.  * The first argument corresponds to the string name of the hook.  Subsequent
  175.  * arguments will be passed to the plugin hook implementations.
  176.  * 
  177.  * <code>fire_plugin_hook('after_save_item', $item, $arg2);  //would call the plugin hook
  178.  * 'after_save_item' with those 2 arguments.</code>
  179.  *
  180.  * @access private
  181.  * @param string $hookName 
  182.  * @return array 
  183.  ***/
  184. function fire_plugin_hook()
  185. {
  186.     if ($pluginBroker get_plugin_broker()) {
  187.         $args func_get_args();
  188.         $hook array_shift($args);
  189.         return call_user_func_array(array($pluginBroker$hook)$args);
  190.     }
  191. }
  192.  
  193. /**
  194.  * Retrieve the output of fire_plugin_hook() as a string.
  195.  * 
  196.  * This is invoked in the same way as fire_plugin_hook().
  197.  * 
  198.  * @uses fire_plugin_hook()
  199.  * @return string 
  200.  ***/
  201. {
  202.     $args func_get_args();
  203.     ob_start();
  204.     call_user_func_array('fire_plugin_hook'$args);
  205.     $content ob_get_contents();
  206.     ob_end_clean();
  207.     return $content;
  208. }
  209.  
  210. /**
  211.  * Retrieve the output of a specific plugin's hook as a string.
  212.  * 
  213.  * This is like get_plugin_hook_output() but only calls the hook within the
  214.  * provided plugin.
  215.  * 
  216.  * @param string $pluginName 
  217.  * @param string $hookName 
  218.  * @param [any number of further arguments]
  219.  * @return string 
  220.  */
  221. {
  222.     $args func_get_args();
  223.     
  224.     // Get the plugin name (1st arg) and hook name (2nd arg).
  225.     $pluginName array_shift($args);
  226.     $hookName array_shift($args);
  227.     
  228.     // Get the specific hook.
  229.     $pluginBroker get_plugin_broker();
  230.     $hookNameSpecific $pluginBroker->getHook($pluginName$hookName);
  231.     
  232.     // Return null if the specific hook doesn't exist.
  233.     if (!$hookNameSpecific{
  234.         return null;
  235.     }
  236.     
  237.     // Buffer and return any output originating from the hook.
  238.     ob_start();
  239.     call_user_func_array($hookNameSpecific$args);
  240.     $content ob_get_contents();
  241.     ob_end_clean();
  242.     
  243.     return $content;
  244. }
  245.  
  246. /**
  247.  * @access private
  248.  * @return Omeka_Plugin_Broker|null
  249.  ***/
  250. function get_plugin_broker()
  251. {
  252.     if (Zend_Registry::isRegistered('pluginbroker')) {
  253.         return Zend_Registry::get('pluginbroker');
  254.     }
  255. }
  256.  
  257. /**
  258.  * Retrieves specified descriptive info for a plugin from its ini file.
  259.  *
  260.  * @param string $pluginDirName The directory name of the plugin
  261.  * @param string $iniKeyName The name of the key in the ini file
  262.  * @return null | string The value of the specified plugin key. If the key does not exist, it returns null
  263.  ***/
  264. function get_plugin_ini($pluginDirName$iniKeyName)
  265. {         
  266.    $pluginBroker Omeka_Context::getInstance()->getPluginBroker();
  267.    return $pluginBroker->getPluginIniValue($pluginDirName$iniKeyName);
  268. }
  269.  
  270. /**
  271.  * Declare a function that will be used to display files with a given MIME type.
  272.  * 
  273.  * @uses Omeka_Plugin_Broker::addMediaAdapter() See for info on arguments and
  274.  *  usage.
  275.  * @return void 
  276.  ***/
  277. function add_mime_display_type($mimeTypes$callbackarray $options=array())
  278. {
  279.     get_plugin_broker()->addMediaAdapter($mimeTypes$callback$options);
  280. }
  281.  
  282. /**
  283.  * Apply a set of plugin filters to a given value.
  284.  * 
  285.  * The first two arguments represent the name of the filter and the value to
  286.  * filter, and all subsequent arguments are passed to the individual filter
  287.  * implementations.
  288.  * 
  289.  * @since 0.10
  290.  * @uses Omeka_Plugin_Filters::applyFilters()
  291.  * @param string|array$filterName 
  292.  * @param mixed $valueToFilter 
  293.  * @return mixed 
  294.  ***/
  295. function apply_filters($filterName$valueToFilter)
  296. {
  297.     if ($pluginBroker get_plugin_broker()) {
  298.         $extraOptions array_slice(func_get_args()2);
  299.         return $pluginBroker->applyFilters($filterName$valueToFilter$extraOptions);
  300.     }
  301.     
  302.     // If the plugin broker is not enabled for this request (possibly for testing), return the original value.
  303.     return $valueToFilter;
  304. }
  305.  
  306. /**
  307.  * Declare a filter implementation.
  308.  * 
  309.  * @since 0.10
  310.  * @param string|array$filterName 
  311.  * @param callback $callback 
  312.  * @param integer $priority Optional Defaults to 10.
  313.  * @return void 
  314.  ***/
  315. function add_filter($filterName$callback$priority 10)
  316. {
  317.     if ($pluginBroker get_plugin_broker()) {
  318.         $pluginBroker->addFilter($filterName$callback$priority);
  319.     }
  320. }
  321.  
  322. /**
  323.  * Retrieve the ACL object.
  324.  * 
  325.  * @return Omeka_Acl 
  326.  ***/
  327. function get_acl()
  328. {
  329.     return Omeka_Context::getInstance()->getAcl();
  330. }
  331.  
  332. /**
  333.  * Determine whether or not the script is being executed through the
  334.  * administrative interface.
  335.  * 
  336.  * Can be used to branch behavior based on whether or not the admin theme is
  337.  * being accessed, but should not be relied upon in place of using the ACL for
  338.  * controlling access to scripts.
  339.  * 
  340.  * @return boolean 
  341.  ***/
  342. function is_admin_theme()
  343. {
  344.     return defined('ADMIN');
  345. }
  346.  
  347. /**
  348.  * Insert a new item into the Omeka database.
  349.  *
  350.  * * @param array $itemMetadata Optional Set of metadata options for configuring the
  351.  *  item.  Array which can include the following properties:
  352.  *  <ul>
  353.  *      <li>'public' (boolean)</li>
  354.  *      <li>'featured' (boolean)</li>
  355.  *      <li>'collection_id' (integer)</li>
  356.  *      <li>'item_type_id' (integer)</li>
  357.  *      <li>'item_type_name' (string)</li>
  358.  *      <li>'tags' (string, comma-delimited)</li>
  359.  *      <li>'tag_entity' (Entity, optional and only checked if 'tags' is given)</li>
  360.  *      <li>'overwriteElementTexts' (boolean) -- determines whether or not to
  361.  *  overwrite existing element texts.  If true, this will loop through the
  362.  *  element texts provided in $elementTexts, and it will update existing
  363.  *  records where possible.  All texts that are not yet in the DB will be
  364.  *  added in the usual manner.  False by default.</li>
  365.  *  </ul>
  366.  * 
  367.  * @param array $elementTexts Optional Array of element texts to assign to the item.
  368.  *   This follows the format:
  369.  *  <code>
  370.  *  array(
  371.   *      [element set name] => array(
  372.   *          [element name] => array(
  373.   *              array('text' => [string], 'html' => [false|true]),
  374.   *              array('text' => [string], 'html' => [false|true])
  375.   *          ),
  376.   *          [element name] => array(
  377.   *              array('text' => [string], 'html' => [false|true]),
  378.   *              array('text' => [string], 'html' => [false|true])
  379.   *          )
  380.   *      ),
  381.   *      [element set name] => array(
  382.   *          [element name] => array(
  383.   *              array('text' => [string], 'html' => [false|true]),
  384.   *              array('text' => [string], 'html' => [false|true])
  385.   *          ),
  386.   *          [element name] => array(
  387.   *              array('text' => [string], 'html' => [false|true]),
  388.   *              array('text' => [string], 'html' => [false|true])
  389.   *          )
  390.   *      )
  391.   *  );
  392.   *  </code>
  393.  *   See ActsAsElementText::addElementTextsByArray() for more info.
  394.  * 
  395.  * @param array $fileMetadata Optional Set of metadata options that allow one or more
  396.  *  files to be associated with the item.  Includes the following options:
  397.  *   <ul>
  398.  *       <li>'file_transfer_type' (string = 'Url|Filesystem|Upload' or
  399.  *  Omeka_File_Transfer_Adapter_Interface).  Corresponds to the
  400.  *  $transferStrategy argument for addFiles().</li>
  401.  *       <li>'file_ingest_options' OPTIONAL (array of possible options to pass
  402.  *  modify the behavior of the ingest script).  Corresponds to the $options
  403.  *  argument for addFiles().</li>
  404.  *       <li>'files' (array or string) Represents information indicating the file
  405.  *  to ingest.  Corresponds to the $files argument for addFiles().</li>
  406.  *  </ul>
  407.  * @uses ItemBuilder For more information on arguments and usage.
  408.  * @see ActsAsElementText::addElementTextsByArray()
  409.  * @return Item 
  410.  */
  411. function insert_item($metadata array()$elementTexts array()$fileMetadata array())
  412. {    
  413.     // Passing null means this will create a new item.
  414.     $builder new ItemBuilder($metadata$elementTexts$fileMetadata);
  415.     return $builder->build();
  416. }
  417.  
  418. /**
  419.  * Add files to an item.
  420.  * 
  421.  * @uses ItemBuilder::addFiles() See for information on arguments and notes
  422.  *  on usage.
  423.  * @param Item|integer$item 
  424.  * @param string|Omeka_File_Ingest_Abstract$transferStrategy 
  425.  * @param array $files 
  426.  * @param array $options Optional
  427.  * @return array 
  428.  ***/
  429. function insert_files_for_item($item$transferStrategy$files$options array())
  430. {
  431.     // TODO: Maybe this should be a separate helper class.
  432.     $helper new ItemBuilder(array()array()array()$item);
  433.     return $helper->addFiles($transferStrategy$files$options);
  434. }
  435.  
  436. /**
  437.  * @see insert_item()
  438.  * @uses ItemBuilder
  439.  * @param Item|int$item Either an Item object or the ID for the item.
  440.  * @param array $metadata Set of options that can be passed to the item.
  441.  * @param array $elementTexts 
  442.  * @param array $fileMetadata 
  443.  * @return Item 
  444.  ***/
  445. function update_item($item$metadata array()$elementTexts array()$fileMetadata array())
  446. {
  447.     $builder new ItemBuilder($metadata$elementTexts$fileMetadata$item);
  448.     return $builder->build();
  449. }
  450.  
  451. /**
  452.  * Insert a new item type.
  453.  *
  454.  * @param array $metadata Follows the format:
  455.  *  <code>
  456.  *  array(
  457.   *      'name'       => [string],
  458.   *      'description'=> [string]
  459.   *  )
  460.  *  </code>
  461.  * @param array $elementInfos An array containing element data. Each entry follows
  462.  *  one or more of the following formats:
  463.  *  <ol>
  464.  *  <li>An array containing element metadata</li>
  465.  *  <li>A string containing the element name</li>
  466.  *  </ol>
  467.  *  <code>
  468.  *     array(
  469.  *          array(
  470.  *              'name'        => [(string) name, required],
  471.  *              'description' => [(string) description, optional],
  472.  *              'record_type' => [(string) record type name, optional],
  473.  *              'data_type'   => [(string) data type name, optional],
  474.  *              'order'       => [(int) order, optional],
  475.  *              'record_type_id' => [(int) record type id, optional],
  476.  *              'data_type_id'   => [(int) data type id, optional]
  477.  *          ),
  478.  *          [(string) element name],
  479.  *      );
  480.  *  </code>
  481.  * @return ItemType 
  482.  * @throws Exception
  483.  ***/
  484. function insert_item_type($metadata array()$elementInfos array()) {
  485.     $builder new ItemTypeBuilder($metadata$elementInfos);    
  486.     return $builder->build();
  487. }
  488.  
  489.  
  490. /**
  491.  * Inserts a collection
  492.  * 
  493.  * @param array $metadata Follows the format:
  494.  *  <code> array(
  495.  *      'name'       => [string],
  496.  *      'description'=> [string],
  497.  *      'public'     => [true|false],
  498.  *      'featured'   => [true|false]
  499.  *      'collectors' => [array of entities, entity ids, or entity property arrays]
  500.  *  )</code>
  501.  * 
  502.  *  You can specify collectors in several ways.
  503.  *
  504.  *  You can provide an array of entity properties:
  505.  *  <code>
  506.  *  insert_collection(array('collectors'=>array(
  507.  *    array('first_name' => $entityFirstName1,
  508.  *          'middle_name' => $entityMiddleName1,
  509.  *          'last_name' => $entityLastName1,
  510.  *           ...
  511.  *          ),
  512.  *    array('first_name' => $entityFirstName2,
  513.  *          'middle_name' => $entityMiddleName2,
  514.  *          'last_name' => $entityLastName2,
  515.  *          ...
  516.  *          ),
  517.  *    array(...),
  518.  *    ...
  519.  *  ));
  520.  *  </code>
  521.  *
  522.  *  Alternatively, you can use an array of entity objects or entity ids.
  523.  *
  524.  *   insert_collection(array('collectors'=>array($entity1, $entity2, ...));
  525.  *   insert_collection(array('collectors'=>array($entityId1, $entityId2, ...));
  526.  *
  527.  *  Also you can mix the parameters:
  528.  *
  529.  *  <code>
  530.  *  insert_collection(array('collectors'=>array(
  531.  *     array('first_name' => $entityFirstName1,
  532.  *          'middle_name' => $entityMiddleName1,
  533.  *          'last_name' => $entityLastName1,
  534.  *           ...
  535.  *          ),
  536.  *    $entity2,
  537.  *    $entityId3,
  538.  *    ...
  539.  *  ));
  540.  *  </code>
  541.  * 
  542.  ***/
  543. function insert_collection($metadata array())
  544. {
  545.     $builder new CollectionBuilder($metadata);
  546.     return $builder->build();
  547. }
  548.  
  549. /**
  550.  * Insert an element set and its elements into the database.
  551.  * 
  552.  * @param string|array$elementSetMetadata Element set information.
  553.  *  <code>
  554.  *      [(string) element set name]
  555.  *      -OR-
  556.  *      array(
  557.  *          'name'        => [(string) element set name, required, unique],
  558.  *          'description' => [(string) element set description, optional]
  559.  *      );
  560.  *  </code>
  561.  * @param array $elements An array containing element data. Follows one of more
  562.  *  of the following formats:
  563.  *  <ol>
  564.  *  <li>An array containing element metadata</li>
  565.  *  <li>A string of the element name</li>
  566.  *  </ol>
  567.  *  <code>
  568.  *     array(
  569.  *          array(
  570.  *              'name'        => [(string) name, required],
  571.  *              'description' => [(string) description, optional],
  572.  *              'record_type' => [(string) record type name, optional],
  573.  *              'data_type'   => [(string) data type name, optional],
  574.  *              'record_type_id' => [(int) record type id, optional],
  575.  *              'data_type_id'   => [(int) data type id, optional]
  576.  *          ),
  577.  *          [(string) element name]
  578.  *      );
  579.  *  </code>
  580.  * @return ElementSet 
  581.  */
  582. function insert_element_set($elementSetMetadata array()array $elements array())
  583. {
  584.     $builder new ElementSetBuilder($elementSetMetadata$elements);
  585.     return $builder->build();
  586. }
  587.  
  588. /**
  589.  * Releases an object from memory.
  590.  * 
  591.  * Use this fuction after you are done using an Omeka model object to prevent
  592.  * memory leaks.  Required because PHP 5.2 does not do garbage collection on
  593.  * circular references.
  594.  *
  595.  * @param mixed 
  596.  */
  597. function release_object(&$var
  598. {
  599.     if (is_array($var)) {
  600.         array_walk($var'release_object');
  601.     else if (is_object($var)) {
  602.         $var->__destruct();
  603.     }
  604.     $var null;
  605. }
  606.  
  607. /**
  608.  * Return either the value or, if it's empty, output the default.
  609.  * 
  610.  * @param mixed $value 
  611.  * @param mixed $default 
  612.  * @return mixed 
  613.  */
  614. function not_empty_or($value$default
  615. {
  616.     return !empty($value$value $default;
  617. }
  618.  
  619.  
  620.  
  621. /**
  622.   * Returns whether a value is true or not.
  623.   * If the value is a string and its lowercased value is 'true' or '1', it returns true.
  624.   * If the value is an integer and equal to 1, then it returns true.
  625.   * Otherwise it returns false.
  626.   * @param string $value 
  627.   * @return boolean 
  628.   ***/
  629. function is_true($value
  630. {
  631.     if ($value === null{
  632.         return false;
  633.     }
  634.     $value strtolower(trim($value));
  635.     return ($value == '1' || $value == 'true');
  636. }

Documentation generated on Thu, 15 Oct 2009 15:37:57 -0400 by phpDocumentor 1.4.2