PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/commands/folder/FolderTagsEdit.php

http://xerxes-portal.googlecode.com/
PHP | 124 lines | 69 code | 32 blank | 23 comment | 11 complexity | 1c97bc814a41a86a0b7e5cb36de42492 MD5 | raw file
  1. <?php
  2. /**
  3. * Add tags to a record
  4. *
  5. * @author David Walker
  6. * @copyright 2008 California State University
  7. * @link http://xerxes.calstate.edu
  8. * @license http://www.gnu.org/licenses/
  9. * @version $Id: FolderTagsEdit.php 1030 2010-01-05 18:42:09Z dwalker@calstate.edu $
  10. * @package Xerxes
  11. */
  12. class Xerxes_Command_FolderTagsEdit extends Xerxes_Command_Folder
  13. {
  14. public function doExecute()
  15. {
  16. $strUsername = $this->request->getSession( "username" );
  17. $iRecord = $this->request->getProperty( "record" );
  18. $strTags = $this->request->getProperty( "tags" ); // updated tags
  19. $strShadowTags = $this->request->getProperty( "tagsShaddow" ); // original tags
  20. // split tags out on comma
  21. $arrShadow = explode( ",", $strShadowTags );
  22. $arrTags = explode( ",", $strTags );
  23. for ( $x = 0 ; $x < count( $arrTags ) ; $x ++ )
  24. {
  25. $arrTags[$x] = Xerxes_Framework_Parser::strtolower( trim( $arrTags[$x] ) );
  26. }
  27. for ( $x = 0 ; $x < count( $arrShadow ) ; $x ++ )
  28. {
  29. $arrShadow[$x] = Xerxes_Framework_Parser::strtolower( trim( $arrShadow[$x] ) );
  30. }
  31. // remove any duplicates
  32. $arrTags = array_unique( $arrTags );
  33. // update the database
  34. $objData = new Xerxes_DataMap( );
  35. $objData->assignTags( $strUsername, $arrTags, $iRecord );
  36. // now update the cached version without recalculating all the
  37. // totals with a round-trip to the database
  38. $arrStored = $this->request->getSession( "tags" );
  39. // see which tags are new and which are actually being deleted or changed
  40. $arrDelete = array_diff( $arrShadow, $arrTags );
  41. $arrAdded = array_diff( $arrTags, $arrShadow );
  42. // deletes!
  43. foreach ( $arrDelete as $strTag )
  44. {
  45. foreach ( $arrStored as $strStoredKey => $iStoredValue )
  46. {
  47. if ( Xerxes_Framework_Parser::strtoupper( $strTag ) == Xerxes_Framework_Parser::strtoupper( $strStoredKey ) )
  48. {
  49. $iStoredValue = ( int ) $iStoredValue;
  50. if ( $iStoredValue > 1 )
  51. {
  52. // just deincrement it
  53. $iStoredValue --;
  54. $arrStored[$strStoredKey] = $iStoredValue;
  55. } else
  56. {
  57. // this was the only entry for the tag so remove it
  58. unset( $arrStored[$strStoredKey] );
  59. }
  60. }
  61. }
  62. }
  63. // adds!
  64. foreach ( $arrAdded as $strTag )
  65. {
  66. if ( $strTag != "" )
  67. {
  68. $bolExists = false;
  69. foreach ( $arrStored as $strStoredKey => $iStoredValue )
  70. {
  71. if ( Xerxes_Framework_Parser::strtoupper( $strTag ) == Xerxes_Framework_Parser::strtoupper( $strStoredKey ) )
  72. {
  73. // there is one in here already so increment
  74. $iStoredValue = ( int ) $iStoredValue;
  75. $iStoredValue ++;
  76. $arrStored[$strStoredKey] = $iStoredValue;
  77. $bolExists = true;
  78. }
  79. }
  80. // if it wasn't in there already, add it as the first
  81. if ( $bolExists == false )
  82. {
  83. $arrStored[$strTag] = 1;
  84. }
  85. }
  86. }
  87. // now store it back in session
  88. $this->setTagsCache( $arrStored );
  89. return 1;
  90. }
  91. }
  92. ?>