vcategories.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Thomas Tanghus
  6. * @copyright 2012 Thomas Tanghus <thomas@tanghus.net>
  7. * @copyright 2012 Bart Visscher bartv@thisnet.nl
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /**
  24. * Class for easy access to categories in VCARD, VEVENT, VTODO and VJOURNAL.
  25. * A Category can be e.g. 'Family', 'Work', 'Chore', 'Special Occation' or
  26. * anything else that is either parsed from a vobject or that the user chooses
  27. * to add.
  28. * Category names are not case-sensitive, but will be saved with the case they
  29. * are entered in. If a user already has a category 'family' for an app, and
  30. * tries to add a category named 'Family' it will be silently ignored.
  31. * NOTE: There is a limitation in that the the configvalue field in the
  32. * preferences table is a varchar(255).
  33. */
  34. class OC_VCategories {
  35. const PREF_CATEGORIES_LABEL = 'extra_categories';
  36. /**
  37. * Categories
  38. */
  39. private $categories = array();
  40. private $app = null;
  41. private $user = null;
  42. /**
  43. * @brief Constructor.
  44. * @param $app The application identifier e.g. 'contacts' or 'calendar'.
  45. * @param $user The user whos data the object will operate on. This
  46. * parameter should normally be omitted but to make an app able to
  47. * update categories for all users it is made possible to provide it.
  48. * @param $defcategories An array of default categories to be used if none is stored.
  49. */
  50. public function __construct($app, $user=null, $defcategories=array()) {
  51. $this->app = $app;
  52. $this->user = is_null($user) ? OC_User::getUser() : $user;
  53. $categories = trim(OC_Preferences::getValue($this->user, $app, self::PREF_CATEGORIES_LABEL, ''));
  54. $this->categories = $categories != '' ? unserialize($categories) : $defcategories;
  55. }
  56. /**
  57. * @brief Get the categories for a specific user.
  58. * @returns array containing the categories as strings.
  59. */
  60. public function categories() {
  61. OC_Log::write('core','OC_VCategories::categories: '.print_r($this->categories, true), OC_Log::DEBUG);
  62. usort($this->categories, 'strnatcasecmp'); // usort to also renumber the keys
  63. return $this->categories;
  64. }
  65. /**
  66. * @brief Checks whether a category is already saved.
  67. * @param $name The name to check for.
  68. * @returns bool
  69. */
  70. public function hasCategory($name) {
  71. return $this->in_arrayi($name, $this->categories);
  72. }
  73. /**
  74. * @brief Add a new category name.
  75. * @param $names A string with a name or an array of strings containing
  76. * the name(s) of the categor(y|ies) to add.
  77. * @param $sync bool When true, save the categories
  78. * @returns bool Returns false on error.
  79. */
  80. public function add($names, $sync=false) {
  81. if(!is_array($names)) {
  82. $names = array($names);
  83. }
  84. $names = array_map('trim', $names);
  85. $newones = array();
  86. foreach($names as $name) {
  87. if(($this->in_arrayi($name, $this->categories) == false) && $name != '') {
  88. $newones[] = $name;
  89. }
  90. }
  91. if(count($newones) > 0) {
  92. $this->categories = array_merge($this->categories, $newones);
  93. if($sync === true) {
  94. $this->save();
  95. }
  96. }
  97. return true;
  98. }
  99. /**
  100. * @brief Extracts categories from a vobject and add the ones not already present.
  101. * @param $vobject The instance of OC_VObject to load the categories from.
  102. */
  103. public function loadFromVObject($vobject, $sync=false) {
  104. $this->add($vobject->getAsArray('CATEGORIES'), $sync);
  105. }
  106. /**
  107. * @brief Reset saved categories and rescan supplied vobjects for categories.
  108. * @param $objects An array of vobjects (as text).
  109. * To get the object array, do something like:
  110. * // For Addressbook:
  111. * $categories = new OC_VCategories('contacts');
  112. * $stmt = OC_DB::prepare( 'SELECT carddata FROM *PREFIX*contacts_cards' );
  113. * $result = $stmt->execute();
  114. * $objects = array();
  115. * if(!is_null($result)) {
  116. * while( $row = $result->fetchRow()){
  117. * $objects[] = $row['carddata'];
  118. * }
  119. * }
  120. * $categories->rescan($objects);
  121. */
  122. public function rescan($objects, $sync=true) {
  123. $this->categories = array();
  124. foreach($objects as $object) {
  125. //OC_Log::write('core','OC_VCategories::rescan: '.substr($object, 0, 100).'(...)', OC_Log::DEBUG);
  126. $vobject = OC_VObject::parse($object);
  127. if(!is_null($vobject)) {
  128. $this->loadFromVObject($vobject, $sync);
  129. } else {
  130. OC_Log::write('core','OC_VCategories::rescan, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 50).'(...)', OC_Log::DEBUG);
  131. }
  132. }
  133. $this->save();
  134. }
  135. /**
  136. * @brief Save the list with categories
  137. */
  138. private function save() {
  139. usort($this->categories, 'strnatcasecmp'); // usort to also renumber the keys
  140. $escaped_categories = serialize($this->categories);
  141. OC_Log::write('core','OC_VCategories::save: '.print_r($this->categories, true), OC_Log::DEBUG);
  142. OC_Preferences::setValue($this->user, $this->app, self::PREF_CATEGORIES_LABEL, $escaped_categories);
  143. }
  144. /**
  145. * @brief Delete categories from the db and from all the vobject supplied
  146. * @param $names An array of categories to delete
  147. * @param $objects An array of arrays with [id,vobject] (as text) pairs suitable for updating the apps object table.
  148. */
  149. public function delete($names, array &$objects=null) {
  150. if(!is_array($names)) {
  151. $names = array($names);
  152. }
  153. OC_Log::write('core','OC_VCategories::delete, before: '.print_r($this->categories, true), OC_Log::DEBUG);
  154. foreach($names as $name) {
  155. OC_Log::write('core','OC_VCategories::delete: '.$name, OC_Log::DEBUG);
  156. if($this->hasCategory($name)) {
  157. OC_Log::write('core','OC_VCategories::delete: '.$name.' got it', OC_Log::DEBUG);
  158. unset($this->categories[$this->array_searchi($name, $this->categories)]);
  159. }
  160. }
  161. $this->save();
  162. OC_Log::write('core','OC_VCategories::delete, after: '.print_r($this->categories, true), OC_Log::DEBUG);
  163. if(!is_null($objects)) {
  164. foreach($objects as $key=>&$value) {
  165. $vobject = OC_VObject::parse($value[1]);
  166. if(!is_null($vobject)){
  167. $categories = $vobject->getAsArray('CATEGORIES');
  168. //OC_Log::write('core','OC_VCategories::delete, before: '.$key.': '.print_r($categories, true), OC_Log::DEBUG);
  169. foreach($names as $name) {
  170. $idx = $this->array_searchi($name, $categories);
  171. OC_Log::write('core','OC_VCategories::delete, loop: '.$name.', '.print_r($idx, true), OC_Log::DEBUG);
  172. if($idx !== false) {
  173. OC_Log::write('core','OC_VCategories::delete, unsetting: '.$categories[$this->array_searchi($name, $categories)], OC_Log::DEBUG);
  174. unset($categories[$this->array_searchi($name, $categories)]);
  175. //unset($categories[$idx]);
  176. }
  177. }
  178. OC_Log::write('core','OC_VCategories::delete, after: '.$key.': '.print_r($categories, true), OC_Log::DEBUG);
  179. $vobject->setString('CATEGORIES', implode(',', $categories));
  180. $value[1] = $vobject->serialize();
  181. $objects[$key] = $value;
  182. } else {
  183. OC_Log::write('core','OC_VCategories::delete, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 50).'(...)', OC_Log::DEBUG);
  184. }
  185. }
  186. }
  187. }
  188. // case-insensitive in_array
  189. private function in_arrayi($needle, $haystack) {
  190. return in_array(strtolower($needle), array_map('strtolower', $haystack));
  191. }
  192. // case-insensitive array_search
  193. private function array_searchi($needle, $haystack) {
  194. return array_search(strtolower($needle),array_map('strtolower',$haystack));
  195. }
  196. }
  197. ?>